LoginSignup
2
2

More than 5 years have passed since last update.

CloudFormationでクロスアカウントなVPCPeeringを構築する

Last updated at Posted at 2017-10-18

概要

別アカウントのVPCとVPCピアリングを行う際のCFnの書き方と、ロールの設定方法について。
なお同一アカウント内でのVPCピアリングだとロール無しでスルッと行ける。

やり方

ざっくり

  1. 前提としてピアリングを行うVPCは各アカウントで用意してあること
  2. VPCPeeringを受け入れる(accepter)アカウントでクロスアカウント許可用ロールを用意
  3. VPCPeeringをリクエストする(requester)アカウントでVPCPeeringリクエストを送信
  4. 完了

ロールの用意

テンプレート

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は適宜
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2