

# 다른 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 계정의 Virtual Private Cloud(VPC)와 피어링할 수 있습니다. 이렇게 하면 두 VPC 간에 네트워킹 연결이 생성되어 두 VPC 간에 트래픽을 라우팅할 수 있습니다. 따라서 두 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)이 필요합니다.
**참고**  
이 연습에서는 두 가지 계정을 참조합니다. 첫 번째는 교차 계정 피어링을 허용하는 계정이고(*수락자 계정*), 두 번째는 피어링 연결을 요청하는 계정입니다(*요청자 계정*).

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 Management Console에 로그인하여 [https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/)에서 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>

VPC를 다른 AWS 계정 계정에 피어링할 때 매우 제한적인 정책을 생성하고자 할 수 있습니다.

다음 예제 템플릿은 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 피어링에 대한 자격 증명 및 액세스 관리](https://docs.aws.amazon.com/vpc/latest/peering/security-iam.html)를 참조하세요.