有没有办法从 CFT 中的 DNS 名称中获取 HostedZoneId?

问题描述

在 CloudFormation 模板中,我们可以从 DNS 名称获取 HostedZoneId 吗?一种方法是我们检查资源并查找 HostedZoneId 并插入它。我们是否也可以使用 GetAtt 或任何其他函数获取 id。

我想在 HostedZoneId 上做这样的事情,但这是不正确的。有没有其他方法可以做到这一点?

Parameter:
  VPCednS:
  Description: VPCe DNS target 
  Type: String

Resources:
  PrimaryRecord:
    Type: AWS::Route53::RecordSet
    Properties: 
      AliasTarget: 
        HostedZoneId: !GetAtt (!Ref "VPCednS").HostedZoneId
        DNSName: !Ref "VPCednS"
        EvaluateTargetHealth: true
      Failover: PRIMARY
      HostedZoneName: example.com
      Name: service.example.com
      Type: A

解决方法

遗憾的是,在普通的 CFN 中没有这样的方法。如果您确实需要此类功能,则必须使用 custom resource

资源将采用 lambda 函数的形式,它将您的 DNS 名称作为输入参数。然后使用 AWS SDK(例如 boto3),它会找到并匹配相应的托管区域,并将其 ID 返回到您的 CFN 堆栈中以供进一步使用。