首页 > 解决方案 > CloudFormation:以 HostedZone 身份访问 PrivateDnsNamespace

问题描述

文档ServiceDiscovery::PrivateDnsNamespace指出:

当您创建私有 DNS 命名空间时,AWS Cloud Map 会自动创建一个与命名空间同名的 Amazon Route 53 私有托管区域。

在 CloudFormation 中,有什么方法可以访问创建的HostedZone(作为 a Route53::HostedZone),以便我可以向其中添加额外Route53:RecordSet的 s?

例如,我想在私有 DNS 条目中公开我的 RDS 和 ElastiCache (Redis) 实例。我意识到这可能是 XY 问题,但我正在我的遗留代码中的标准位置访问这些系统,并尝试在我的第一次 ECS 尝试中最小化定制(以及潜在的错误)。

警告:虽然 AWS 服务发现策略可用于公开 DB 和 RDS 等服务,但有几个条件:

标签: amazon-web-servicesamazon-cloudformationamazon-ecsamazon-route53service-discovery

解决方案


无法访问HostedZone在 CloudFormation 中创建的内容。

不过,如果您想向此私有区域添加其他记录,您可以使用AWS::ServiceDiscovery::ServiceAWS::ServiceDiscovery::Instance资源。

基本上,您需要为您的 RDS 实例创建一项服务,并为您的 Redis 实例创建一项服务。然后在服务中创建实例。

RDS 示例:

RDSSDService:
  Type: AWS::ServiceDiscovery::Service
  Properties:
    DnsConfig:
      DnsRecords:
        - TTL: 60
          Type: CNAME
      NamespaceId:
        Ref: YourNamespace
      RoutingPolicy: WEIGHTED
    Name: my-rds-service # this will become the record name
    NamespaceId:
      Ref: YourNamespace

RDSSDInstance: # If your RDS instance is in the same stack
  Type: AWS::ServiceDiscovery::Instance
  Properties:
    InstanceAttributes:
      AWS_INSTANCE_CNAME:
        Fn::GetAtt:
          - YourInstanceLogicalName
          - Endpoint.Address
    ServiceId:
      Fn::GetAtt:
        - RDSSDService
        - Id
    InstanceId: # this is optional
      Ref: YourInstanceLogicalName

RDSInstance: # If referencing an existing instance
  Type: AWS::ServiceDiscovery::Instance
  Properties:
    InstanceAttributes:
      AWS_INSTANCE_CNAME: xyz.abc.us-east-1.rds.amazonaws.com # or with Fn::ImportValue
    ServiceId:
      Fn::GetAtt:
        - RDSSDService
        - Id
    InstanceId: xyz # this is optional

推荐阅读