Transit Gatewayを利用して、別アカウントのVPCと接続してみる


この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

TransitGatewayは複数のVPCを接続するときや、オンプレミスネットワーク、DirectConnectゲートウェイを接続する際に活躍するサービスです。

 

TransitGatewayは同一アカウント内のVPCだけしか繋げないわけではなく、他のAWSアカウントにあるVPCとの接続が可能なんですよね。プロジェクトによってはADサーバーは別アカウントで管理したい、なんてことがあるかもしれません。

 

今回の構築では、別のアカウントにあるVPCとの接続を行っていきます。

 

【この記事はこんな方におすすめ】

  • Transit Gatewayについてさらっと知りたい方
  • Transit Gatewayのハンズオンをやってみたい方

 

※記事タイトル通り、アカウント2つ利用します。

 

 

Transit Gatewayで、他アカウントのVPCと接続をしてみる

他アカウントとのVPC間接続ハンズオン

saitou-handson-transitgateway-other構築図

 

前提条件
今回の構築は、前回行った「Transit Gatewayを利用して、VPC間を接続してみる」の環境を利用しますので、ぜひ「Transit Gatewayを利用して、VPC間を接続してみる」を一読ください。

 

 

構築図
saitou-handson-transitgateway-other構築図2

 

構築図にあります“今回はココ”と書かれた部分を構築し、アカウント2からアカウント1にあるEC2インスタンスに疎通確認を行います。

 

 

今回使用するAWSサービス

 

  • Amazon CloudFormation
  • AWS Transit Gateway
  • キーペア
  • AWS Resource Access Manager

 

 

注意
今回の構築はアカウントを2つ利用します。各作業のはじめにどのアカウントで作業するか記載しますが、わかりにくい部分がありましたら申し訳ありません。

 

それでは構築していきます。

 

 

■キーペア作成

アカウント2の作業です。

 

まずはキーペアを作成します。設定値は以下の通りです。

 

項目 設定値
名前 saitou-handson-TGW-keypairs(任意)
キーペアのタイプ RSA
プライベートキーファイル形式 .pem

 

 

EC2コンソール画面の左ペインから「キーペア」をクリックし、「キーペアを作成」を押します。

 

saitou-handson-transitgateway-other-keypair

saitou-handson-transitgateway-other-keypair

 

↓設定値の入力が完了したら、「キーペアを作成」を押してください。

 

saitou-handson-transitgateway-other-keypair

 

 

■CloudFormationテンプレート起動

アカウント2の作業です。

 

CloudFationテンプレートを使用して、アカウント2に環境を作成します。設定値は以下の通りです。

 

項目 設定値
テンプレートの準備 テンプレートの準備完了
テンプレートソース テンプレートファイルのアップロード
スタックの名前 saitou-TGW-test-stack2(任意)

 

 

CloudFormationコンソール画面の左ペインから「スタック」をクリックし、「スタックの作成」を押してください。

 

saitou-handson-transitgateway-other-CloudFormation

 

↓テンプレートファイルはこちらを利用してください。テンプレートファイルのアップロードが完了したら、「次へ」をクリックします。

 

※ファイル開けない向けに、テンプレートファイルの中身を載せておきます。

 

AWSTemplateFormatVersion: 2010-09-09
Description: TGW hands-on lab1 template

## aws cloudformation create-stack --template-body file://static/CFn/wstgw-lab1.tempalte --capabilities CAPABILITY_NAMED_IAM --stack-name tgw-hands-on-lab1

Parameters:
  wstgwBoundaryVpcCidr:
    Type: String
    Default: 192.168.0.0/16
  wstgwPrivateVpc1Cidr:
    Type: String
    Default: 10.0.0.0/16
  wstgwKeyName:
    Type: AWS::EC2::KeyPair::KeyName
    Default: keypair1
  wstgwEc2Ami:
    Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
  wstgwEc2InstanceType:
    Type: String
    Default : t2.micro

Resources:
  ## Create IAM
  wstgwEc2IAMRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement: 
          - Effect: "Allow"
            Principal: 
              Service: 
                - "ec2.amazonaws.com"
            Action: 
              - "sts:AssumeRole"
      ManagedPolicyArns:
      - "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"

  wstgwEc2InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles:
      - !Ref wstgwEc2IAMRole

  ## BoundaryVPC
  #### create VPC
  wstgwBoundaryVpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Sub ${wstgwBoundaryVpcCidr}
      InstanceTenancy: default
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: wstgwBoundaryVpc

  #### create subnet
  wstgwBoundaryVpcNatSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [ 0, !Cidr [ !GetAtt wstgwBoundaryVpc.CidrBlock, 11, 8 ]]
      VpcId: !Ref wstgwBoundaryVpc
      AvailabilityZone: !Select [ 0, !GetAZs ]
      Tags:
        - Key: Name
          Value: wstgwBoundaryVpcNatSubnet

  wstgwBoundaryVpcPrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [ 1, !Cidr [ !GetAtt wstgwBoundaryVpc.CidrBlock, 11, 8 ]]
      VpcId: !Ref wstgwBoundaryVpc
      AvailabilityZone: !Select [ 0, !GetAZs ]
      Tags:
        - Key: Name
          Value: wstgwBoundaryVpcPrivateSubnet

  wstgwBoundaryVpcTgwSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [ 10, !Cidr [ !GetAtt wstgwBoundaryVpc.CidrBlock, 11, 8 ]]
      VpcId: !Ref wstgwBoundaryVpc
      AvailabilityZone: !Select [ 0, !GetAZs ]
      Tags:
        - Key: Name
          Value: wstgwBoundaryVpcTgwSubnet

  #### create IGW
  wstgwIgw:
    Type: AWS::EC2::InternetGateway
    Properties: 
      Tags:
        - Key: Name
          Value: wstgwIgw

  wstgwIgwAttach:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties: 
      InternetGatewayId: !Ref wstgwIgw
      VpcId: !Ref wstgwBoundaryVpc

  #### Create NATGW
  wstgwNatEIP:
    Type: AWS::EC2::EIP
    Properties:
      Domain: vpc

  wstgwNatGW:
    Type: AWS::EC2::NatGateway
    Properties: 
      SubnetId: !Ref wstgwBoundaryVpcNatSubnet
      AllocationId: !GetAtt wstgwNatEIP.AllocationId
      Tags:
        - Key: Name
          Value: NatGW

  #### Route Table
  wstgwRtbBoundaryVpcNatSubnet:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref wstgwBoundaryVpc
      Tags:
        - Key: Name
          Value: wstgwRtbBoundaryVpcNatSubnet

  wstgwRtbBoundaryVpcPrivateSubnet:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref wstgwBoundaryVpc
      Tags:
        - Key: Name
          Value: wstgwRtbBoundaryVpcPrivateSubnet

  wstgwRtbBoundaryVpcTgwSubnet:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref wstgwBoundaryVpc
      Tags:
        - Key: Name
          Value: wstgwRtbBoundaryVpcTgwSubnet

  #### Create route
  ###### Create route & Associsate NatSubnet
  wstgwRouteBoundaryVpcNatSubnetDefault:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbBoundaryVpcNatSubnet
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref wstgwIgw

  wstgwRouteBoundaryVpcNatSubnetToTgw:
    DependsOn: wstgwTgwVpcAttachmentBoundaryVpc
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbBoundaryVpcNatSubnet
      DestinationCidrBlock: 10.0.0.0/8
      TransitGatewayId: !Ref wstgwTgw

  wstgwRtbAssoBoundaryVpcNatSubnet:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref wstgwBoundaryVpcNatSubnet
      RouteTableId: !Ref wstgwRtbBoundaryVpcNatSubnet

  ###### Create route & Associsate PrivateSubnet
  wstgwRouteBoundaryVpcPrivateSubnetToNat:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbBoundaryVpcPrivateSubnet
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref wstgwNatGW

  wstgwRouteBoundaryVpcPrivateSubnetToTgw:
    DependsOn: wstgwTgwVpcAttachmentBoundaryVpc
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbBoundaryVpcPrivateSubnet
      DestinationCidrBlock: 10.0.0.0/8
      TransitGatewayId: !Ref wstgwTgw

  wstgwRtbAssoBoundaryVpcPrivateSubnet:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref wstgwBoundaryVpcPrivateSubnet
      RouteTableId: !Ref wstgwRtbBoundaryVpcPrivateSubnet

  ###### Create route & Associsate TgwSubnet
  wstgwRouteBoundaryVpcTgwSubnetToNat:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbBoundaryVpcTgwSubnet
      DestinationCidrBlock: 0.0.0.0/0
      NatGatewayId: !Ref wstgwNatGW

  wstgwRtbAssoBoundaryVpcTgwSubnet:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref wstgwBoundaryVpcTgwSubnet
      RouteTableId: !Ref wstgwRtbBoundaryVpcTgwSubnet

  #### Create Endpoint
  wstgwSGSsmEndPoint0:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: wstgwSgSsmEndPoint0
      GroupDescription: "for SSM Endpoint"
      VpcId: !Ref wstgwBoundaryVpc
      SecurityGroupIngress:
        - Description: "Allow SSM Endpoint from Internal"
          IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIp: !Sub ${wstgwBoundaryVpcCidr}
      Tags:
        - Key: Name
          Value: wstgwSGSsmEndPoint0

  wstgwSsmEndpoint0:
    DependsOn: wstgwEc2Instance192x168x1x100
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssm"
      VpcEndpointType: Interface
      PrivateDnsEnabled: true
      VpcId: !Ref wstgwBoundaryVpc
      SubnetIds:
        - !Ref wstgwBoundaryVpcPrivateSubnet
      SecurityGroupIds:
        - !Ref wstgwSGSsmEndPoint0

  wstgwSsmMessagesEndpoint0:
    DependsOn: wstgwEc2Instance192x168x1x100
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssmmessages"
      VpcEndpointType: Interface
      PrivateDnsEnabled: true
      VpcId: !Ref wstgwBoundaryVpc
      SubnetIds:
        - !Ref wstgwBoundaryVpcPrivateSubnet
      SecurityGroupIds:
        - !Ref wstgwSGSsmEndPoint0

  #### Create Instace
  wstgwSGEc2Instance0:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: wstgwSGEc2Instance0 
      GroupDescription: "for EC2 instance"
      VpcId: !Ref wstgwBoundaryVpc
      SecurityGroupIngress:
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: "10.0.0.0/8"
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: "192.168.0.0/16"
      Tags:
        - Key: Name
          Value: wstgwSGEc2Instance0

  wstgwEc2Instance192x168x1x100: 
    Type: AWS::EC2::Instance
    Properties: 
      ImageId: !Ref wstgwEc2Ami
      KeyName: !Ref wstgwKeyName
      InstanceType: !Ref wstgwEc2InstanceType
      IamInstanceProfile: !Ref wstgwEc2InstanceProfile
      NetworkInterfaces: 
        - DeviceIndex: "0"
          SubnetId: !Ref wstgwBoundaryVpcPrivateSubnet
          GroupSet:
            - !Ref wstgwSGEc2Instance0
          PrivateIpAddresses:
            - PrivateIpAddress: "192.168.1.100"
              Primary: true
      Tags:
          - Key: Name
            Value: wstgwEc2Instance192-168-1-100

  ## Create PrivateVpc1
  #### Create VPC
  wstgwPrivateVpc1:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Sub ${wstgwPrivateVpc1Cidr}
      InstanceTenancy: default
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: wstgwPrivateVpc1

  #### create Subnet
  wstgwPrivateVpc1PrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [ 1, !Cidr [ !GetAtt wstgwPrivateVpc1.CidrBlock, 11, 8 ]]
      VpcId: !Ref wstgwPrivateVpc1
      AvailabilityZone: !Select [ 0, !GetAZs ]
      Tags:
        - Key: Name
          Value: wstgwPrivateVpc1PrivateSubnet

  wstgwPrivateVpc1TgwSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: !Select [ 10, !Cidr [ !GetAtt wstgwPrivateVpc1.CidrBlock, 11, 8 ]]
      VpcId: !Ref wstgwPrivateVpc1
      AvailabilityZone: !Select [ 0, !GetAZs ]
      Tags:
        - Key: Name
          Value: wstgwPrivateVpc1TgwSubnet

  #### Route Table
  wstgwRtbPrivateVpc1PrivateSubnet:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref wstgwPrivateVpc1
      Tags:
        - Key: Name
          Value: wstgwRtbPrivateVpc1PrivateSubnet

  #### Create route
  ###### Create route & Associsate PrivateSubnet
  wstgwRoutePrivateVpc1PrivateSubnetToTgw:
    DependsOn: wstgwTgwVpcAttachmentPrivateVpc1
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref wstgwRtbPrivateVpc1PrivateSubnet
      DestinationCidrBlock: 0.0.0.0/0
      TransitGatewayId: !Ref wstgwTgw

  wstgwRtbAssoPrivateVpc1PrivateSubnet:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref wstgwPrivateVpc1PrivateSubnet
      RouteTableId: !Ref wstgwRtbPrivateVpc1PrivateSubnet

  #### Create Endpoint
  wstgwSGSsmEndPoint1:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: wstgwSgSsmEndPoint1
      GroupDescription: "for SSM Endpoint"
      VpcId: !Ref wstgwPrivateVpc1
      SecurityGroupIngress:
        - Description: "Allow SSM Endpoint from Internal"
          IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIp: !Sub ${wstgwPrivateVpc1Cidr}
      Tags:
        - Key: Name
          Value: wstgwSGSsmEndPoint1

  wstgwSsmEndpoint1:
    DependsOn: wstgwEc2Instance10x0x1x100
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssm"
      VpcEndpointType: Interface
      PrivateDnsEnabled: true
      VpcId: !Ref wstgwPrivateVpc1
      SubnetIds:
        - !Ref wstgwPrivateVpc1PrivateSubnet
      SecurityGroupIds:
        - !Ref wstgwSGSsmEndPoint1

  wstgwSsmMessagesEndpoint1:
    DependsOn: wstgwEc2Instance10x0x1x100
    Type: AWS::EC2::VPCEndpoint
    Properties:
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.ssmmessages"
      VpcEndpointType: Interface
      PrivateDnsEnabled: true
      VpcId: !Ref wstgwPrivateVpc1
      SubnetIds:
        - !Ref wstgwPrivateVpc1PrivateSubnet
      SecurityGroupIds:
        - !Ref wstgwSGSsmEndPoint1

  #### Create Instace
  wstgwSGEc2Instance1:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: wstgwSGEc2Instance1 
      GroupDescription: "for EC2 instance"
      VpcId: !Ref wstgwPrivateVpc1
      SecurityGroupIngress:
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: "10.0.0.0/8"
        - IpProtocol: icmp
          FromPort: -1
          ToPort: -1
          CidrIp: "192.168.0.0/16"
      Tags:
        - Key: Name
          Value: wstgwSGEc2Instance1

  wstgwEc2Instance10x0x1x100: 
    Type: AWS::EC2::Instance
    Properties: 
      ImageId: !Ref wstgwEc2Ami
      KeyName: !Ref wstgwKeyName
      InstanceType: !Ref wstgwEc2InstanceType
      IamInstanceProfile: !Ref wstgwEc2InstanceProfile
      NetworkInterfaces: 
        - DeviceIndex: "0"
          SubnetId: !Ref wstgwPrivateVpc1PrivateSubnet
          GroupSet:
            - !Ref wstgwSGEc2Instance1
          PrivateIpAddresses:
            - PrivateIpAddress: "10.0.1.100"
              Primary: true
      Tags:
          - Key: Name
            Value: wstgwEc2Instance10-0-1-100

  ## create TransitGW
  wstgwTgw:
    Type: AWS::EC2::TransitGateway
    Properties:
      AmazonSideAsn: 65000
      AutoAcceptSharedAttachments: enable
      DefaultRouteTableAssociation: enable
      DefaultRouteTablePropagation: enable
      Description: !Join ['-', [wstgwTgw, !Ref 'AWS::Region']]
      DnsSupport: enable
      VpnEcmpSupport: enable
      MulticastSupport: disable
      Tags: 
        - Key: Name
          Value: wstgwTgw

  #### Attachment VPC TransitGW
  wstgwTgwVpcAttachmentBoundaryVpc:
    Type: AWS::EC2::TransitGatewayAttachment
    Properties:
      SubnetIds: 
        - !Ref wstgwBoundaryVpcTgwSubnet
      TransitGatewayId: !Ref wstgwTgw
      VpcId: !Ref wstgwBoundaryVpc
      Tags:
        - Key: Name
          Value: wstgwTgwVpcAttachmentBoundaryVpc

  wstgwTgwVpcAttachmentPrivateVpc1:
    Type: AWS::EC2::TransitGatewayAttachment
    Properties:
      SubnetIds: 
        - !Ref wstgwPrivateVpc1TgwSubnet
      TransitGatewayId: !Ref wstgwTgw
      VpcId: !Ref wstgwPrivateVpc1
      Tags:
        - Key: Name
          Value: wstgwTgwVpcAttachmentPrivateVpc1

Outputs:
  wstgwTgw:
    Description: Tgw
    Value: !Ref wstgwTgw
    Export:
      Name: wstgwTgw

  wstgwBoundaryVpc:
    Description: BoundaryVpc
    Value: !Ref wstgwBoundaryVpc
    Export:
      Name: wstgwBoundaryVpc

  wstgwPrivateVpc1:
    Description: PrivateVpc1
    Value: !Ref wstgwPrivateVpc1
    Export:
      Name: wstgwPrivateVpc1

  wstgwEc2Instance192x168x1x100:
    Description: wstgwEc2Instance192-168-1-100
    Value: !Ref wstgwEc2Instance192x168x1x100
    Export:
      Name: wstgwEc2Instance192-168-1-100

  wstgwEc2Instance10x0x1x100:
    Description: wstgwEc2Instance10-0-1-100
    Value: !Ref wstgwEc2Instance10x0x1x100
    Export:
      Name: wstgwEc2Instance10-0-1-100

 

 

saitou-handson-transitgateway-other-CloudFormation

 

↓スタックの名前を入力しましたら、パラメータの“wstgwKeyName”の項目で、最初に作成したキーペアを選択してください。選択が完了しましたら、「次へ」をクリックします。

 

saitou-handson-transitgateway-other-CloudFormation

 

↓“スタックオプションの設定”は何も変更せず、「次へ」をクリックしてください。

 

saitou-handson-transitgateway-other-CloudFormation

saitou-handson-transitgateway-other-CloudFormation

 

↓ステップ4のレビューでは、ページ一番下にあります「AWS CloudFormationによって IAMリソースが作成される場合があることを承認します。」のチェックボックスにチェックを入れてください。

 

saitou-handson-transitgateway-other-CloudFormation

 

↓設定項目に問題なければ、「送信」を押しましょう。

 

saitou-handson-transitgateway-other-CloudFormation

 

 

スタックのステータスが“CREATE_COMPLETE”になれば、環境構築の完了です。

 

 

次の作業はアカウント1で行うのですが、アカウント2のアカウントIDが必要になります。メモ帳などにコピーしておきましょう。

 

saitou-handson-transitgateway-other-account

 

 

■TransitGatewayリソース共有

ここからはアカウント1の作業です。

 

アカウント間でVPCを接続する際に、AWS ResourceAccessManagerのリソース共有を利用します。TrasitGatewayを作成したアカウント1から、アカウント2に対してリソースの共有を追加申請します。

 

 

VPCコンソール画面の左ペインからTransitGatewayをクリックします。

 

saitou-handson-transitgateway-other-RAM

 

↓前回作成したTransitGatewayの“wstgwTgw”を選択し、「共有タブ」から「TransitGatewayを共有」を押してください。

 

saitou-handson-transitgateway-other-RAM

 

↓次に「リソース共有を作成」をクリックします。

 

saitou-handson-transitgateway-other-RAM

 

↓設定値は以下の通りです。

 

項目 設定値
名前 saitou-wstgw-RAM(任意)
リソースタイプ トランジットゲートウェイ
選択されたリソース 名前がwstgwTgwのもの
外部アカウントの許可
アカウントID アカウント2のID

 

 

saitou-handson-transitgateway-other-RAM

 

↓リソースタイプ選択で「トランジットゲートウェイ」を選択してください。

 

saitou-handson-transitgateway-other-RAM

 

↓プリンシパルの項目で、“外部アカウントの許可”にチェックを入れ、メモしておいたアカウント2のIDを検索窓に入れ、該当のAWSアカウントをクリックし、「追加」を押します。入力が完了しましたら、「リソースの共有の作成」をクリックしてください。

 

saitou-handson-transitgateway-other-RAM

 

↓リソースの共有が作成されました。

 

saitou-handson-transitgateway-other-RAM

 

 

しかし、リソース共有が作成されただけで、実際にはまだリソースを共有できていません。アカウントIDを知っているだけで一方的にリソースを共有出来たら、とても危険ですよね。リソースの共有がなされるためには、もう一方のアカウントの承認が必要です。

 

■TransitGatewayリソース共有の承認

ここからはアカウント2の作業です。

 

アカウント2側でリソース共有の承認作業を行っていきます。

 

 

AWSサービス検索画面で、「Resource Access Manager」と検索し、
クリックしてください。

 

saitou-handson-transitgateway-other-RAM

 

↓左ペインから「リソースの共有」を押します。すでに“1招待”という表示がありますね。

 

saitou-handson-transitgateway-other-RAM

 

↓承認する前に念のため概要画面で申請してきたアカウントに関して確認しましょう。

 

saitou-handson-transitgateway-other-RAM

 

↓問題なければ、「リソースの共有を承認」をクリックし、「OK」を押してください。

 

saitou-handson-transitgateway-other-RAM

saitou-handson-transitgateway-other-RAM

 

 

アカウント2側でリソースの共有を承認したことで、アカウント1で作成したTransitGatewayが共有されました。

 

■TransitGatewayアタッチメント作成

アカウント2の作業です。

 

アカウント2で作成したVPCをトランジットゲートウェイに関連付けていきましょう。

 

 

VPCコンソール画面の左ペインから「TransitGateway」をクリックし、まずはリソースの共有がされているか確認します。

 

saitou-handson-transitgateway-other-attachment

 

TransitGatewayが共有されていますね。それではアタッチメントを作成します。設定値は以下の通りです。

 

項目 設定値
名前 wstgwTgwVpcAttachmentPrivateVpc2(任意)
TransitgatewayID wstgwTgw-と名がついたもの
DNSサポート
IPv6サポート
VPCID wstgwPrivateVpc2
サブネット wstgwPrivateVpc2TgwSubnet

 

 

左ペインから「TransitGatewayアタッチメント」をクリックしてください。

 

saitou-handson-transitgateway-other-attachment

 

↓続いて「Transitgatewayアタッチメントを作成」を押します。

 

saitou-handson-transitgateway-other-attachment

 

↓アタッチメントの名前を入力し、TransitgatewayIDを選択します。

 

saitou-handson-transitgateway-other-attachment

 

↓TransitGatewayに関連付けるVPC(wstgwTgw-xxxx)を選択し、アベイラビリティゾーンにチェックを入れ、「wstgwPrivateVpc2TgwSubnet」を選択しましょう。

 

saitou-handson-transitgateway-other-attachment

 

↓入力が完了しましたら、「TransitGatewayアタッチメントを作成」をクリックしてください。

 

saitou-handson-transitgateway-other-attachment

 

↓状態が“available”になりましたら、作成完了です。

 

saitou-handson-transitgateway-other-attachment

 

TransitGatewayアタッチメントの作成は以上です。

 

 

■VPCルートテーブル編集

アカウント2の作業です。

 

アタッチメントの作成が完了しましたら、関連付けやTransitGatewayルートテーブルは自動的に編集されます。しかし、サブネットに紐づいているルートテーブルに関してはそのままです。アカウント2のPrivateSubnetに紐づいたルートテーブルを編集しましょう。

 

 

VPCコンソール画面の左ペインから、「ルートテーブル」を押し、「wstgwRtbPrivateVpc2PrivateSubnet」を選択してください。

 

saitou-handson-transitgateway-other-routetable

 

↓「ルートタブ」から「ルートを編集」をクリックします。

 

saitou-handson-transitgateway-other-routetable

↓以下の送信先を追加してください。

 

送信先 ターゲット
0.0.0.0/0 "tgw-"と付くもの

 

 

saitou-handson-transitgateway-other-routetable

saitou-handson-transitgateway-other-routetable

 

↓追加できましたら、「変更を保存」を押します。

 

saitou-handson-transitgateway-other-routetable

saitou-handson-transitgateway-other-routetable

 

 

今回の構築は以上です。

 

 

■疎通確認

アカウント2の作業です。

 

アカウント2のVPCにありますEC2インスタンスから各VPCとインターネットに疎通確認していきます。

 

EC2コンソール画面からインスタンス「wstgwEc2Instance10-1-1-100」を選択し、「接続」をクリックしてください。

 

saitou-handson-transitgateway-other-communication

 

↓「セッションマネージャー」タブから「接続」を押します。

 

saitou-handson-transitgateway-other-communication

 

以下のコマンドを押します。

bash -I

 

saitou-handson-transitgateway-other-communication

 

 

【疎通確認1】
saitou-handson-transitgateway-other-communication

 

それでは疎通確認していきます。まずはインターネット向きです。以下のコマンドを打ちます。

ping amazon.co.jp

 

saitou-handson-transitgateway-other-communication

 

応答が返ってきました。

 

 

【疎通確認2】
saitou-handson-transitgateway-other-communication

 

次にアカウント1のPrivateVPC1にあるEC2インスタンス(10.0.1.100)に疎通確認してみます。以下のコマンドを打ちます。

ping 10.0.1.100

 

saitou-handson-transitgateway-other-communication

 

応答が返ってきました。

 

 

【疎通確認3】
saitou-handson-transitgateway-other-communication

 

さいごはBoundary VPCにありますEC2インスタンス(192.168.1.100)に疎通確認してみましょう。以下のコマンドを打ちます。

ping 192.168.1.100

 

saitou-handson-transitgateway-other-communication

 

応答が返ってきました。

 

 

今回のハンズオンは以上です。

 

 

まとめ:Transit Gatewayを利用して、別アカウントのVPCと接続してみる

Transit GatewayはVPCとVPCを簡単につなげることができ、ピアリング接続とは違い、数千のVPC、VPN、DirectConnectGatewayとの接続が可能です。

 

TransitGatewayとリソース共有を利用すれば、アカウントを分けてAWS運用ができます。用途ごとにアカウントを分けたいと考えている方はぜひ利用してみてください。

 

今回のハンズオンではCloudFormationを利用して環境構築したので、Transit Gatewayの詳細な設定はしていません。ネクストステップとしてさらに理解を深めたい方は、今回の構築図をもとに一から作成し疎通確認することをおすすめします

 

 

参考リンク:AWS公式ドキュメントAWS Transit Gateway ハンズオン
 

 

↓ほかの協栄情報メンバーもAWS Transit Gatewayやネットワークに関する記事を公開しています。ぜひ参考にしてみてください。

 

■VPC同士を VPC Peering する構築ハンズオン(INAMURA)
https://cloud5.jp/peering-vpc-to-each-other/

 

■DirectConnect GWとTransit GWを利用して国に跨るグローバルネットワークを構築(juwei)
https://cloud5.jp/directconnect-transitgw/

 

Last modified: 2023-02-20

Author