Azure Bastion を Powershell からセットアップする
検証環境用にぱっと作って削除したかったのでPowershell化してみた。
注意点
以下を満たしていないとNew-AzBastion
コマンドでエラーになる。
Bastionホストと関連リソースを作成する
# リソースグループ名 $rg_name = 'rg_test' # Bationを展開するvNet名 $vnet_name = 'vnet_test' # Bastion専用のサブネット $sn_prefix = '10.0.254.0/24' # Bastion専用のサブネットに設定するNSG名 $ngs_name = 'nsg_my_bastion' ## Bastionに設定するNSGを作成 # 自分の端末のグローバルIP $myipinfo = Invoke-RestMethod http://ipinfo.io/json $myip = $myipinfo.ip # 自分の作業端末からBastionへのアクセスを許可するセキュリティグループを作成 $nsg = New-AzNetworkSecurityGroup -Name "$ngs_name" -Location 'Japan East' -ResourceGroupName "$rg_name" # Inbound Rules Add-AzNetworkSecurityRuleConfig -Name AllowMyPublicIPInbound -NetworkSecurityGroup $nsg -Direction Inbound -SourceAddressPrefix $myip -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 443 -Priority 100 -Protocol Tcp -Access Allow -Description "Allow Bastion from my public IP" Add-AzNetworkSecurityRuleConfig -Name AllowGatewayManagerInbound -NetworkSecurityGroup $nsg -Direction Inbound -SourceAddressPrefix 'GatewayManager' -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 443 -Priority 101 -Protocol Tcp -Access Allow Add-AzNetworkSecurityRuleConfig -Name AllowAzureLoadBlancerInbound -NetworkSecurityGroup $nsg -Direction Inbound -SourceAddressPrefix 'AzureLoadBalancer' -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 443 -Priority 102 -Protocol Tcp -Access Allow Add-AzNetworkSecurityRuleConfig -Name AllowBastionHostCommunication -NetworkSecurityGroup $nsg -Direction Inbound -SourceAddressPrefix 'VirtualNetwork' -SourcePortRange * -DestinationAddressPrefix 'VirtualNetwork' -DestinationPortRange 8080,5701 -Priority 103 -Protocol * -Access Allow # Outbound Rules Add-AzNetworkSecurityRuleConfig -Name AllowAnyOutbound -NetworkSecurityGroup $nsg -Direction Outbound -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange * -Priority 200 -Protocol Tcp -Access Allow # NSGにルールを適用 $nsg | Set-AzNetworkSecurityGroup ## vNetにBastion用のパブリック・サブネットを追加する # vNetと作成したNSGをオブジェクトとして取得 $vnet = Get-AzVirtualNetwork -ResourceGroupName "$rg_name" | Where-Object {$_.Name -eq "$vnet_name"} $nsg = Get-AzNetworkSecurityGroup -ResourceGroupName "$rg_name" -Name "$ngs_name" # vNetに AzureBastionSubnet を追加 # インターネットからアクセスする場合は、ユーザ定義のルーティングテーブルは不要(パブリック・サブネットして構成) Add-AzVirtualNetworkSubnetConfig -Name 'AzureBastionSubnet' -VirtualNetwork $vnet -AddressPrefix "$sn_prefix" -NetworkSecurityGroup $nsg $vnet | Set-AzVirtualNetwork $vnet = Get-AzVirtualNetwork -ResourceGroupName "$rg_name" | Where-Object {$_.Name -eq "$vnet_name"} ## Public IPを取得し、Bastionホストをデプロイする # Publid IPのSKUはStandard $publicip = New-AzPublicIpAddress -ResourceGroupName "$rg_name" -name "pubip_bastion" -location 'Japan East' -AllocationMethod Static -Sku Standard # Bastionホストをデプロイ $bastion = New-AzBastion -ResourceGroupName "$rg_name" -Name "myBastion" -PublicIpAddress $publicip -VirtualNetwork $vnet
Bastionホストと関連リソースを削除する
# リソースグループ名 $rg_name = 'rg_test' # Bationを展開するvNet名 $vnet_name = 'vnet_test' # Bastion専用のサブネットに設定するNSG名 $ngs_name = 'nsg_my_bastion' Remove-AzBastion -ResourceGroupName "$rg_name" -Name "myBastion" Remove-AzPublicIpAddress -Name "pubip_bastion" -ResourceGroupName "$rg_name" $vnet = Get-AzVirtualNetwork -ResourceGroupName "$rg_name" -Name "$vnet_name" Remove-AzVirtualNetworkSubnetConfig -Name 'AzureBastionSubnet' -VirtualNetwork $vnet $vnet | Set-AzVirtualNetwork Remove-AzNetworkSecurityGroup -ResourceGroupName "$rg_name" -Name "$ngs_name"