You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Route53 HostedZone ID获取域名?CloudFormation参数优化

如何在CloudFormation中避免同时传入Route53 Hosted Zone ID和域名?

当然可以!你完全没必要让用户同时提供Route53 Hosted Zone的ID和域名,CloudFormation提供了几种优雅的方式来解决这个问题,下面根据你的Hosted Zone创建场景给出具体方案:

方案1:如果Hosted Zone在另一个CloudFormation栈中创建

如果你的Hosted Zone是通过独立的CloudFormation栈部署的,你可以在那个栈里导出Hosted Zone的ID和域名,然后在当前栈中通过Fn::ImportValue导入这两个值。这样用户只需要提供Hosted Zone所在的栈名,不用手动输入ID和域名。

步骤1:在Hosted Zone栈中添加输出

Outputs:
  HostedZoneId:
    Value: !Ref MyHostedZone
    Export:
      Name: !Sub "${AWS::StackName}-HostedZoneId"
  HostedZoneName:
    Value: !GetAtt MyHostedZone.Name
    Export:
      Name: !Sub "${AWS::StackName}-HostedZoneName"

步骤2:在当前栈中导入并使用

修改你的参数和RecordSet资源:

Parameters:
  HostedZoneStackName:
    Type: String
    Description: Name of the CloudFormation stack that created your Route53 Hosted Zone
  EnvType:
    Type: String
    Description: Environment type (e.g., prod, staging)
  DatabaseEndPoint:
    Type: String
    Description: RDS instance endpoint

Resources:
  RDSDNSRecord:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId: !ImportValue !Sub "${HostedZoneStackName}-HostedZoneId"
      Comment: "DNS name for RDS"
      Name: !Join ["", [!Ref EnvType, ".rds.", !Ref AWS::Region, ".", !ImportValue !Sub "${HostedZoneStackName}-HostedZoneName"]]
      Type: "CNAME"
      TTL: "900"
      ResourceRecords: [!Ref DatabaseEndPoint]

方案2:如果Hosted Zone和RecordSet在同一个栈中创建

如果Hosted Zone是和当前RecordSet在同一个CloudFormation栈里定义的,直接用!GetAtt就能获取Hosted Zone的域名,完全不需要额外参数。

修改后的代码示例:

Parameters:
  EnvType:
    Type: String
    Description: Environment type (e.g., prod, staging)
  DatabaseEndPoint:
    Type: String
    Description: RDS instance endpoint
  HostedZoneDomain:
    Type: String
    Description: Root domain name for your Hosted Zone (e.g., example.com)

Resources:
  MyHostedZone:
    Type: AWS::Route53::HostedZone
    Properties:
      Name: !Ref HostedZoneDomain

  RDSDNSRecord:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId: !Ref MyHostedZone
      Comment: "DNS name for RDS"
      Name: !Join ["", [!Ref EnvType, ".rds.", !Ref AWS::Region, ".", !GetAtt MyHostedZone.Name]]
      Type: "CNAME"
      TTL: "900"
      ResourceRecords: [!Ref DatabaseEndPoint]

方案3:如果Hosted Zone是手动创建的

如果你的Hosted Zone是手动在Route53控制台创建的,可以把它的ID和域名存储到AWS Systems Manager Parameter Store中,然后在CloudFormation中用Fn::GetParameter获取这些值,用户只需要知道参数名称即可。

步骤1:在SSM Parameter Store中存储值

  • 创建参数/your-app/route53/hosted-zone-id,值为你的Hosted Zone ID
  • 创建参数/your-app/route53/hosted-zone-name,值为你的Hosted Zone域名(注意要带末尾的.,比如example.com.)

步骤2:在CloudFormation中引用参数

Parameters:
  EnvType:
    Type: String
    Description: Environment type (e.g., prod, staging)
  DatabaseEndPoint:
    Type: String
    Description: RDS instance endpoint

Resources:
  RDSDNSRecord:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId: !GetParameter
        Name: /your-app/route53/hosted-zone-id
      Comment: "DNS name for RDS"
      Name: !Join ["", [!Ref EnvType, ".rds.", !Ref AWS::Region, ".", !GetParameter
        Name: /your-app/route53/hosted-zone-name]]
      Type: "CNAME"
      TTL: "900"
      ResourceRecords: [!Ref DatabaseEndPoint]

额外说明

如果你的场景比较特殊(比如Hosted Zone是手动创建且不想用SSM),还可以通过自定义CloudFormation资源调用Route53的GetHostedZone API来获取域名,但这种方式需要编写Lambda函数,复杂度较高,一般不推荐。前面的三种方案已经覆盖了绝大多数常见场景。

内容的提问来源于stack exchange,提问作者Frodo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:04:17