概要
別アカウントのVPCとVPCピアリングを行う際のCFnの書き方と、ロールの設定方法について。
なお同一アカウント内でのVPCピアリングだとロール無しでスルッと行ける。
やり方
ざっくり
- 前提としてピアリングを行うVPCは各アカウントで用意してあること
- VPCPeeringを受け入れる(accepter)アカウントでクロスアカウント許可用ロールを用意
- VPCPeeringをリクエストする(requester)アカウントでVPCPeeringリクエストを送信
- 完了
ロールの用意
テンプレート
acceptrole.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Accept VPC Peering Role
Parameters:
AcceptAccountId:
Type: String
Resources:
AcceptVpcPeeringRole:
Type: AWS::IAM::Role
Properties:
RoleName: 'AcceptVpcPeeringRole'
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: 'sts:AssumeRole'
Principal:
# ここで複数アカウントを指定することも可能
AWS: !Join
- ''
- - 'arn:aws:iam::'
- !Ref AcceptAccountId
- ':root'
Policies:
- PolicyName: 'AcceptVpcPeering'
PolicyDocument:
Statement:
- Effect: Allow
Action: 'ec2:AcceptVpcPeeringConnection'
# 適宜許可を行いたいVPCを設定
Resource: '*'
実行コマンド
$ aws cloudformation create-stack \
--stack-name accept-vpcpeering-role \
--template-body file://acceptvpcpeeringrole.yaml \
--parameters ParameterKey=AcceptAccountId,ParameterValue=000000 \
--capabilities CAPABILITY_NAMED_IAM \
--profile=accepter # profileは適宜
VPCPeeringの作成
テンプレート
vpcpeering.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: VPC Peering
Parameters:
VpcId:
Type: String
Default: 'vpc-xxxxxxxxx'
PeerOwnerId:
Type: String
Default: '000000000000000'
PeerVpcId:
Type: String
Default: 'vpc-xxxxxxxxx'
PeerRoleArn:
Type: String
Default: 'arn:aws:iam::000000000000000:role/AcceptVpcPeeringRole'
Resources:
VpcPeering:
Type: "AWS::EC2::VPCPeeringConnection"
Properties:
VpcId: !Ref VpcId
PeerOwnerId: !Ref PeerOwnerId
PeerVpcId: !Ref PeerVpcId
PeerRoleArn: !Ref PeerRoleArn
Tags:
- Key: Name
Value: 'TestPeering'
コマンド
$ aws cloudformation create-stack \
--stack-name vpcpeering \
--template-body file://vpcpeering.yaml \
--profile=requester # profileは適宜