CloudFormation:访问 PrivateDnsNamespace 作为 HostedZone

CloudFormation: Access PrivateDnsNamespace as HostedZone

ServiceDiscovery::PrivateDnsNamespacedocumentation 指出:

When you create a private DNS namespace, AWS Cloud Map automatically creates an Amazon Route 53 private hosted zone that has the same name as the namespace.

在 CloudFormation 中,是否有任何方法可以访问创建的 HostedZone(作为 Route53::HostedZone)以便我可以向其中添加额外的 Route53:RecordSet

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

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

无法访问在 CloudFormation 中创建的 HostedZone

不过,如果您想向这个私人区域添加额外的记录,您可以使用 AWS::ServiceDiscovery::Service and AWS::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