云形成将RDS端点设置为Route53 CName记录

4
下面是一个以YAML格式编写的云形成文件示例。思路是让Route53记录依赖于创建RDS数据库,一旦创建完成后,从RDS数据库的端点获取值。 我在这些参考文档中进行了大量调查,但一直无法正确使用语法。

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

你可以看到它应该有一个返回值,但我不确定如何获取并将其用于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}'.

我在这里找到了非常接近我所需的东西,https://forums.aws.amazon.com/thread.jspa?threadID=69082,但一直出现语法错误,不确定YAML等效物是什么? - Joseph Astrahan
3个回答

8
使用!GetAtt的简写形式更易读。
ResourceRecords:
  - !GetAtt RDSDatabase.Endpoint.Address

你能给我展示具体的语法错误吗?我目前正在完全相同的方式下使用它。 - Laurent Jalbert Simard
我认为这是因为我正在使用无服务器,但它会显示类似于<!/getattr>属性无效的错误。我会尽快重新创建此错误以便为您提供准确的错误信息。如果您的方法有效,我肯定更喜欢那个。 - Joseph Astrahan

6
找到答案了,就是这个...
ResourceRecords:
        - {"Fn::GetAtt": ["RDSDatabase","Endpoint.Address"]}

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


0
我所做的是创建了一个环境变量来存储指向RDS实例端点的引用,并根据需要使用它。
environment:
  rdsEndpoint: !GetAtt <Name of the RDS instance>.Endpoint.Address

现在,如果我想使用它,我可以这样使用

const endpoint = process.env.rdsEndpoint

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接