本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您可以使用 與其他虛擬私有雲端 (VPC) AWS 帳戶 對等AWS::EC2::VPCPeeringConnection。這會在兩個 VPC 之間建立網路連線,讓您能在兩者之間路由流量,使其如同在相同的網路中通訊。VPC 對等互連有助於促進資料存取和資料傳輸。
若要建立 VPC 對等互連,您需要在單一 CloudFormation 堆疊中授權兩個單獨的 AWS 帳戶 。
如需 VPC 對等互連及其限制的詳細資訊,請參閱 Amazon VPC 對等互連指南。
先決條件
-
您需要對等 VPC ID、對等 AWS 帳戶 ID 和跨帳戶存取角色以進行對等連線。
注意
本演練涉及兩個帳戶:第一個是允許跨帳戶對等的帳戶 (「接受者帳戶」)。第二個是請求對等連線的帳戶 (「請求者帳戶」)。
-
若要接受 VPC 對等互連,您必須擔任跨帳戶存取角色。該資源的行為方式與同一帳戶中的 VPC 對等互連資源相同。如需有關 IAM 管理員如何授予許可以擔任跨帳戶角色的資訊,請參閱《IAM 使用者指南》中的授予使用者許可以切換角色。
步驟 1:建立 VPC 和跨帳戶角色
在此步驟中,您需要在「接受者帳戶」 中建立 VPC 和角色。
建立 VPC 和跨帳戶存取角色
登入 AWS Management Console ,並在 https://https://console.aws.amazon.com/cloudformation
開啟 AWS CloudFormation 主控台。 -
從堆疊頁面,選擇右上角的建立堆疊,然後選擇使用新資源 (標準)。
-
針對先決條件 - 準備範本,選擇選擇現有範本,然後上傳範本檔案、選擇檔案。
-
在本機電腦上開啟文字編輯器,並新增下列其中一個範本。儲存檔案並返回 主控台,將其選取為範本檔案。
範例 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
-
選擇 Next (下一步)。
-
為堆疊命名 (例如
VPC-owner
),然後在 PeerRequesterAccountId 欄位中輸入請求者帳戶的 AWS 帳戶 ID。 -
接受預設值,然後選擇 Next (下一步)。
-
選擇我確認 AWS CloudFormation 可能會建立 IAM 資源,然後選擇建立堆疊。
步驟 2:建立包含 AWS::EC2::VPCPeeringConnection
的範本
現在您已建立 VPC 和跨帳戶角色,您可以使用另一個 AWS 帳戶 (請求者帳戶) 與 VPC 對等。
建立包含 AWS::EC2::VPCPeeringConnection 資源的範本
-
返回 AWS CloudFormation 主控台首頁。
-
從堆疊頁面,選擇右上角的建立堆疊,然後選擇使用新資源 (標準)。
-
針對先決條件 - 準備範本,選擇選擇現有範本,然後上傳範本檔案、選擇檔案。
-
在本機電腦上開啟文字編輯器,並新增下列其中一個範本。儲存檔案並返回 主控台,將其選取為範本檔案。
範例 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
-
選擇 Next (下一步)。
-
命名堆疊 (例如
VPC-peering-connection
)。 -
接受預設值,然後選擇 Next (下一步)。
-
選擇我確認 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 對等互連的身分和存取管理。