演练:与另一个 Amazon Web Services 账户 账户中的 VPC 对等 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

演练:与另一个 Amazon Web Services 账户 账户中的 VPC 对等

可以通过使用 AWS::EC2::VPCPeeringConnection,与另一个 Amazon Web Services 账户 中的 Virtual Private Cloud(VPC)进行对等连接。这会在两个 VPC 之间创建网络连接,使您能够在它们之间路由流量,它们可以像在同一网络中那样进行通信。VPC 对等连接有助于简化数据访问和数据传输。

要建立 VPC 对等连接,您需要为单个 Amazon CloudFormation 堆栈中的两个单独的 Amazon Web Services 账户 授权。

有关 VPC 对等及其限制的更多信息,请参阅 Amazon VPC Peering Guide 中的 VPC 对等概述

先决条件

  1. 您需要为对等连接提供对等 VPC ID、对等 Amazon Web Services 账户 ID 以及跨账户访问角色

    注意

    本演练涉及两个账户:第一个是允许跨账户对等的账户 (接受方账户)。第二个是请求对等连接的账户 (请求者账户)。

  2. 要接受 VPC 对等连接,您必须能够担任跨账户访问角色。该资源的行为方式与同一账户中的 VPC 对等连接资源相同。

步骤 1:创建 VPC 和跨账户角色

创建 VPC 和跨账户访问角色 (示例)

在该步骤中,您需要在接受方账户 中创建 VPC 和角色。

  1. 在 Amazon Web Services Management Console中,选择 Amazon CloudFormation

  2. 选择创建堆栈

  3. 您有多种选择。要使用 Amazon CloudFormation Designer 创建新的空白模板,请选择 Create template in Designer (在 Designer 中创建模板)

    如果您要在另一个文本编辑器中创建模板,请选择 Template is ready (模板已就绪),然后视情况选择 Amazon S3 URLUpload a template file (上传模板文件)

  4. 使用以下示例模板创建 VPC 和跨账户角色,以允许其他账户实现对等。

    例 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 字段中输入请求者账户 的 Amazon Web Services 账户 ID。

  7. 接受默认值,然后选择 Next

  8. 依次选择 I acknowledge that Amazon CloudFormation might create IAM resources (我确认, Amazon CloudFormation 可能创建 IAM 资源)Create stack (创建堆栈)

步骤 2:创建一个模板,其中包括 AWS::EC2::VPCPeeringConnection

现在,您已创建 VPC 和跨账户角色,您可以与使用另一个 Amazon Web Services 账户(请求者账户)的 VPC 进行对等连接。

创建包含 AWS::EC2::VPCPeeringConnection 资源的模板(示例)

  1. 返回 Amazon CloudFormation 控制台主页。

  2. 选择创建堆栈

  3. 选择 Create template in Designer (在 Designer 中创建模板),以使用 Amazon CloudFormation Designer 创建新的空白模板。

    如果您要在另一个文本编辑器中创建模板,请选择 Template is ready (模板已就绪),然后视情况选择 Amazon S3 URLUpload a template file (上传模板文件)

  4. 使用以下示例模板及在步骤 1 中创建的对等角色创建 VPC 和 VPC 对等连接。

    例 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. 依次选择 I acknowledge that Amazon CloudFormation might create IAM resources (我确认, Amazon CloudFormation 可能创建 IAM 资源)Create stack (创建堆栈)

创建具有高度限制策略的模板

在将您的 VPC 与另一个 Amazon Web Services 账户 进行对等连接时,您可能需要创建一个具有高度限制性的策略。

以下示例模板显示如何更改 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": { "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" } ] }, "Type": "AWS::IAM::Role" }, "vpc": { "Properties": { "CidrBlock": "10.1.0.0/16", "EnableDnsHostnames": false, "EnableDnsSupport": false, "InstanceTenancy": "default" }, "Type": "AWS::EC2::VPC" } }, "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: 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 Type: 'AWS::IAM::Role' vpc: Properties: CidrBlock: 10.1.0.0/16 EnableDnsHostnames: false EnableDnsSupport: false InstanceTenancy: default Type: 'AWS::EC2::VPC' Outputs: RoleARN: Value: 'Fn::GetAtt': - peerRole - Arn VPCId: Value: Ref: vpc

要访问 VPC,您可以使用上面步骤 2 中使用的请求者模板。