VNet ピアリングと Azure Bastion を Azure CLI でやってみた

## 背景と目的

[https://docs.microsoft.com/ja-jp/azure/bastion/vnet-peering#architecture](https://docs.microsoft.com/ja-jp/azure/bastion/vnet-peering#architecture) のアーキテクチャ図にある構成を Azure CLI で試してみたので、残しておきます。

## 前提条件

Windows 10 の GitBash 上で Azure CLI を実行しました。

## 実施内容

東日本、東アジア、米国西部リージョンに VNET を作成し、東日本と東アジア、東日本と米国西部でピアリングします。東日本には Bastion、東アジアは Windows Server 2019、米国西部は Ubuntu を配置します。

```bash:bash
prefix=vpbclidemo

az account show

az group create \
  --name ${prefix}-rg \
  --location japaneast

az network vnet create \
  --name ${prefix}-vnet-japaneast \
  --resource-group ${prefix}-rg \
  --location japaneast \
  --address-prefixes 10.1.1.0/24 \
  --subnet-name AzureBastionSubnet \
  --subnet-prefixes 10.1.1.0/27

az network vnet create \
  --name ${prefix}-vnet-eastasia \
  --resource-group ${prefix}-rg \
  --location eastasia \
  --address-prefixes 10.1.3.0/24 \
  --subnet-name vm-subnet \
  --subnet-prefixes 10.1.3.0/27

az network vnet create \
  --name ${prefix}-vnet-westus \
  --resource-group ${prefix}-rg \
  --location westus \
  --address-prefixes 10.1.5.0/24 \
  --subnet-name vm-subnet \
  --subnet-prefixes 10.1.5.0/27

az network nsg create \
  --resource-group ${prefix}-rg \
  --location japaneast \
  --name ${prefix}-bastion-nsg-japaneast

az network nsg create \
  --resource-group ${prefix}-rg \
  --location eastasia \
  --name ${prefix}-vm-nsg-eastasia

az network nsg create \
  --resource-group ${prefix}-rg \
  --location westus \
  --name ${prefix}-vm-nsg-westus

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowHttpsInbound \
  --priority 120 \
  --source-address-prefixes Internet \
  --destination-port-ranges 443 \
  --access Allow \
  --protocol Tcp

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowGatewayManagerInbound \
  --priority 130 \
  --source-address-prefixes GatewayManager \
  --destination-port-ranges 443 \
  --access Allow \
  --protocol Tcp

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowAzureLoadBalancerInbound \
  --priority 140 \
  --source-address-prefixes AzureLoadBalancer \
  --destination-port-ranges 443 \
  --access Allow \
  --protocol Tcp

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowBastionHostCommunication \
  --priority 150 \
  --source-address-prefixes VirtualNetwork \
  --destination-port-ranges 8080 5701 \
  --destination-address-prefixes VirtualNetwork \
  --access Allow \
  --protocol "*"

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowSshRdpOutbound \
  --priority 100 \
  --destination-port-ranges 22 3389 \
  --destination-address-prefixes VirtualNetwork \
  --access Allow \
  --protocol "*" \
  --direction Outbound

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowAzureCloudOutbound \
  --priority 110 \
  --destination-port-ranges 443 \
  --destination-address-prefixes AzureCloud \
  --access Allow \
  --protocol Tcp \
  --direction Outbound

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowBastionCommunication \
  --priority 120 \
  --source-address-prefixes VirtualNetwork \
  --destination-port-ranges 8080 5701 \
  --destination-address-prefixes VirtualNetwork \
  --access Allow \
  --protocol "*" \
  --direction Outbound

az network nsg rule create \
  --resource-group ${prefix}-rg \
  --nsg-name ${prefix}-bastion-nsg-japaneast \
  --name AllowGetSessionInformation \
  --priority 130 \
  --destination-port-ranges 80 \
  --destination-address-prefixes Internet \
  --access Allow \
  --protocol Tcp \
  --direction Outbound

az network vnet subnet update \
  --resource-group ${prefix}-rg \
  --name AzureBastionSubnet \
  --vnet-name ${prefix}-vnet-japaneast \
  --network-security-group ${prefix}-bastion-nsg-japaneast

az network vnet subnet update \
  --resource-group ${prefix}-rg \
  --name vm-subnet \
  --vnet-name ${prefix}-vnet-eastasia \
  --network-security-group ${prefix}-vm-nsg-eastasia

az network vnet subnet update \
  --resource-group ${prefix}-rg \
  --name vm-subnet \
  --vnet-name ${prefix}-vnet-westus \
  --network-security-group ${prefix}-vm-nsg-westus

az network vnet peering create \
  --name japaneast-eastasia \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-japaneast \
  --remote-vnet ${prefix}-vnet-eastasia \
  --allow-vnet-access

az network vnet peering create \
  --name eastasia-japaneast \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-eastasia \
  --remote-vnet ${prefix}-vnet-japaneast \
  --allow-vnet-access

az network vnet peering show \
  --name japaneast-eastasia \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-japaneast \
  --query peeringState

az network vnet peering create \
  --name japaneast-westus \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-japaneast \
  --remote-vnet ${prefix}-vnet-westus \
  --allow-vnet-access

az network vnet peering create \
  --name westus-japaneast \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-westus \
  --remote-vnet ${prefix}-vnet-japaneast \
  --allow-vnet-access

az network vnet peering show \
  --name japaneast-westus \
  --resource-group ${prefix}-rg \
  --vnet-name ${prefix}-vnet-japaneast \
  --query peeringState

az network public-ip create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-bastion-pip \
  --sku Standard \
  --location japaneast

az network bastion create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-bastion \
  --public-ip-address ${prefix}-bastion-pip \
  --vnet-name ${prefix}-vnet-japaneast \
  --location japaneast

vmpassword=$(openssl rand -base64 16)
echo $vmpassword

az vm create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm01 \
  --os-disk-name ${prefix}-vm01OSDisk \
  --image Win2019Datacenter \
  --size Standard_B1s \
  --vnet-name ${prefix}-vnet-eastasia \
  --location eastasia \
  --subnet vm-subnet \
  --admin-username azureuser \
  --admin-password $vmpassword \
  --nsg "" \
  --public-ip-address "" \
  --storage-sku Standard_LRS

az vm create \
  --resource-group ${prefix}-rg \
  --name ${prefix}-vm02 \
  --os-disk-name ${prefix}-vm02OSDisk \
  --image UbuntuLTS \
  --size Standard_B1s \
  --vnet-name ${prefix}-vnet-westus \
  --location westus \
  --subnet vm-subnet \
  --admin-username azureuser \
  --admin-password $vmpassword \
  --nsg "" \
  --public-ip-address "" \
  --storage-sku Standard_LRS

az group delete \
  --name ${prefix}-rg
```

## 実施結果

RDP 接続できました。

![vnetpeering-bastion-01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/675885/6109fbac-8ebd-8e51-da1e-3eae97c004ab.png)

SSH 接続できました。

![vnetpeering-bastion-02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/675885/3e73a981-5f60-3173-f5be-f3268e828ed4.png)

## 参考

[General availability: VNET peering support for Azure Bastion](https://azure.microsoft.com/ja-jp/updates/general-availability-vnet-peering-support-for-azure-bastion/)

[VNet ピアリングと Azure Bastion](https://docs.microsoft.com/ja-jp/azure/bastion/vnet-peering)

[Azure CLI を使用して Azure Bastion ホストを作成する](https://docs.microsoft.com/ja-jp/azure/bastion/create-host-cli)

[Azure CLI を使用して仮想ネットワーク ピアリングで仮想ネットワークを接続する](https://docs.microsoft.com/ja-jp/azure/virtual-network/tutorial-connect-virtual-networks-cli)