Search code examples
amazon-rdsaws-cloudformationamazon-route53serverless-frameworkserverless

Cloud Formation Set RDS Endpoint to Route53 CName Record


Below is an example cloud formation file in YAML format. The idea is to make the Route53 Record depend upon creation of the RDS Database and then once its created get the value from the endpoint of the RDS Database.

I did a lot of poking around in these reference documents but kept failing to get the syntax correct.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html#aws-properties-rds-database-instance-returnvalues

You can see it should have a return value but I'm not sure how to get it and use it for the route53 cName record name.

resources:
      Resources:
        uploadBucket:
           Type: AWS::S3::Bucket
           Properties:
             BucketName: ${self:custom.uploadBucket}
        RDSDatabase:
          Type: AWS::RDS::DBInstance
          Properties:
            Engine : mysql
            MasterUsername: ${env:RDS_USERNAME}
            MasterUserPassword: ${env:RDS_PASSWORD}
            DBInstanceClass : db.t2.micro
            AllocatedStorage: '5'
            PubliclyAccessible: true
            #TODO: The Value of Stage is also available as a TAG automatically which I may use to replace this manually being put here..
            Tags:
              -
                Key: "Name"
                Value: ${self:custom.databaseName}
          DeletionPolicy: Snapshot
        DNSRecordSet:
          Type: AWS::Route53::RecordSet
          Properties:
            HostedZoneName: mydomain.com.
            Name: database-${self:custom.stage}.mydomain.com
            Type: CNAME
            TTL: '300'
            ResourceRecords:
            - [[Put End Point Here]]
          DependsOn: RDSDatabase

I tried doing this but no luck - ${!Ref RDSDatabase.Endpoint.Address}

An error occurred: DNSRecordSet - Invalid Resource Record: FATAL problem: RRDATANotSingleField (Value contains spaces) encounte
red with '${!Ref RDSDatabase.Endpoint.Address}'.

Solution

  • Found the answer its this...

    ResourceRecords:
            - {"Fn::GetAtt": ["RDSDatabase","Endpoint.Address"]}
    

    I didn't know I could use the brackets like that in YAML...