顯示具有 Iac 標籤的文章。 顯示所有文章
顯示具有 Iac 標籤的文章。 顯示所有文章

2023年8月24日 星期四

[AWS] AWS Route53 private zone cross account

  Written by: AWS Community Builder Wang Sheng Hau前幾天跟朋友聊天聊到一個需求

他們想做 A account開 Route53 private zone 讓 B account 也可以使用到

於是貼了文件給他 [1] 

但是他們想透過 Iac 的方式來達成,不想透過 Web UI 來進行

因此找了 AWS CLI 的指令給他 [2],他還是不滿足,於似乎就找了 Cloudformation 的解法了 [3]

AWSTemplateFormatVersion: '2010-09-09'
Description: Create VPC and PHZ association

Parameters:
  SourceAccountId:
    Type: String
    Description: The AWS Account ID where the PHZ is hosted
  PHZName:
    Type: String
    Description: The name of the Private Hosted Zone

Resources:
  MyVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: "10.0.0.0/16"
      EnableDnsSupport: true
      EnableDnsHostnames: true

  MyVpcDnsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              AWS:
                - arn:aws:iam::${SourceAccountId}:root
            Action: sts:AssumeRole

  MyVPCAssociation:
    Type: AWS::Route53::VPCAssociationAuthorization
    Properties:
      HostedZoneId: !FindInMap [PHZMap, !Ref "AWS::Region", PHZId]
      VPCId: !Ref MyVPC
      VPCRegion: !Ref "AWS::Region"

Mappings:
  PHZMap:
    us-east-1:
      PHZId: "PHZID-1234567890abcdef0"  # Replace with your PHZ ID for us-east-1
    us-west-2:
      PHZId: "PHZID-0987654321fedcba0"  # Replace with your PHZ ID for us-west-2



此模板示範了如何創建一個 VPC(MyVPC)並將其與指定的 PHZ 關聯。請注意以下事項:

模板使用了兩個參數:SourceAccountId(PHZ 所在的 AWS 帳戶 ID)和 PHZName(PHZ 的名稱)。

它創建一個 VPC,啟用了 DNS 支援和主機名稱解析。

它創建一個 IAM 角色(MyVpcDnsRole),該角色允許來自 SourceAccountId 的 AWS 帳戶假設該角色。

最後,它使用 AWS::Route53::VPCAssociationAuthorization 來關聯 VPC 和 PHZ。

您需要替換 PHZMap 中的 PHZ ID,以確保它匹配您的 PHZ 資源。此外,請注意,實際的 CloudFormation 模板可能需要包括更多的資源和設定,以滿足您的具體需求。這個範例僅供參考,您可以根據自己的需求進行修改和擴展。


參考文件

[1] https://docs.aws.amazon.com/zh_tw/Route53/latest/DeveloperGuide/hosted-zone-private-associate-vpcs-different-accounts.html

[2] https://repost.aws/knowledge-center/route53-private-hosted-zone

[3] https://docs.aws.amazon.com/zh_tw/AWSCloudFormation/latest/UserGuide/aws-resource-route53-hostedzone.html