AWS CloudFormation:从托管区域ID获取托管区域名称

13
当使用类型为 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

你几乎肯定需要使用自定义资源,它可以查找该信息并为该ID返回名称。 - 404
3个回答

6

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

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,则可能被强制执行),则需要在其中一些区域伪造导出。


感谢您提供有关HostedZoneName末尾"."的提示!这在文档中并没有明确说明。 - Schrockwell
1
我会把这描述为一个有用的解决方法(当然,这是一个完全有效的回答 - 所以我点了+1)。 要求模板用户在其帐户中选择实际托管区域非常有价值,以便正确地使用模板。 不幸的是,AWS :: Route53 :: HostedZone :: Id是唯一支持的Route53参数类型。 看起来像是一个差距。 - nickform

1

托管区域 ID 在 Route 53 控制台 UI 中显示,类似于 Z1AVC899B05E2Y


0

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