マルチクラウドでVPC同士接続してみるその①(AWSとGCP編)
マルチクラウドでネットワーク構成してみて強固なバックエンドを作れないかとゆうのを5年くらい前から夢見てて、ようやく手出しができそうになってきたのでお勉強しようとしてみた。最初にAWSのVPCとGCPのVPCをつないでEC2とGCEで通信できるようにしてみた。
ホントはクラウド間通信のサービスで組むんだろうけど個人の勉強なのでSite to SiteVPNとClassicVPNという構成からスタートする。
貧乏なのでどっちも最小構成的な感じからスタートしてみて構成に慣れて短時間で構築できる様になったらGoogle推奨のHA構成で組んでみようかなと思ってます。
まぁいろんなとこで出尽くしている感じがあるので、いまさらですがヨワヨワなのでご容赦ください。構成はこんな感じです。
マネジメントコンソールでやるのも面白くないのでどちらもCloudShell使ってやってみることにした。(単に環境準備するのが面倒なだけ)
◆シリーズ
1.AWSとGCP接続
2.AWSとAzure接続
3.GCPとAzure接続
◆大まかな流れ
1.AWSでVPCとサブネット作成
2.GCPでVPCとサブネット作成
3.GCPでVPNGateway作成
4.AWSで仮想プライベートゲートウェイ作成
5.AWSでGCPとのVPN接続作成
6.GCPでVPNトンネル作成
7.AWSでプライベートサブネットにEC2作成
8.GCPでGCEをサブネットに作成
9.GCP側の後始末
10.AWS側の後始末
◆コマンドライン(先頭にプロンプト付きなので注意)
1.AWSでVPCとサブネット作成
1.1.VPC作成
$ REGION=ap-northeast-1 $ VPC_NAME=multi-cloud-vpn-aws $ VPC_CIDR=172.20.0.0/16 $ STRING_EC2_VPC_TAG="ResourceType=vpc,Tags=[{Key=Name,Value=${VPC_NAME}}]" \ && echo ${STRING_EC2_VPC_TAG} $ aws ec2 create-vpc \ --cidr-block ${VPC_CIDR} \ --tag-specifications ${STRING_EC2_VPC_TAG}
1.2.サブネット作成
$ AZ_CODE="a" $ SUBNET_TYPE="private" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_CIDR=172.20.10.0/24 $ AZ_NAME=${REGION}${AZ_CODE} \ && echo ${AZ_NAME} $ STRING_EC2_SUBNET_TAG="ResourceType=subnet,Tags=[{Key=Name,Value=${SUBNET_NAME}}]" \ && echo ${STRING_EC2_SUBNET_TAG} $ VPC_ID=$( \ aws ec2 describe-vpcs \ --filters Name=tag:Name,Values=${VPC_NAME} \ --query 'Vpcs[].VpcId' \ --output text \ ) \ && echo ${VPC_ID} $ aws ec2 create-subnet \ --vpc-id ${VPC_ID} \ --cidr-block ${SUBNET_CIDR} \ --availability-zone ${AZ_NAME} \ --tag-specifications ${STRING_EC2_SUBNET_TAG} $ AZ_CODE="c" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_CIDR=172.20.20.0/24 $ AZ_NAME=${REGION}${AZ_CODE} \ && echo ${AZ_NAME} $ STRING_EC2_SUBNET_TAG="ResourceType=subnet,Tags=[{Key=Name,Value=${SUBNET_NAME}}]" \ && echo ${STRING_EC2_SUBNET_TAG} $ aws ec2 create-subnet \ --vpc-id ${VPC_ID} \ --cidr-block ${SUBNET_CIDR} \ --availability-zone ${AZ_NAME} \ --tag-specifications ${STRING_EC2_SUBNET_TAG}
1.3.セキュリティグループ作成
$ SECURITY_GROUP_NAME=${VPC_NAME}"-sg" \ && echo ${SECURITY_GROUP_NAME} $ STRING_SG_TAG="ResourceType=security-group,Tags=[{Key=Name,Value=${SECURITY_GROUP_NAME}}]" \ && echo ${STRING_SG_TAG} $ SECURITY_GROUP_DESCRIPTION='multi-cloud-vpn' $ aws ec2 create-security-group \ --group-name ${SECURITY_GROUP_NAME} \ --description "${SECURITY_GROUP_DESCRIPTION}" \ --vpc-id ${VPC_ID} \ --tag-specifications ${STRING_SG_TAG}
$ VPC_COMMENT=ingress_internal $ ACCEPT_CIDR=172.30.0.0/16 #GCPのサブネットIPアドレスレンジ $ ACCEPT_INGRESS_COMMENT=ingress_vpn # $ ACCEPT_EGRESS_COMMENT=egress_vpn $ SECURITY_GROUP_ID=$( \ aws ec2 describe-security-groups \ --filter Name=vpc-id,Values=${VPC_ID} \ Name=group-name,Values=${SECURITY_GROUP_NAME} \ --query 'SecurityGroups[].GroupId' \ --output text \ ) \ && echo ${SECURITY_GROUP_ID} $ aws ec2 authorize-security-group-ingress \ --group-id ${SECURITY_GROUP_ID} \ --ip-permissions IpProtocol=-1,IpRanges="[{CidrIp=${VPC_CIDR},Description=\"${VPC_COMMENT}\"}]" $ aws ec2 authorize-security-group-ingress \ --group-id ${SECURITY_GROUP_ID} \ --ip-permissions IpProtocol=-1,IpRanges="[{CidrIp=${ACCEPT_CIDR},Description=\"${ACCEPT_INGRESS_COMMENT}\"}]"
1.4.ルートテーブルへのアタッチ
$ ROUTE_TABLE_ID=$( \
aws ec2 describe-route-tables \
--filters Name=vpc-id,Values=${VPC_ID} \
--query "RouteTables[].RouteTableId" \
--output text \
) \
&& echo ${ROUTE_TABLE_ID}
$ ROUTE_TABLE_NAME="rt-"${VPC_NAME} \
&& echo ${ROUTE_TABLE_NAME}
$ aws ec2 create-tags \
--resources ${ROUTE_TABLE_ID} \
--tags '[{"Key":"Name","Value":"'${ROUTE_TABLE_NAME}'"}]'
$ AZ_CODE="a"
$ SUBNET_TYPE="private"
$ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \
&& echo ${SUBNET_NAME}
$ SUBNET_ID=$( \
aws ec2 describe-subnets \
--filters Name=vpc-id,Values=${VPC_ID} \
Name=tag:Name,Values=${SUBNET_NAME} \
--query "Subnets[].SubnetId" \
--output text \
) \
&& echo ${SUBNET_ID}
$ aws ec2 associate-route-table \
--subnet-id ${SUBNET_ID} \
--route-table-id ${ROUTE_TABLE_ID}
$ AZ_CODE="c"
$ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \
&& echo ${SUBNET_NAME}
$ SUBNET_ID=$( \
aws ec2 describe-subnets \
--filters Name=vpc-id,Values=${VPC_ID} \
Name=tag:Name,Values=${SUBNET_NAME} \
--query "Subnets[].SubnetId" \
--output text \
) \
&& echo ${SUBNET_ID}
$ aws ec2 associate-route-table \
--subnet-id ${SUBNET_ID} \
--route-table-id ${ROUTE_TABLE_ID}
2.GCPでVPCとサブネット作成
2.1.VPC作成
$ VPC_NETWORK=multi-cloud-vpn-gcp $ PROJECT=multicloud-sitevpn-project $ REGION=asia-northeast1 $ gcloud compute networks create ${VPC_NETWORK} \ --project=${PROJECT} \ --subnet-mode=custom
2.2.サブネット作成
$ SUBNET_NAME=${VPC_NETWORK}"-subnet" \ && echo ${SUBNET_NAME} $ PRIMARY_RANGE=172.30.0.0/16 $ gcloud compute networks subnets create ${SUBNET_NAME} \ --network=${VPC_NETWORK} \ --range=${PRIMARY_RANGE} \ --project=${PROJECT} \ --region=${REGION}
2.3.ファイアウォールルールの追加
$ FIREWALL_RULE=${VPC_NETWORK}"-ssh-allow-rule" \ && echo ${FIREWALL_RULE} $ RULE_DESCRIPTION="AllowSSH" $ DIRECTION=ingress $ PRIORITY=1000 $ ACTION=allow $ RULES=tcp:22 $ SOURCE_RANGE=35.235.240.0/20 #Googleからのアクセス $ gcloud compute firewall-rules create ${FIREWALL_RULE} \ --project=${PROJECT} \ --network=${VPC_NETWORK} \ --description=${RULE_DESCRIPTION} \ --direction=${DIRECTION} \ --action=${ACTION} \ --priority=${PRIORITY} \ --rules=${RULES} \ --source-ranges=${SOURCE_RANGE} \ --destination-ranges=${PRIMARY_RANGE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-ingress-gcp-allow-rule" \ && echo ${FIREWALL_RULE} $ RULE_DESCRIPTION="Allowinternal" $ PRIORITY=1001 $ RULES=all $ SOURCE_RANGE=${PRIMARY_RANGE} #GCPサブネット内のIPレンジ $ gcloud compute firewall-rules create ${FIREWALL_RULE} \ --project=${PROJECT} \ --network=${VPC_NETWORK} \ --description=${RULE_DESCRIPTION} \ --direction=${DIRECTION} \ --action=${ACTION} \ --priority=${PRIORITY} \ --rules=${RULES} \ --source-ranges=${SOURCE_RANGE} \ --destination-ranges=${PRIMARY_RANGE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-ingress-aws-allow-rule" \ && echo ${FIREWALL_RULE} $ RULE_DESCRIPTION="AllowVPNinternal" $ PRIORITY=1001 $ RULES=all $ SOURCE_RANGE=172.20.0.0/16 #AWSのIPレンジ $ gcloud compute firewall-rules create ${FIREWALL_RULE} \ --project=${PROJECT} \ --network=${VPC_NETWORK} \ --description=${RULE_DESCRIPTION} \ --direction=${DIRECTION} \ --action=${ACTION} \ --priority=${PRIORITY} \ --rules=${RULES} \ --source-ranges=${SOURCE_RANGE} \ --destination-ranges=${PRIMARY_RANGE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-egress-allow-rule" \ && echo ${FIREWALL_RULE} $ RULE_DESCRIPTION="AllowVPNexternal" $ DIRECTION=egress $ PRIORITY=1002 $ RULES=all $ SOURCE_RANGE=${PRIMARY_RANGE} #GCPサブネット内のIPレンジ $ DESTINATION_RANGE=172.20.0.0/16 #AWSのサブネットのレンジ $ gcloud compute firewall-rules create ${FIREWALL_RULE} \ --project=${PROJECT} \ --description=${RULE_DESCRIPTION} \ --direction=${DIRECTION} \ --priority=${PRIORITY} \ --network=${VPC_NETWORK} \ --action=${ACTION} \ --rules=${RULES} \ --source-ranges=${SOURCE_RANGE} \ --destination-ranges=${DESTINATION_RANGE}
3.GCPでVPNGateway作成
$ GW_NAME=${VPC_NETWORK}"-gw" \ && echo ${GW_NAME} $ gcloud --project="${PROJECT}" compute target-vpn-gateways create "${GW_NAME}" \ --network="${VPC_NETWORK}" \ --region="${REGION}" $ GW_IP_NAME=${GW_NAME}"-ip" \ && echo ${GW_IP_NAME} $ gcloud --project="${PROJECT}" compute addresses create "${GW_IP_NAME}" --region="${REGION}" $ GW_IP_ADDRESS=$(gcloud --project="${PROJECT}" compute addresses describe "${GW_IP_NAME}" \ --region=${REGION} \ --format='flattened(address)' \ ) $ FORWORDING_RULE="fr-"${GW_NAME}"-esp" \ && echo ${FORWORDING_RULE} $ PROTPCOL=ESP $ gcloud --project="${PROJECT}" compute forwarding-rules create "${FORWORDING_RULE}" \ --load-balancing-scheme=EXTERNAL \ --network-tier=PREMIUM \ --ip-protocol="${PROTPCOL}" \ --address="${GW_IP_NAME}" \ --target-vpn-gateway="${GW_NAME}" \ --region="${REGION}" $ FORWORDING_RULE="fr-"${GW_NAME}"-udp500" \ && echo ${FORWORDING_RULE} $ PROTPCOL=UDP $ PORT_NUMBER=500 $ gcloud --project="${PROJECT}" compute forwarding-rules create "${FORWORDING_RULE}" \ --load-balancing-scheme=EXTERNAL \ --network-tier=PREMIUM \ --ip-protocol="${PROTPCOL}" \ --ports="${PORT_NUMBER}" \ --address="${GW_IP_NAME}" \ --target-vpn-gateway="${GW_NAME}" \ --region="${REGION}" $ FORWORDING_RULE="fr-"${GW_NAME}"-udp4500" \ && echo ${FORWORDING_RULE} $ PORT_NUMBER=4500 $ gcloud --project="${PROJECT}" compute forwarding-rules create "${FORWORDING_RULE}" \ --load-balancing-scheme=EXTERNAL \ --network-tier=PREMIUM \ --ip-protocol="${PROTPCOL}" \ --ports="${PORT_NUMBER}" \ --address="${GW_IP_NAME}" \ --target-vpn-gateway="${GW_NAME}" \ --region="${REGION}" $ echo ${GW_IP_ADDRESS}
4.AWSで仮想プライベートゲートウェイ作成
4.1.仮想プライベートゲートウェイ作成
$ VPN_GATEWAY_NAME=${VPC_NAME}"-vpn-gw" \ && echo ${VPN_GATEWAY_NAME} $ REGION=ap-northeast-1 $ aws ec2 create-vpn-gateway \ --tag-specifications "ResourceType=vpn-gateway,Tags=[{Key=Name,Value=${VPN_GATEWAY_NAME}}]" \ --region $REGION \ --type ipsec.1 $ VGW_ID=$( \ aws ec2 describe-vpn-gateways \ --filters "Name=tag:Name,Values=${VPN_GATEWAY_NAME}" \ --query 'VpnGateways[?State==`available`].VpnGatewayId' \ --output text \ ) \ && echo ${VGW_ID} $ aws ec2 attach-vpn-gateway --vpn-gateway-id ${VGW_ID} --vpc-id ${VPC_ID}
4.2.ルート伝播を有効化する
$ aws ec2 enable-vgw-route-propagation \ --gateway-id ${VGW_ID} \ --route-table-id ${ROUTE_TABLE_ID}
5.AWSでGCPとのVPN接続作成
5.1.CustomerGateway作成
$ CUSTOMER_GATEWAY_NAME=${VPC_NAME}"-csgw" \ && echo ${CUSTOMER_GATEWAY_NAME} $ GCP_IP_ADDRESS= #GCPで予約した外部アドレス(GW_IP_ADDRESS) $ BGP_ARN=65000 $ aws ec2 create-customer-gateway \ --tag-specifications "ResourceType=customer-gateway,Tags=[{Key=Name,Value=${CUSTOMER_GATEWAY_NAME}}]" \ --type ipsec.1 \ --public-ip ${GCP_IP_ADDRESS} \ --bgp-asn ${BGP_ARN}
5.2.VPN接続作成
$ CGW_ID=$( \ aws ec2 describe-customer-gateways \ --filters "Name=tag:Name,Values=${CUSTOMER_GATEWAY_NAME}" \ --query 'CustomerGateways[?State==`available`].CustomerGatewayId' \ --output text \ )\ && echo ${CGW_ID} $ VPN_CONNECTION_NAME=${VPC_NAME}"-con" \ && echo ${VPN_CONNECTION_NAME} $ aws ec2 create-vpn-connection \ --tag-specifications "ResourceType=vpn-connection,Tags=[{Key=Name,Value=${VPN_CONNECTION_NAME}}]" \ --type ipsec.1 \ --customer-gateway-id ${CGW_ID} \ --vpn-gateway-id ${VGW_ID} \ --options "{\"StaticRoutesOnly\":true}" #~利用可能になるまで5分ほど待つ必要あり~ $ VPN_CONID=$( \ aws ec2 describe-vpn-connections \ --filters "Name=tag:Name,Values=${VPN_CONNECTION_NAME}" \ --query 'VpnConnections[?State==`available`].VpnConnectionId' \ --output text \ ) \ && echo ${VPN_CONID} $ GCP_REMOTE_CIDR=172.30.0.0/16 #GCPのサブネットのCIDR $ aws ec2 create-vpn-connection-route \ --destination-cidr-block ${GCP_REMOTE_CIDR} \ --vpn-connection-id ${VPN_CONID}
5.3.AWSの接続情報取得
$ aws ec2 describe-vpn-connections --vpn-connection-id ${VPN_CONID} --output text | \ grep -E '^TUNNELOPTIONS' | \ sed -e 's/^TUNNELOPTIONS\s+//' | \ sed -e 's/\s+/ /g'
6.GCPでVPNトンネル作成
6.1.VPNトンネル1の設定
$ TUNNEL_NAME=${VPC_NETWORK}"-vpn-tunel1" \ && echo ${TUNNEL_NAME} $ ON_PREM_IP= #AWSのVirtual Private Gateway1のIPアドレス $ LOCAL_CIDR=172.30.0.0/16 #GCP側のサブネットCIDR $ AWS_REMOTE_CIDR=172.20.0.0/16 #AWS側のVPCのCIDR $ IKE_VERS=2 $ SHARED_SECRET= #AWSで発行したシェアードシークレット $ GW_NAME=${VPC_NETWORK}"-gw" \ && echo ${GW_NAME} $ gcloud compute vpn-tunnels create ${TUNNEL_NAME} \ --project=${PROJECT} \ --region=${REGION} \ --peer-address=${ON_PREM_IP} \ --ike-version=${IKE_VERS} \ --shared-secret=${SHARED_SECRET} \ --local-traffic-selector=${LOCAL_CIDR} \ --remote-traffic-selector=${AWS_REMOTE_CIDR} \ --target-vpn-gateway=${GW_NAME} $ ROUTE_NAME=${VPC_NETWORK}"-route1" \ && echo ${ROUTE_NAME} $ gcloud compute routes create ${ROUTE_NAME} \ --project=${PROJECT} \ --destination-range=${AWS_REMOTE_CIDR} \ --next-hop-vpn-tunnel=${TUNNEL_NAME} \ --network=${VPC_NETWORK} \ --next-hop-vpn-tunnel-region=${REGION}
6.2.VPNトンネル2の設定
$ TUNNEL_NAME=${VPC_NETWORK}"-vpn-tunel2" \ && echo ${TUNNEL_NAME} $ ON_PREM_IP= #AWSのVirtual Private Gateway2のIPアドレス $ SHARED_SECRET= #AWSで発行したシェアードシークレット $ gcloud compute vpn-tunnels create ${TUNNEL_NAME} \ --project=${PROJECT} \ --region=${REGION} \ --peer-address=${ON_PREM_IP} \ --ike-version=${IKE_VERS} \ --shared-secret=${SHARED_SECRET} \ --local-traffic-selector=${LOCAL_CIDR} \ --remote-traffic-selector=${AWS_REMOTE_CIDR} \ --target-vpn-gateway=${GW_NAME} $ ROUTE_NAME=${VPC_NETWORK}"-route2" \ && echo ${ROUTE_NAME} $ gcloud compute routes create ${ROUTE_NAME} \ --project=${PROJECT} \ --destination-range=${AWS_REMOTE_CIDR} \ --next-hop-vpn-tunnel=${TUNNEL_NAME} \ --network=${VPC_NETWORK} \ --next-hop-vpn-tunnel-region=${REGION}
7.AWSでプライベートサブネットにEC2作成
7.1.AZ-aでEC2作成
$ INSTANCE_AMI=ami-0a0b7b240264a48d7 $ INSTANCE_TYPE=t3.nano $ AZ_CODE="a" $ SUBNET_TYPE="private" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_ID=$( \ aws ec2 describe-subnets \ --filters Name=vpc-id,Values=${VPC_ID} \ Name=tag:Name,Values=${SUBNET_NAME} \ --query "Subnets[].SubnetId" \ --output text \ ) \ && echo ${SUBNET_ID} $ INSTANCE_NAME=multicloud_aws_instance $ KEY_PAIR_NAME=multicloud_test #キーはあらかじめ作成しておく $ aws ec2 run-instances \ --image-id $INSTANCE_AMI --count 1 \ --instance-type t3.nano --key-name $KEY_PAIR_NAME \ --subnet-id $SUBNET_ID \ --security-group-ids $SECURITY_GROUP_ID \ --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${INSTANCE_NAME}}]"
7.2.AZ-cでEC2作成
$ AZ_CODE="c" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_ID=$( \ aws ec2 describe-subnets \ --filters Name=vpc-id,Values=${VPC_ID} \ Name=tag:Name,Values=${SUBNET_NAME} \ --query "Subnets[].SubnetId" \ --output text \ ) \ && echo ${SUBNET_ID} $ INSTANCE_NAME=multicloud_aws_instance_2 $ aws ec2 run-instances \ --image-id $INSTANCE_AMI --count 1 \ --instance-type t3.nano --key-name $KEY_PAIR_NAME \ --subnet-id $SUBNET_ID \ --security-group-ids $SECURITY_GROUP_ID \ --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${INSTANCE_NAME}}]"
8.GCPでGCEをサブネットに作成
$ PROJECT=multicloud-sitevpn-project $ COMPUTE_NAME=multicloud-gcp-instance $ ZONE=asia-northeast1-a $ MACHINE_TYPE=e2-micro $ SUBNET_NAME=${VPC_NETWORK}"-subnet" \ && echo ${SUBNET_NAME} $ TAG=multicloud $ OS_IMAGE=projects/ubuntu-os-cloud/global/images/ubuntu-2404-noble-amd64-v20240701a $ DISK_SIZE=10 $ DISK_NAME=test-instance $ gcloud compute --project "${PROJECT}" instances create "${COMPUTE_NAME}" \ --zone "${ZONE}" \ --machine-type "${MACHINE_TYPE}" \ --subnet "${SUBNET_NAME}" \ --maintenance-policy "MIGRATE" \ --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append \ --tags "${TAG}" \ --image "${OS_IMAGE}" \ --boot-disk-size "10" \ --boot-disk-type "pd-standard" \ --boot-disk-device-name "${DISK_NAME}"
9.接続確認
GCEにブラウザからアクセスしAWSのEC2にpingしてからsshで接続できるかを確認する10.GCP側の後始末
10.1.GCEの削除
$ PROJECT=multicloud-sitevpn-project $ REGION=asia-northeast1 $ VPC_NETWORK=multi-cloud-vpn-gcp $ COMPUTE_NAMES=multicloud-gcp-instance $ ZONE=asia-northeast1-a $ gcloud compute --project "${PROJECT}" instances delete ${COMPUTE_NAME} --zone=${ZONE}
10.2.ルートとVPNトンネルの削除
$ ROUTE_NAME=${VPC_NETWORK}"-route1" \ && echo ${ROUTE_NAME} $ gcloud --project=${PROJECT} compute routes delete ${ROUTE_NAME} $ ROUTE_NAME=${VPC_NETWORK}"-route2" \ && echo ${ROUTE_NAME} $ gcloud --project=${PROJECT} compute routes delete ${ROUTE_NAME} $ TUNNEL_NAME=${VPC_NETWORK}-vpn-tunel1 $ gcloud compute vpn-tunnels delete ${TUNNEL_NAME} \ --project=${PROJECT} \ --region=${REGION} $ TUNNEL_NAME=${VPC_NETWORK}-vpn-tunel2 $ gcloud compute vpn-tunnels delete ${TUNNEL_NAME} \ --project=${PROJECT} \ --region=${REGION}
10.3.GCPのVPNgateway削除
$ GW_NAME=${VPC_NETWORK}"-gw" \ && echo ${GW_NAME} $ FORWORDING_RULE="fr-"${GW_NAME}"-esp" \ && echo ${FORWORDING_RULE} $ gcloud --project="${PROJECT}" compute forwarding-rules delete "${FORWORDING_RULE}" \ --region="${REGION}" $ FORWORDING_RULE="fr-"${GW_NAME}"-udp500" \ && echo ${FORWORDING_RULE} $ gcloud --project="${PROJECT}" compute forwarding-rules delete "${FORWORDING_RULE}" \ --region="${REGION}" $ FORWORDING_RULE="fr-"${GW_NAME}"-udp4500" \ && echo ${FORWORDING_RULE} $ gcloud --project="${PROJECT}" compute forwarding-rules delete "${FORWORDING_RULE}" \ --region="${REGION}" $ gcloud compute target-vpn-gateways delete "${GW_NAME}" \ --project="${PROJECT}" \ --region="${REGION}"
10.4.GW用IPアドレス削除
$ GW_IP_NAME=${GW_NAME}"-ip" $ gcloud --project="${PROJECT}" compute addresses delete "${GW_IP_NAME}" --region="${REGION}"
10.5.サブネットとVPC削除
$ SUBNET_NAME=${VPC_NETWORK}"-subnet" \ && echo ${SUBNET_NAME} $ gcloud --project="${PROJECT}" compute networks subnets delete ${SUBNET_NAME} --region ${REGION} $ FIREWALL_RULE=${VPC_NETWORK}"-ssh-allow-rule" \ && echo ${FIREWALL_RULE} $ gcloud --project="${PROJECT}" compute firewall-rules delete ${FIREWALL_RULE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-ingress-gcp-allow-rule" \ && echo ${FIREWALL_RULE} $ gcloud --project="${PROJECT}" compute firewall-rules delete ${FIREWALL_RULE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-ingress-aws-allow-rule" \ && echo ${FIREWALL_RULE} $ gcloud --project="${PROJECT}" compute firewall-rules delete ${FIREWALL_RULE} $ FIREWALL_RULE=${VPC_NETWORK}"-vpn-egress-allow-rule" \ && echo ${FIREWALL_RULE} $ gcloud --project="${PROJECT}" compute firewall-rules delete ${FIREWALL_RULE} $ gcloud --project="${PROJECT}" compute networks delete ${VPC_NETWORK}
11.AWS側の後始末
11.1.EC2削除
$ INSTANCE_NAME=multicloud_aws_instance $ EC2_ID=$( \ aws ec2 describe-instances \ --filters "Name=tag:Name,Values=${INSTANCE_NAME}" \ --query "Reservations[*].Instances[*].InstanceId" \ --output text \ ) \ && echo ${EC2_ID} $ aws ec2 terminate-instances --instance-ids ${EC2_ID} $ INSTANCE_NAME=multicloud_aws_instance_2 $ EC2_ID=$( \ aws ec2 describe-instances \ --filters "Name=tag:Name,Values=${INSTANCE_NAME}" \ --query "Reservations[*].Instances[*].InstanceId" \ --output text \ ) \ && echo ${EC2_ID} $ aws ec2 terminate-instances --instance-ids ${EC2_ID}
11.2.VPNトンネル削除
$ VPC_NAME=multi-cloud-vpn-aws $ VPN_CONNECTION_NAME=${VPC_NAME}"-con" \ && echo ${VPN_CONNECTION_NAME} $ VPN_CONID=$( \ aws ec2 describe-vpn-connections \ --filters "Name=tag:Name,Values=$VPN_CONNECTION_NAME" \ --query 'VpnConnections[?State==`available`].VpnConnectionId' \ --output text \ ) \ && echo ${VPN_CONID} $ aws ec2 delete-vpn-connection --vpn-connection-id ${VPN_CONID}
11.3.カスタマーゲートウェイ削除
$ CUSTOMER_GATEWAY_NAME=${VPC_NAME}"-csgw" \ && echo ${CUSTOMER_GATEWAY_NAME} $ CGW_ID=$( \ aws ec2 describe-customer-gateways \ --filters "Name=tag:Name,Values=$CUSTOMER_GATEWAY_NAME" \ --query 'CustomerGateways[?State==`available`].CustomerGatewayId' \ --output text \ )\ && echo ${CGW_ID} $ aws ec2 delete-customer-gateway --customer-gateway-id $CGW_ID
11.4.仮想プライベートゲートウェイ削除
$ VPN_GATEWAY_NAME=${VPC_NAME}"-vpn-gw" \ && echo ${VPN_GATEWAY_NAME} $ VPC_ID=$( \ aws ec2 describe-vpcs \ --filters "Name=tag:Name,Values=$VPC_NAME" \ --query 'Vpcs[*].VpcId' \ --output text \ ) \ && echo ${VPC_ID} $ VGW_ID=$( \ aws ec2 describe-vpn-gateways \ --filters "Name=tag:Name,Values=$VPN_GATEWAY_NAME" \ --query 'VpnGateways[?State==`available`].VpnGatewayId' \ --output text \ ) \ && echo ${VGW_ID} $ aws ec2 detach-vpn-gateway --vpn-gateway-id $VGW_ID --vpc-id $VPC_ID $ aws ec2 delete-vpn-gateway --vpn-gateway-id $VGW_ID --region $REGION
11.5.VPC削除
$ SECURITY_GROUP_NAME=${VPC_NAME}"-sg" \ && echo ${SECURITY_GROUP_NAME} $ SECURITY_GROUP_ID=$( \ aws ec2 describe-security-groups \ --filter Name=vpc-id,Values=${VPC_ID} \ Name=group-name,Values=${SECURITY_GROUP_NAME} \ --query 'SecurityGroups[].GroupId' \ --output text \ ) \ && echo ${SECURITY_GROUP_ID} $ aws ec2 delete-security-group --group-id ${SECURITY_GROUP_ID}
$ AZ_CODE="a" $ SUBNET_TYPE="private" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_ID=$( \ aws ec2 describe-subnets \ --filters Name=vpc-id,Values=${VPC_ID} \ Name=tag:Name,Values=${SUBNET_NAME} \ --query "Subnets[].SubnetId" \ --output text \ ) \ && echo ${SUBNET_ID} $ aws ec2 delete-subnet --subnet-id ${SUBNET_ID} $ AZ_CODE="c" $ SUBNET_NAME=${VPC_NAME}"-"${SUBNET_TYPE}"-subnet-"${AZ_CODE} \ && echo ${SUBNET_NAME} $ SUBNET_ID=$( \ aws ec2 describe-subnets \ --filters Name=vpc-id,Values=${VPC_ID} \ Name=tag:Name,Values=${SUBNET_NAME} \ --query "Subnets[].SubnetId" \ --output text \ ) \ && echo ${SUBNET_ID} $ aws ec2 delete-subnet --subnet-id ${SUBNET_ID}
$ aws ec2 delete-vpc --vpc-id ${VPC_ID}
◆参考サイト
・ほぼここの手順なぞってるだけ
https://www.topgate.co.jp/blog/google-service/14840
コメント