チュートリアル: 別の AWS アカウント アカウントで VPC とピア接続する - AWS CloudFormation

チュートリアル: 別の AWS アカウント アカウントで VPC とピア接続する

AWS::EC2::VPCPeeringConnection を使用して、別の AWS アカウント の仮想プライベートクラウド (VPC) とピア接続できます。これにより、2 つの VPC 間でトラフィックをルーティングできるネットワーク接続が作成され、同じネットワーク内に存在しているかのように通信できます。VPC ピア接続により、データアクセスとデータ転送が容易になります。

VPC ピアリング接続を確立するには、単一の AWS CloudFormation スタック内で 2 つの別個の AWS アカウント を認証する必要があります。

VPC ピアリングとその制限の詳細については、「Amazon VPC ピアリング機能ガイド」の「VPC peering overview」(VPC ピアリング機能の概要) を参照してください。

前提条件

  1. ピアリング接続には、ピア VPC ID、ピア AWS アカウント ID、およびクロスアカウントアクセスが必要です。

    注記

    このチュートリアルでは、2 つのアカウントについて説明します。1 つめは、クロスアカウントのピア接続を許可するアカウント (アクセプタアカウント) です。2 つめは、ピア接続をリクエストするアカウント (リクエスタアカウント) です。

  2. VPC ピア接続を受け入れるには、クロスアカウントアクセスロールを自分で引き受ける必要があります。リソースは、同じアカウントで VPC ピア接続リソースと同じように動作します。

ステップ 1: VPC とクロスアカウントロールの作成

VPC とクロスアカウントアクセスロールを作成する (例)

このステップでは、アクセプタアカウントに VPC とロールを作成します。

  1. [AWS Management Console] で、[AWS CloudFormation] を選択します。

  2. [スタックの作成] を選択します。

  3. これには複数の方法があります。AWS CloudFormation デザイナー を使用して新しい空のテンプレートを作成するには、[Create template in Designer] (デザイナーでテンプレートを作成) を選択します。

    別のテキストエディタでテンプレートを作成する場合は、[Template is ready (テンプレートの準備ができています)] を選択し、次に [Amazon S3 URL] または [テンプレートファイルをアップロード] を必要に応じて選択します。

  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 フィールドにリクエスタアカウントの AWS アカウント ID を入力します。

  7. デフォルトを受け入れて [次へ] を選択します。

  8. [AWS CloudFormation によって IAM リソースが作成される場合があることを承認します] を選択した後、[スタックを作成] を選択します。

ステップ 2: AWS::EC2::VPCPeeringConnection を含むテンプレートを作成する

VPC とクロスアカウントロールが作成できたので、別の AWS アカウント (リクエスタアカウント) を使用して VPC とピア接続できます。

AWS::EC2::VPCPeeringConnection リソース (例) を含むテンプレートを作成するには

  1. AWS CloudFormation コンソールのホームページに戻ります。

  2. [スタックの作成] を選択します。

  3. AWS CloudFormation デザイナーを使用して新しい空のテンプレートを作成するには、[Create template in Designer] (デザイナーでテンプレートを作成する) を選択します。

    別のテキストエディタでテンプレートを作成する場合は、[Template is ready (テンプレートの準備ができています)] を選択し、次に [Amazon S3 URL] または [テンプレートファイルをアップロード] を必要に応じて選択します。

  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. [Next] を選択します。

  6. スタックに名前 (VPC-peering-connection など) を付けます。

  7. デフォルトを受け入れて [次へ] を選択します。

  8. [AWS CloudFormation によって IAM リソースが作成される場合があることを承認します] を選択した後、[スタックを作成] を選択します。

制限の厳しいポリシーでテンプレートを作成する

別の AWS アカウント を使用した VPC ピア接続で、制限の厳しいポリシーを作成できます。

次の例では、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 と同じリクエスタテンプレートを使用できます。