

# 別の AWS アカウントで VPC とピア接続する
<a name="peer-with-vpc-in-another-account"></a>

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

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

VPC ピアリングとその制限については、「[Amazon VPC ピアリングガイド](https://docs.aws.amazon.com/vpc/latest/peering/)」を参照してください。

## 前提条件
<a name="peer-with-vpc-in-another-account-prerequisites"></a>

1. ピアリング接続には、ピア VPC ID、ピア AWS アカウント ID、および[クロスアカウントアクセス](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_common-scenarios_aws-accounts.html)が必要です。
**注記**  
このチュートリアルでは、2 つのアカウントについて説明します。1 つめは、クロスアカウントのピア接続を許可するアカウント (*アクセプタアカウント*) です。2 つめは、ピア接続をリクエストするアカウント (*リクエスタアカウント*) です。

1. VPC ピア接続を受け入れるには、クロスアカウントアクセスロールを自分で引き受ける必要があります。リソースは、同じアカウントで VPC ピア接続リソースと同じように動作します。IAM 管理者がクロスアカウントロールの継承を許可する方法については、「*IAM ユーザーガイド*」の「[ロールを切り替えるアクセス許可をユーザーに付与する](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_permissions-to-switch.html)」を参照してください。

## ステップ 1: VPC とクロスアカウントロールの作成
<a name="step-1-create-vpc-and-cross-account-role"></a>

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

**VPC とクロスアカウントアクセスロールを作成するには**

1. AWS マネジメントコンソール にサインインし、CloudFormation コンソール ([https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/)) を開きます。

1. **[スタック]** ページでは、右上の **[スタックの作成]** を選択してから、**[新しいリソースを使用 (標準)]** を選択します。

1. **[前提条件 - テンプレートの準備]** で、**[既存のテンプレートを選択]** を選択し、**テンプレートファイルをアップロードし**、**ファイルを選択します**。

1. ローカルマシンでテキストエディタを開き、次のいずれかのテンプレートを追加します。ファイルを保存し、コンソールに戻って、テンプレートファイルとして選択します。  
**Example 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"
           ]
         }
       }
     }
   }
   ```  
**Example 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
   ```

1. [**次へ**] を選択します。

1. スタックに名前 (**VPC-owner** など) を付け、**PeerRequesterAccountId** フィールドに*リクエスタアカウント*の AWS アカウント ID を入力します。

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

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

## ステップ 2: `AWS::EC2::VPCPeeringConnection` を含むテンプレートを作成する
<a name="step-2-create-template-for-vpc-peering-connection-owner"></a>

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

**[AWS::EC2::VPCPeeringConnection](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-vpcpeeringconnection.html) リソースを含むテンプレートを作成するには**

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

1. **[スタック]** ページでは、右上の **[スタックの作成]** を選択してから、**[新しいリソースを使用 (標準)]** を選択します。

1. **[前提条件 - テンプレートの準備]** で、**[既存のテンプレートを選択]** を選択し、**テンプレートファイルをアップロードし**、**ファイルを選択します**。

1. ローカルマシンでテキストエディタを開き、次のいずれかのテンプレートを追加します。ファイルを保存し、コンソールに戻って、テンプレートファイルとして選択します。  
**Example 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"
         }
       }
     }
   }
   ```  
**Example 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
   ```

1. [**次へ**] を選択します。

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

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

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

## 制限がより厳格なポリシーでテンプレートを作成する
<a name="create-template-with-highly-restrictive-policy"></a>

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

次の例では、VPC ピアの所有者のテンプレート (上記のステップ 1 で作成した*アクセプタアカウント*) を変更し、より制限を厳しくする方法を示しています。

**Example 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"
      }
    }
  }
}
```

**Example 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 ピアリングの Identity and Access Management](https://docs.aws.amazon.com/vpc/latest/peering/security-iam.html)」を参照してください。