AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
  endpoint:
    Type: String
    Description: The Secrets Manager endpoint to hit
  functionName:
    Type: String
    Description: The name of the lambda function
  vpcSubnetIds:
    Type: CommaDelimitedList
    Description: List of VPC subnet IDs
    Default: ''
  vpcSecurityGroupIds:
    Type: CommaDelimitedList
    Description: List of VPC security grouop IDs
    Default: ''
Conditions:
  AddVpcConfig:
    Fn::And:
    - Fn::Not:
      - Fn::Equals:
        - ''
        - Fn::Join:
          - ''
          - Ref: vpcSubnetIds
    - Fn::Not:
      - Fn::Equals:
        - ''
        - Fn::Join:
          - ''
          - Ref: vpcSecurityGroupIds
Resources:
  SecretsManagerRDSSQLServerRotationSingleUser:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName:
        Ref: functionName
      Description: Conducts an AWS SecretsManager secret rotation for RDS SQLServer using single user rotation scheme
      Handler: lambda_function.lambda_handler
      Runtime: python2.7
      CodeUri:
        Bucket: <%REPO_BUCKET%>
        Key: c96dd353-9e69-4a73-b443-e21e4a79bace
      Timeout: 30
      Policies:
      - VPCAccessPolicy: {}
      - AWSSecretsManagerRotationPolicy:
          FunctionName:
            Ref: functionName
      Environment:
        Variables:
          SECRETS_MANAGER_ENDPOINT:
            Ref: endpoint
      VpcConfig:
        Fn::If:
        - AddVpcConfig
        - SubnetIds:
            Ref: vpcSubnetIds
          SecurityGroupIds:
            Ref: vpcSecurityGroupIds
        - Ref: AWS::NoValue
      Tags:
        SecretsManagerLambda: Rotation
Outputs:
  RotationLambdaARN:
    Description: The ARN of the rotation lambda
    Value:
      Fn::GetAtt:
      - SecretsManagerRDSSQLServerRotationSingleUser
      - Arn