{VPN}Site-to-Site VPN(Azure <-> AWS)

 

https://docs.microsoft.com/ja-jp/azure/vpn-gateway/vpn-gateway-howto-aws-bgp
https://docs.microsoft.com/ja-jp/azure/vpn-gateway/vpn-gateway-activeactive-rm-powershell
https://qiita.com/johanburati/items/5f6cccdf6323e6bfe779
https://docs.microsoft.com/en-us/powershell/module/az.network/new-azvirtualnetworkgateway?view=azps-8.0.0
https://docs.aws.amazon.com/vpc/latest/userguide/WorkWithRouteTables.html#EnableDisableRouteProp

 


BGP APIPA アドレスは、AZURE で予約されている VPN 用の APIPA 範囲 (169.254.21.0 から 169.254.22.255) 内にある必要があります。

トンネル1本のみ使用の場合もAzure VPN ゲートウェイでアクティブ/アクティブを有効にする必要がある


前提: 
AWS側の作業OS: Amazon Linux2(EC2)
Azure側の作業OS: CentOS7(オンプレ)
AWS側の作業ユーザ: ec2-user
Azure側の作業ユーザ: testuser

AWS側のASN: 64512
Azure側のASN: 64514

IPSec tunnel使用本数: 1本

AWS側のサブネット: 10.0.0.0/24
Azure側のサブネット: 10.2.0.0/24
Azure側のゲートウェイサブネット: 10.2.1.0/24

 

-- 1. 事前準備【AWS

-- 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. azコマンドのインストール【Azure】

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc


echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | sudo tee /etc/yum.repos.d/azure-cli.repo

sudo yum -y install azure-cli
az --version 

az login

※コンソールで実行

az account show

-- 3. Azure Powershellモジュールのインストール【Azure】


curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
sudo yum install -y powershell
pwsh

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

Connect-AzAccount -UseDeviceAuthentication

 


-- 4. VPC、サブネット作成【AWS

-- 4.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'


-- 4.2 サブネット(10.0.0.0/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 describe-subnets
aws ec2 describe-subnets | jq -r '.Subnets.SubnetId'


-- 4.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'


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


-- 4.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

 

-- 5. リソースグループ作成【Azure】


az account list-locations

az group create \
--name rg01 \
--location japaneast

az group list


az configure --defaults group=rg01

 

-- 6. VNetを作成する【Azure】


az network vnet create \
--name vnet21 \
--resource-group rg01 \
--address-prefixes 10.2.0.0/16 \
--ddos-protection false \
--location japaneast


az network vnet list
az network vnet show --resource-group rg01 --name vnet21

 


-- 7. サブネットを作成する【Azure】

az network vnet subnet create \
--address-prefixes 10.2.0.0/24 \
--name subnet21 \
--resource-group rg01 \
--vnet-name vnet21


az network vnet subnet create \
--address-prefixes 10.2.1.0/24 \
--name GatewaySubnet \
--resource-group rg01 \
--vnet-name vnet21


az network vnet subnet list --resource-group rg01 --vnet-name vnet21
az network vnet subnet show --resource-group rg01 --vnet-name vnet21 --name subnet21
az network vnet subnet show --resource-group rg01 --vnet-name vnet21 --name GatewaySubnet


-- 8. パブリックIPアドレスを作成する【Azure】

az network public-ip create \
--name pip21 \
--resource-group rg01 \
--allocation-method Static \
--location japaneast \
--sku Standard \
--tier Regional \
--version IPv4

az network public-ip create \
--name pip22 \
--resource-group rg01 \
--allocation-method Static \
--location japaneast \
--sku Standard \
--tier Regional \
--version IPv4


az network public-ip list 
az network public-ip show --resource-group rg01 --name pip21
az network public-ip show --resource-group rg01 --name pip22

 


-- 9. 仮想ネットワークゲートウェイを作成する【Azure】
※azコマンドはactive-activeに未対応の模様のためpowershellで実行


$pip21 = Get-AzPublicIpAddress -Name pip21 -ResourceGroupName rg01
$pip22 = Get-AzPublicIpAddress -Name pip22 -ResourceGroupName rg01

$gatewaysubnet = Get-AzVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $(Get-AzVirtualNetwork -Name vnet21 -ResourceGroupName rg01)


$pip21conf = New-AzVirtualNetworkGatewayIpConfig -Name pip21conf -Subnet $gatewaysubnet -PublicIpAddress $pip21
$pip22conf = New-AzVirtualNetworkGatewayIpConfig -Name pip22conf -Subnet $gatewaysubnet -PublicIpAddress $pip22


$addresslist21 = @('169.254.21.2','169.254.22.2')
$pip21bgpconf = New-AzIpConfigurationBgpPeeringAddressObject -IpConfigurationId $pip21conf.Id  -CustomAddress $addresslist21

$addresslist22 = @('169.254.21.6','169.254.22.6')
$pip22bgpconf = New-AzIpConfigurationBgpPeeringAddressObject -IpConfigurationId $pip22conf.Id  -CustomAddress $addresslist22

 

New-AzVirtualNetworkGateway `
-Name vng21 `
-ResourceGroupName rg01 `
-Location japaneast `
-IpConfigurations $pip21conf,$pip22conf `
-GatewayType Vpn `
-VpnType RouteBased `
-GatewaySku VpnGw1 `
-VpnGatewayGeneration "Generation1" `
-Asn 64514 `
-EnableActiveActiveFeature `
-IpConfigurationBgpPeeringAddresses $pip21bgpconf,$pip22bgpconf `
-EnableBGP $True


※作成に20分程度かかる

 

 

az network vnet-gateway list
az network vnet-gateway show --resource-group rg01 --name vng21

 


-- 10. カスタマーゲートウェイ(CGW)作成【AWS


aws ec2 create-customer-gateway \
--bgp-asn 64514 \
--type ipsec.1 \
--tag-specifications 'ResourceType=customer-gateway,Tags=[{Key=Name,Value=cgw01}]' \
--ip-address 192.0.2.1

※ip-addressはAzure 仮想ネットワークゲートウェイのpip21のIPアドレスを指定する


aws ec2 describe-customer-gateways

 

-- 11. 仮想プライベートゲートウェイ(VGW)作成【AWS


aws ec2 create-vpn-gateway \
--availability-zone ap-northeast-1a \
--type ipsec.1 \
--tag-specifications 'ResourceType=vpn-gateway,Tags=[{Key=Name,Value=vgw01}]' \
--amazon-side-asn 64512

aws ec2 describe-vpn-gateways

 

aws ec2 attach-vpn-gateway \
--vpc-id vpc-11111111111111111 \
--vpn-gateway-id vgw-44444444444444444

 

-- 12. ルート伝達を有効にする【AWS


aws ec2 enable-vgw-route-propagation \
--gateway-id vgw-44444444444444444 \
--route-table-id rtb-33333333333333333

 

-- 13. VPN接続の作成【AWS

 


aws ec2 create-vpn-connection \
--customer-gateway-id cgw-55555555555555555 \
--type ipsec.1 \
--vpn-gateway-id vgw-44444444444444444 \
--tag-specifications 'ResourceType=vpn-connection,Tags=[{Key=Name,Value=vpn01}]' \
--options TunnelOptions='[{TunnelInsideCidr=169.254.21.0/30,PreSharedKey=PreSharedKey1,StartupAction=start},{TunnelInsideCidr=169.254.22.0/30,PreSharedKey=PreSharedKey2,StartupAction=start}]'

 

aws ec2 describe-vpn-connections


aws ec2 get-vpn-connection-device-types
aws ec2 get-vpn-connection-device-types | grep -C 5 "Generic"


aws ec2 get-vpn-connection-device-sample-configuration \
--vpn-connection-id vpn-66666666666666666 \
--vpn-connection-device-type-id 9005b6c1 \
--internet-key-exchange-version ikev2 \
--output text

 

IPSec Tunnel #1のPre-Shared KeyとOutside IP AddressesとInside IP Addressesを記録


-- 14. ローカル ネットワーク ゲートウェイを作成する【Azure】


az network local-gateway create \
--gateway-ip-address 192.0.2.2 \
--name lng21 \
--resource-group rg01 \
--asn 64512 \
--bgp-peering-address 169.254.21.1 \
--location japaneast


gateway-ip-addressはAWS側Tunnel #1の外部IPアドレス

 


az network local-gateway list
az network local-gateway show --resource-group rg01 --name lng21


-- 15. 接続を作成する【Azure】


az network vpn-connection create \
--name vpn21 \
--resource-group rg01 \
--vnet-gateway1 vng21 \
--enable-bgp \
--local-gateway2 lng21 \
--location japaneast \
--shared-key PreSharedKey1


BGP設定のコマンドオプションが見つからないのでGUIから下記更新実施

カスタム BGP アドレスを有効にする
169.254.21.2
169.254.21.6 

接続モード -> ResponderOnly

 

 

az network vpn-connection list
az network vpn-connection show --resource-group rg01 --name vpn21


IPSECトンネルがUPするまで5分程度待つ

 

-- 16. BGPステータス確認【AWS

aws ec2 describe-vpn-connections

 

-- 17. 疎通確認

 

-- 17.1 インスタンス作成【AWS

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-77777777777777777 \
--associate-public-ip-address


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


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


0.0.0.0/0からのSSH許可追加

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

 

-- 17.2 インスタンス作成【Azure】

ssh-keygen

az vm image list \
--location japaneast \
--offer CentOS \
--all

 


az vm create \
--resource-group rg01 \
--name instance-1 \
--image OpenLogic:CentOS:7_9-gen2:7.9.2022020701 \
--admin-username azureuser \
--generate-ssh-keys \
--size Standard_B1ls \
--public-ip-address ip01 \
--public-ip-sku Basic \
--enable-agent false \
--vnet-name vnet21 \
--subnet subnet21


ssh azureuser@192.0.2.3

 


-- 17.3 pingで疎通確認【AWS

ping 10.2.0.4

-- 17.4 pingで疎通確認【Azure】

ping 10.0.0.234

 


-- 18. クリーンアップ【Azure】
-- リソースグループの削除

az group list

az group delete \
--name rg01 \
--yes

az group delete \
--name NetworkWatcherRG \
--yes

 


-- 16. クリーンアップ【AWS


-- インスタンス削除

aws ec2 describe-instances

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

 

-- VPN接続削除


aws ec2 describe-vpn-connections

aws ec2 delete-vpn-connection \
--vpn-connection-id vpn-66666666666666666

 


-- 仮想プライベートゲートウェイ削除(VPCからデタッチしてから)


aws ec2 describe-vpn-gateways

aws ec2 detach-vpn-gateway \
--vpc-id vpc-11111111111111111 \
--vpn-gateway-id vgw-44444444444444444


aws ec2 delete-vpn-gateway \
--vpn-gateway-id vgw-44444444444444444

 


-- カスタマーゲートウェイ削除

aws ec2 describe-customer-gateways

aws ec2 delete-customer-gateway \
--customer-gateway-id cgw-55555555555555555

 

-- インターネットゲートウェイ削除(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-77777777777777777

 

-- VPC削除

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

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