当采用AWS::Route53::HostedZone::Id类型的参数时,有没有一种方法来获取HostedZone名称?

托管区域已经存在,但不是使用Cloudformation创建的,因此我无法从另一个模板引用该名称。

使用AWS::Route53::HostedZone::Id类型允许用户从下拉列表中进行选择,但选择的ID不是名称。

是否可以从ID中获取名称,以便可以创建记录集?

这是我正在使用的模板,请注意记录集条目的名称,在这里我们需要托管区域的名称来创建记录集。

AWSTemplateFormatVersion: '2010-09-09'
Description: Route53
Parameters:
  HostedZone:
    Type: AWS::Route53::HostedZone::Id
    Description: The Hosted Zone for the Record Set
  RecordSetName:
    Type: String
    Description: The name of the record set (all lowercase)

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId: !Ref HostedZone
      Comment: DNS name
      Name: !Sub ${RecordSetName}.??????
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.1

最佳答案

给定您似乎要解决的问题(为您的顶点域添加A记录),您实际上不需要AWS::Route53::HostedZone::Id类型的下拉参数选择器。相反,您可以只使用String输入并在HostedZoneName中使用HostedZoneId而不是AWS::Route53::RecordSet,如下所示:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  DomainName:
    Type: String
    Description: apex domain name

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneName: !Sub '${DomainName}.'
      Comment: DNS name
      Name: !Ref DomainName
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.1

(请注意,您需要在.DomainName的末尾添加额外的句点HostedZoneName)。

如果您想要一个子域,则可以执行以下操作:
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  DomainName:
    Type: String
    Description: apex domain name
  DomainPrefix:
    Type: String
    Description: sub domain prefix

Resources:
  Route53:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneName: !Sub '${DomainName}.'
      Comment: DNS name
      Name: !Sub '${DomainPrefix}.${DomainName}'
      Type: A
      TTL: '60'
      ResourceRecords:
        - 10.1.1.2

引用Fn::GetAtt,您将在为您的资源创建cloudformation导出时使用它们,而不是像在本问题中那样在使用资源时使用它们。

如果愿意,可以创建包含顶点域名和托管区域ID的导出文件,这是我为保持整洁而希望执行的操作。但是,导出是特定于区域的,因此,如果您跨多个区域进行部署(如果您正在使用CloudFront并希望将API部署到us-east-1以外的地方,则可能会强加于您),那么您将需要在某些情况下伪造导出内容地区。

关于amazon-web-services - AWS Cloudformation从托管区域ID获取托管区域名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57257106/

10-09 08:21
查看更多