Cloudformation 获取 RDS 端点到 Route 53 CName 记录

Cloud Formation Set RDS Endpoint to Route53 CName Record

以下是 YAML 格式的示例云形成文件。这个想法是让 Route53 记录依赖于 RDS 数据库的创建,然后一旦创建它就从 RDS 数据库的端点获取值。

我在这些参考文档中查了很多资料,但始终未能获得正确的语法。

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

您可以看到它应该有一个 return 值,但我不确定如何获取它并将其用于 route53 cName 记录名称。

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

我试过这样做但没有成功- ${!Ref RDSDatabase.Endpoint.Address}

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

找到答案是这个...

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

我不知道我可以在 YAML 中使用这样的括号...

使用 !GetAtt 缩写形式更具可读性。

ResourceRecords:
  - !GetAtt RDSDatabase.Endpoint.Address