{VPC}VPC ピア接続を操作する

https://docs.aws.amazon.com/ja_jp/vpc/latest/peering/working-with-vpc-peering.html
https://docs.aws.amazon.com/ja_jp/Route53/latest/DeveloperGuide/hosted-zones-private.html
https://dev.classmethod.jp/articles/the-same-aws-account-other-vpc-route53-private-hostzone/

 

前提: 


VPC名: vpc01
サブネット: 10.0.0.0/24,10.0.1.0/24
プライベートホストゾーン名: zone20220709-aws.local


VPC名: vpc21
サブネット: 10.2.0.0/24,10.2.1.0/24
プライベートホストゾーン名: zone20220709-aws.local

 

-- 1. 事前準備

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール


sudo yum -y install jq

 

-- 2. VPC、サブネット作成[vpc01]


-- 2.1 VPC(10.0.0.0/16)を作成する

aws ec2 create-vpc \
--cidr-block 10.0.0.0/16 \
--instance-tenancy default \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=vpc01}]' 


aws ec2 describe-vpcs
aws ec2 describe-vpcs | jq -r '.Vpcs.VpcId'


-- 2.2 サブネット(10.0.0.0/24,10.0.0.1/24)を作成する


aws ec2 create-subnet \
--availability-zone ap-northeast-1a \
--cidr-block 10.0.0.0/24 \
--vpc-id vpc-11111111111111111 \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=subnet01}]' 

aws ec2 create-subnet \
--availability-zone ap-northeast-1c \
--cidr-block 10.0.1.0/24 \
--vpc-id vpc-11111111111111111 \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=subnet02}]' 


aws ec2 describe-subnets
aws ec2 describe-subnets | jq -r '.Subnets.SubnetId'


-- 2.3 インターネットゲートウェイを作成する


aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=igw01}]' 


aws ec2 describe-internet-gateways
aws ec2 describe-internet-gateways | jq -r '.InternetGateways.InternetGatewayId'


-- 2.4 インターネットゲートウェイVPCにアタッチする
aws ec2 attach-internet-gateway \
--internet-gateway-id igw-22222222222222222 \
--vpc-id vpc-11111111111111111


-- 2.5 ルートテーブルのデフォルトゲートウェイとしてインターネットゲートウエイを指定する

aws ec2 describe-route-tables
aws ec2 describe-route-tables | jq -r '.RouteTables.RouteTableId'

aws ec2 create-route \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-22222222222222222 \
--route-table-id rtb-33333333333333333

 


-- 2.6 インスタンス作成(FW設定含む)

aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance01}]' \
--subnet-id subnet-44444444444444444 \
--associate-public-ip-address


0.0.0.0/0からのSSH許可追加

aws ec2 authorize-security-group-ingress \
--group-id sg-55555555555555555 \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0 \
--tag-specifications 'ResourceType=security-group-rule,Tags=[{Key=Name,Value=fw01}]' 

 

10.2.0.0/24からのアクセス許可


aws ec2 authorize-security-group-ingress \
--group-id sg-55555555555555555 \
--protocol all \
--port all \
--cidr 10.2.0.0/24 \
--tag-specifications 'ResourceType=security-group-rule,Tags=[{Key=Name,Value=fw02}]' 

 

-- 2.7 プライベートホストゾーンの作成

aws route53 create-hosted-zone \
--name zone20220709-aws.local \
--vpc VPCRegion=ap-northeast-1,VPCId=vpc-11111111111111111 \
--caller-reference 2022-08-16-23:09

aws route53 list-hosted-zones


-- 2.8 EC2インスタンスのプライベートIPアドレスをAレコードで登録する


vim a.json

{
  "Comment": "CREATE/DELETE/UPSERT a record ",
  "Changes": [
    { "Action": "UPSERT",
      "ResourceRecordSet": { "Name": "instance01.zone20220709-aws.local.",
                             "Type": "A",
                             "TTL": 300,
                             "ResourceRecords": [
                                 { "Value": "10.0.0.201" }
                             ]
                           }
    }
  ]
}


aws route53 change-resource-record-sets \
--hosted-zone-id Z66666666666666666666 \
--change-batch file://a.json

 

aws route53 list-resource-record-sets \
--hosted-zone-id Z66666666666666666666

 

-- 2.9 enableDnsHostnames および enableDnsSupport  を true に設定


aws ec2 describe-vpc-attribute \
--attribute enableDnsSupport \
--vpc-id vpc-11111111111111111

aws ec2 describe-vpc-attribute \
--attribute enableDnsHostnames \
--vpc-id vpc-11111111111111111

aws ec2 modify-vpc-attribute \
--enable-dns-hostnames \
--vpc-id vpc-11111111111111111

aws ec2 modify-vpc-attribute \
--enable-dns-support \
--vpc-id vpc-11111111111111111

立ち上げたEC2インスタンスで確認
nslookup instance01.zone20220709-aws.local

反映まで時間がかかることがある

 

 


-- 3. VPCピアリング用VPC、サブネット作成[vpc21]

-- 3.1 VPC(10.2.0.0/16)を作成する

aws ec2 create-vpc \
--cidr-block 10.2.0.0/16 \
--instance-tenancy default \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=vpc21}]' 


aws ec2 describe-vpcs
aws ec2 describe-vpcs | jq -r '.Vpcs.VpcId'


-- 3.2 サブネット(10.2.0.0/24,10.2.0.1/24)を作成する


aws ec2 create-subnet \
--availability-zone ap-northeast-1a \
--cidr-block 10.2.0.0/24 \
--vpc-id vpc-77777777777777777 \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=subnet21}]' 

aws ec2 create-subnet \
--availability-zone ap-northeast-1c \
--cidr-block 10.2.1.0/24 \
--vpc-id vpc-77777777777777777 \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=subnet22}]' 


aws ec2 describe-subnets
aws ec2 describe-subnets | jq -r '.Subnets.SubnetId'


-- 3.3 インターネットゲートウェイを作成する


aws ec2 create-internet-gateway \
--tag-specifications 'ResourceType=internet-gateway,Tags=[{Key=Name,Value=igw21}]' 


aws ec2 describe-internet-gateways
aws ec2 describe-internet-gateways | jq -r '.InternetGateways.InternetGatewayId'


-- 3.4 インターネットゲートウェイVPCにアタッチする
aws ec2 attach-internet-gateway \
--internet-gateway-id igw-88888888888888888 \
--vpc-id vpc-77777777777777777


-- 3.5 ルートテーブルのデフォルトゲートウェイとしてインターネットゲートウエイを指定する

aws ec2 describe-route-tables
aws ec2 describe-route-tables | jq -r '.RouteTables.RouteTableId'

aws ec2 create-route \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id igw-88888888888888888 \
--route-table-id rtb-99999999999999999

 


-- 3.6 インスタンス作成(FW設定含む)

aws ec2 run-instances \
--image-id ami-0404778e217f54308 \
--instance-type t3.nano \
--key-name key1 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=instance21}]' \
--subnet-id subnet-00000000000000000 \
--associate-public-ip-address

 


0.0.0.0/0からのSSH許可追加

aws ec2 authorize-security-group-ingress \
--group-id sg-aaaaaaaaaaaaaaaaa \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0 \
--tag-specifications 'ResourceType=security-group-rule,Tags=[{Key=Name,Value=fw21}]' 

 

10.0.0.0/24からのアクセス許可


aws ec2 authorize-security-group-ingress \
--group-id sg-aaaaaaaaaaaaaaaaa \
--protocol all \
--port all \
--cidr 10.0.0.0/24 \
--tag-specifications 'ResourceType=security-group-rule,Tags=[{Key=Name,Value=fw22}]' 

 

-- 3.7 vpc01と同じプライベートホストゾーンにvpc21を追加


aws route53 associate-vpc-with-hosted-zone \
--hosted-zone-id Z66666666666666666666 \
--vpc VPCRegion=ap-northeast-1,VPCId=vpc-77777777777777777


aws route53 list-hosted-zones

 

 


-- 3.8 EC2インスタンスのプライベートIPアドレスをAレコードで登録する


vim a.json

{
  "Comment": "CREATE/DELETE/UPSERT a record ",
  "Changes": [
    { "Action": "UPSERT",
      "ResourceRecordSet": { "Name": "instance21.zone20220709-aws.local.",
                             "Type": "A",
                             "TTL": 300,
                             "ResourceRecords": [
                                 { "Value": "10.2.0.75" }
                             ]
                           }
    }
  ]
}


aws route53 change-resource-record-sets \
--hosted-zone-id Z66666666666666666666 \
--change-batch file://a.json

 

aws route53 list-resource-record-sets \
--hosted-zone-id Z66666666666666666666

 

-- 3.9 enableDnsHostnames および enableDnsSupport  を true に設定


aws ec2 describe-vpc-attribute \
--attribute enableDnsSupport \
--vpc-id vpc-77777777777777777

aws ec2 describe-vpc-attribute \
--attribute enableDnsHostnames \
--vpc-id vpc-77777777777777777

aws ec2 modify-vpc-attribute \
--enable-dns-hostnames \
--vpc-id vpc-77777777777777777

aws ec2 modify-vpc-attribute \
--enable-dns-support \
--vpc-id vpc-77777777777777777

立ち上げたEC2インスタンスで確認
nslookup instance21.zone20220709-aws.local

反映まで時間がかかることがある

 


-- 4. VPCピアリング作成

aws ec2 create-vpc-peering-connection \
--peer-vpc-id vpc-77777777777777777 \
--vpc-id vpc-11111111111111111 \
--tag-specifications 'ResourceType=vpc-peering-connection,Tags=[{Key=Name,Value=pcx01}]' 

aws ec2 describe-vpc-peering-connections

aws ec2 accept-vpc-peering-connection \
--vpc-peering-connection-id pcx-bbbbbbbbbbbbbbbbb

 


-- 5. VPCピアリング間のルート追加

aws ec2 create-route \
--destination-cidr-block 10.2.0.0/16 \
--gateway-id pcx-bbbbbbbbbbbbbbbbb \
--route-table-id rtb-33333333333333333

aws ec2 create-route \
--destination-cidr-block 10.0.0.0/16 \
--gateway-id pcx-bbbbbbbbbbbbbbbbb \
--route-table-id rtb-99999999999999999

 

 

 

-- 6. pingで疎通確認
下記2パターンで確認

AWSVPCピアリング間 vpc01 -> vpc21

ping 10.2.0.75

AWSVPCピアリング間 vpc01 <- vpc21

ping 10.0.0.201


-- 7. 名前解決確認
下記2パターンで確認

AWSVPCピアリング間 vpc01 -> vpc21

nslookup instance21.zone20220709-aws.local 10.0.0.2

AWSVPCピアリング間 vpc01 <- vpc21

nslookup instance01.zone20220709-aws.local 10.2.0.2

 

-- 8. クリーンアップ[vpc21]

-- VPCピアリング削除

aws ec2 delete-vpc-peering-connection \
--vpc-peering-connection-id pcx-bbbbbbbbbbbbbbbbb

aws ec2 describe-vpc-peering-connections

 

 

-- Aレコードの削除


vim a.json

{
  "Comment": "CREATE/DELETE/UPSERT a record ",
  "Changes": [
    { "Action": "DELETE",
      "ResourceRecordSet": { "Name": "instance21.zone20220709-aws.local.",
                             "Type": "A",
                             "TTL": 300,
                             "ResourceRecords": [
                                 { "Value": "10.2.0.75" }
                             ]
                           }
    }
  ]
}


aws route53 change-resource-record-sets \
--hosted-zone-id Z66666666666666666666 \
--change-batch file://a.json

 

aws route53 list-resource-record-sets \
--hosted-zone-id Z66666666666666666666

 

-- インスタンス削除

aws ec2 describe-instances

aws ec2 terminate-instances \
--instance-ids i-ccccccccccccccccc

 

 

-- インターネットゲートウェイ削除(VPCからデタッチしてから)

aws ec2 describe-internet-gateways
aws ec2 describe-internet-gateways | jq -r '.InternetGateways.InternetGatewayId'

aws ec2 detach-internet-gateway \
--internet-gateway-id igw-88888888888888888 \
--vpc-id vpc-77777777777777777

aws ec2 delete-internet-gateway \
--internet-gateway-id igw-88888888888888888


-- サブネット削除


aws ec2 describe-subnets
aws ec2 describe-subnets | jq -r '.Subnets.SubnetId'

aws ec2  delete-subnet \
--subnet-id subnet-00000000000000000


aws ec2  delete-subnet \
--subnet-id subnet-ddddddddddddddddd

 

-- VPC削除

aws ec2  delete-vpc \
--vpc-id vpc-77777777777777777

aws ec2 describe-vpcs
aws ec2 describe-vpcs | jq -r '.Vpcs.VpcId'

 

-- 9. クリーンアップ[vpc01]


-- Aレコードの削除

 


vim a.json

{
  "Comment": "CREATE/DELETE/UPSERT a record ",
  "Changes": [
    { "Action": "DELETE",
      "ResourceRecordSet": { "Name": "instance01.zone20220709-aws.local.",
                             "Type": "A",
                             "TTL": 300,
                             "ResourceRecords": [
                                 { "Value": "10.0.0.201" }
                             ]
                           }
    }
  ]
}


aws route53 change-resource-record-sets \
--hosted-zone-id Z66666666666666666666 \
--change-batch file://a.json

 

aws route53 list-resource-record-sets \
--hosted-zone-id Z66666666666666666666


-- プライベートホストゾーンの削除

aws route53 list-hosted-zones

aws route53 delete-hosted-zone \
--id Z66666666666666666666

 

-- インスタンス削除

aws ec2 describe-instances

aws ec2 terminate-instances \
--instance-ids i-eeeeeeeeeeeeeeeee

 

 

-- インターネットゲートウェイ削除(VPCからデタッチしてから)

aws ec2 describe-internet-gateways
aws ec2 describe-internet-gateways | jq -r '.InternetGateways.InternetGatewayId'

aws ec2 detach-internet-gateway \
--internet-gateway-id igw-22222222222222222 \
--vpc-id vpc-11111111111111111

aws ec2 delete-internet-gateway \
--internet-gateway-id igw-22222222222222222


-- サブネット削除


aws ec2 describe-subnets
aws ec2 describe-subnets | jq -r '.Subnets.SubnetId'

aws ec2  delete-subnet \
--subnet-id subnet-44444444444444444


aws ec2  delete-subnet \
--subnet-id subnet-fffffffffffffffff

 

-- VPC削除

aws ec2  delete-vpc \
--vpc-id vpc-11111111111111111

aws ec2 describe-vpcs
aws ec2 describe-vpcs | jq -r '.Vpcs.VpcId'