選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

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

焦點模式
在另一個 VPC 中的對等 AWS 帳戶 - AWS CloudFormation

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

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

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

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

如需 VPC 對等互連及其限制的詳細資訊,請參閱 Amazon VPC 對等互連指南

先決條件

  1. 您需要對等 VPC ID、對等 AWS 帳戶 ID 和跨帳戶存取角色以進行對等連線。

    注意

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

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

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

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

建立 VPC 和跨帳戶存取角色
  1. 登入 AWS Management Console ,並在 https://https://console.aws.amazon.com/cloudformation 開啟 AWS 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. 選擇 Next (下一步)

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

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

  8. 選擇我確認 AWS CloudFormation 可能會建立 IAM 資源,然後選擇建立堆疊

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

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

建立包含 AWS::EC2::VPCPeeringConnection 資源的範本
  1. 返回 AWS 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. 選擇 Next (下一步)

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

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

  8. 選擇我確認 AWS 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 對等互連的身分和存取管理

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。