與另一個 AWS 帳戶 中的 VPC 對等 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

與另一個 AWS 帳戶 中的 VPC 對等

您可以透過使用 AWS::EC2::VPCPeeringConnection 與另一個 AWS AWS 帳戶 中的虛擬私有雲端 (VPC) 對等。這會在兩個 VPC 之間建立網路連線,讓您能在兩者之間路由流量,使其如同在相同的網路中通訊。VPC 對等互連有助於促進資料存取和資料傳輸。

若要建立 VPC 對等互連,您需要在單一 CloudFormation 堆疊內授權兩個獨立 AWS 帳戶 帳戶。

如需詳細資訊,請參閱《Amazon VPC 互連指南》中的 VPC 互連限制。

必要條件

  1. 您需要對等 VPC ID、對等 AWS 帳戶 ID,以及跨帳戶存取權角色,才能建立對等互連。

    注意

    本演練涉及兩個帳戶:第一個是允許跨帳戶對等的帳戶 (「接受者帳戶」)。第二個是請求對等連線的帳戶 (「請求者帳戶」)。

  2. 若要接受 VPC 對等互連,您必須擔任跨帳戶存取角色。該資源的行為方式與同一帳戶中的 VPC 對等互連資源相同。如需有關 IAM 管理員如何授予許可以擔任跨帳戶角色的資訊,請參閱《IAM 使用者指南》中的向使用者授予切換角色的許可

步驟 1:建立 VPC 和跨帳戶角色

在此步驟中,您需要在「接受者帳戶」 中建立 VPC 和角色。

建立 VPC 和跨帳戶存取權角色
  1. 請登入 AWS 管理主控台,開啟位於 https://console.aws.amazon.com/cloudformation 的 CloudFormation 主控台。

  2. 堆疊頁面的右上角,選擇建立堆疊,並選擇使用新資源 (標準)

  3. 對於先決條件 - 準備範本,選擇選擇現有範本,然後選擇上傳範本檔案選擇檔案

  4. 在本機電腦上打開文字編輯器,然後新增其中一個下列範本。儲存檔案,並返回主控台將其選為範本檔案。

    範例 JSON
    { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Create a VPC and an assumable role for cross account VPC peering.", "Parameters": { "PeerRequesterAccountId": { "Type": "String" } }, "Resources": { "vpc": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.1.0.0/16", "EnableDnsSupport": false, "EnableDnsHostnames": false, "InstanceTenancy": "default" } }, "peerRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Statement": [ { "Principal": { "AWS": { "Ref": "PeerRequesterAccountId" } }, "Action": [ "sts:AssumeRole" ], "Effect": "Allow" } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ec2:AcceptVpcPeeringConnection", "Resource": "*" } ] } } ] } } }, "Outputs": { "VPCId": { "Value": { "Ref": "vpc" } }, "RoleARN": { "Value": { "Fn::GetAtt": [ "peerRole", "Arn" ] } } } }
    範例 YAML
    AWSTemplateFormatVersion: 2010-09-09 Description: Create a VPC and an assumable role for cross account VPC peering. Parameters: PeerRequesterAccountId: Type: String Resources: vpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.1.0.0/16 EnableDnsSupport: false EnableDnsHostnames: false InstanceTenancy: default peerRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Principal: AWS: !Ref PeerRequesterAccountId Action: - 'sts:AssumeRole' Effect: Allow Path: / Policies: - PolicyName: root PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: 'ec2:AcceptVpcPeeringConnection' Resource: '*' Outputs: VPCId: Value: !Ref vpc RoleARN: Value: !GetAtt - peerRole - Arn
  5. 選擇下一步

  6. 命名堆疊 (例如 VPC-owner),然後在 PeerRequesterAccountId 欄位中輸入請求者帳戶的 AWS 帳戶 ID。

  7. 接受預設值,然後選擇 Next (下一步)

  8. 選取我知道 CloudFormation 可能會建立 IAM 資源,然後選擇建立堆疊

步驟 2:建立包含 AWS::EC2::VPCPeeringConnection 的範本

現在您已建立 VPC 和跨帳戶角色,可以使用另一個 AWS 帳戶 (請求者帳戶) 與該 VPC 對等。

建立其中包含 AWS::EC2::VPCPeeringConnection 資源的範本
  1. 回到 CloudFormation 主控台首頁。

  2. 堆疊頁面的右上角,選擇建立堆疊,並選擇使用新資源 (標準)

  3. 對於先決條件 - 準備範本,選擇選擇現有範本,然後選擇上傳範本檔案選擇檔案

  4. 在本機電腦上打開文字編輯器,然後新增其中一個下列範本。儲存檔案,並返回主控台將其選為範本檔案。

    範例 JSON
    { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Create a VPC and a VPC Peering connection using the PeerRole to accept.", "Parameters": { "PeerVPCAccountId": { "Type": "String" }, "PeerVPCId": { "Type": "String" }, "PeerRoleArn": { "Type": "String" } }, "Resources": { "vpc": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock": "10.2.0.0/16", "EnableDnsSupport": false, "EnableDnsHostnames": false, "InstanceTenancy": "default" } }, "vpcPeeringConnection": { "Type": "AWS::EC2::VPCPeeringConnection", "Properties": { "VpcId": { "Ref": "vpc" }, "PeerVpcId": { "Ref": "PeerVPCId" }, "PeerOwnerId": { "Ref": "PeerVPCAccountId" }, "PeerRoleArn": { "Ref": "PeerRoleArn" } } } }, "Outputs": { "VPCId": { "Value": { "Ref": "vpc" } }, "VPCPeeringConnectionId": { "Value": { "Ref": "vpcPeeringConnection" } } } }
    範例 YAML
    AWSTemplateFormatVersion: 2010-09-09 Description: Create a VPC and a VPC Peering connection using the PeerRole to accept. Parameters: PeerVPCAccountId: Type: String PeerVPCId: Type: String PeerRoleArn: Type: String Resources: vpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.2.0.0/16 EnableDnsSupport: false EnableDnsHostnames: false InstanceTenancy: default vpcPeeringConnection: Type: AWS::EC2::VPCPeeringConnection Properties: VpcId: !Ref vpc PeerVpcId: !Ref PeerVPCId PeerOwnerId: !Ref PeerVPCAccountId PeerRoleArn: !Ref PeerRoleArn Outputs: VPCId: Value: !Ref vpc VPCPeeringConnectionId: Value: !Ref vpcPeeringConnection
  5. 選擇下一步

  6. 命名堆疊 (例如 VPC-peering-connection)。

  7. 接受預設值,然後選擇 Next (下一步)

  8. 選取我知道 CloudFormation 可能會建立 IAM 資源,然後選擇建立堆疊

建立具有高度限制政策的範本

在將您的 VPC 與另一個 AWS 帳戶 對等時,您可能需要建立高度限制的政策。

下列範例範本說明如何變更 VPC 對等擁有者範本 (在上述步驟 1 中建立的接受者帳戶),使其限制更嚴格。

範例 JSON
{ "AWSTemplateFormatVersion":"2010-09-09", "Description":"Create a VPC and an assumable role for cross account VPC peering.", "Parameters":{ "PeerRequesterAccountId":{ "Type":"String" } }, "Resources":{ "peerRole":{ "Type":"AWS::IAM::Role", "Properties":{ "AssumeRolePolicyDocument":{ "Statement":[ { "Action":[ "sts:AssumeRole" ], "Effect":"Allow", "Principal":{ "AWS":{ "Ref":"PeerRequesterAccountId" } } } ] }, "Path":"/", "Policies":[ { "PolicyDocument":{ "Statement":[ { "Action":"ec2:acceptVpcPeeringConnection", "Effect":"Allow", "Resource":{ "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}" } }, { "Action":"ec2:acceptVpcPeeringConnection", "Condition":{ "StringEquals":{ "ec2:AccepterVpc":{ "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}" } } }, "Effect":"Allow", "Resource":{ "Fn::Sub":"arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc-peering-connection/*" } } ], "Version":"2012-10-17" }, "PolicyName":"root" } ] } }, "vpc":{ "Type":"AWS::EC2::VPC", "Properties":{ "CidrBlock":"10.1.0.0/16", "EnableDnsHostnames":false, "EnableDnsSupport":false, "InstanceTenancy":"default" } } }, "Outputs":{ "RoleARN":{ "Value":{ "Fn::GetAtt":[ "peerRole", "Arn" ] } }, "VPCId":{ "Value":{ "Ref":"vpc" } } } }
範例 YAML
AWSTemplateFormatVersion: 2010-09-09 Description: Create a VPC and an assumable role for cross account VPC peering. Parameters: PeerRequesterAccountId: Type: String Resources: peerRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: - 'sts:AssumeRole' Effect: Allow Principal: AWS: Ref: PeerRequesterAccountId Path: / Policies: - PolicyDocument: Statement: - Action: 'ec2:acceptVpcPeeringConnection' Effect: Allow Resource: 'Fn::Sub': 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}' - Action: 'ec2:acceptVpcPeeringConnection' Condition: StringEquals: 'ec2:AccepterVpc': 'Fn::Sub': 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${vpc}' Effect: Allow Resource: 'Fn::Sub': >- arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc-peering-connection/* Version: 2012-10-17 PolicyName: root vpc: Type: AWS::EC2::VPC Properties: CidrBlock: 10.1.0.0/16 EnableDnsHostnames: false EnableDnsSupport: false InstanceTenancy: default Outputs: RoleARN: Value: 'Fn::GetAtt': - peerRole - Arn VPCId: Value: Ref: vpc

若要存取 VPC,您可以使用上述步驟 2 中使用的請求者範本。

如需詳細資訊,請參閱《Amazon VPC 對等互連指南》中的適用於 VPC 對等互連的身分與存取管理