Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}'.
like image 699
Joseph Astrahan Avatar asked Oct 16 '25 20:10

Joseph Astrahan


1 Answers

Using the !GetAtt short form is even more readable.

ResourceRecords:
  - !GetAtt RDSDatabase.Endpoint.Address
like image 139
Laurent Jalbert Simard Avatar answered Oct 18 '25 17:10

Laurent Jalbert Simard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!