0
0

PowerShellを使ったAzure Firewallの停止・起動方法

Last updated at Posted at 2023-12-25

はじめに

本記事は、2023/10/19 時点の内容をまとめたものである。

Azure Firewall(以下、AFW)は Azure Portal から停止することができない。
コスト節約のため、AFW を都度作成・削除する運用を行っていたが、Azure PowerShell を利用した AFW の停止・起動方法を知り、手順についてまとめた。

AFW を停止・起動できるツールは下記の通り。

Azure Portal Azure CLI Azure PowerShell
停止 × ×
起動 ×

前提

  • AFW、AFWポリシー、パブリックIPアドレス、仮想ネットワーク、サブネット、仮想マシンは作成済み
  • 強制トンネリング用に構成されていない AFW とする
  • ローカルPCは Windows

事前準備

1. Windows に PowerShell をインストールする

ローカルPCで下記コマンドを実行し、最新バージョンの PowerShell を検索

cmd
winget search Microsoft.PowerShell
結果
Name               Id                           Version   Source
-----------------------------------------------------------------
PowerShell         Microsoft.PowerShell         7.3.8.0   winget
PowerShell Preview Microsoft.PowerShell.Preview 7.4.0.6   winget

実際の画面
画像1.png

id パラメータを使用して PowerShell または PowerShell Preview をインストール
(今回はPowerShellを選択)

cmd
winget install --id Microsoft.Powershell --source winget

実際の画面
画像2.png

2. Azure PowerShell モジュールをインストールする

PowerShell で下記コマンドを実行し、Azure PowerShell モジュールをインストール

PowerShell
if (Get-Module -Name AzureRM -ListAvailable) {
    Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
      'Az modules installed at the same time is not supported.')
} else {
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
}

実際の画面
画像3.png

PSGallery の初回使用時には、以下のプロンプトが表示される。インストールを続行するには、「Yes」または「Yes to All」を選択
ダウンロード.png

NuGetプロバイダーとリポジトリの信頼が必要な場合の画面
ダウンロード.png

AFW の停止手順

1. Azure Account に接続する

PowerShell で下記コマンドを実行し、認証されたアカウントを使用して Azure 環境に接続

PowerShell
Connect-AzAccount

実際の画面
画像4.png

MFA認証が動作しなかった場合はテナントIDを指定
画像5.png

2. AFW を停止する

下記コマンドを実行し、AFW を停止

PowerShell
$ResourceGroupName = "<リソースグループの名前>"
$AzureFirewallName = "<AFWの名前>"
$azfw = Get-AzFirewall -Name $AzureFirewallName -ResourceGroupName $ResourceGroupName
$azfw.Deallocate()
Set-AzFirewall -AzureFirewall $azfw

3. Azure Portal から確認

  • 停止前

    • AFW の画面
      • サブネット、パブリックIPアドレス、プライベートIPアドレスが表示されている
        画像6.png
    • パブリックIPアドレスの画面
      • 関連付け先に AFW が表示されている
        画像7.png
  • 停止後

    • AFW の画面
      • サブネット、パブリックIPアドレス、プライベートIPアドレスが表示されない
        画像9.png
    • パブリックIPアドレスの画面
      • 関連付け先が表示されない
        画像10.png

4. AFW を経由した通信確認

仮想マシンから Google へアクセスする AFWポリシールールを作成。
仮想マシンにログインし、下記コマンドを実行してGoogleにアクセス

cmd
curl -I http://www.google.com
  • 停止前
    • アクセス成功
      画像11.png
  • 停止後
    • アクセス失敗
      画像12.png

AFW の起動手順

1. Azure Account に接続する

PowerShell で下記コマンドを実行し、認証されたアカウントを使用して Azure 環境に接続

PowerShell
Connect-AzAccount

2. AFW を起動する

下記コマンドを実行し、AFW を起動

PowerShell
$ResourceGroupName = "<リソースグループの名前>"
$AzureFirewallName = "<AFWの名前>"
$VirtualNetworkName = "<仮想マシンの名前>"
$PublicIPName = "<パブリックIPアドレスの名前>"
$azfw = Get-AzFirewall -Name $AzureFirewallName -ResourceGroupName $ResourceGroupName
$vnet = Get-AzVirtualNetwork  -Name $VirtualNetworkName -ResourceGroupName $ResourceGroupName
$publicip = Get-AzPublicIpAddress -Name $PublicIPName -ResourceGroupName $ResourceGroupName
$azfw.Allocate($vnet, $publicip)
Set-AzFirewall -AzureFirewall $azfw

実際の画面
画像1.png

3. Azure Portal から確認

  • 起動前

    • AFW の画面
      • サブネット、パブリックIPアドレス、プライベートIPアドレスが表示されない
        画像7.png
    • パブリックIPアドレスの画面
      • 関連付け先が表示されない
        画像6.png
  • 起動後

    • AFW の画面
      • サブネット、パブリックIPアドレス、プライベートIPアドレスが表示されている
        画像4.png
    • パブリックIPアドレスの画面
      • 関連付け先に AFW が表示されている
        画像3.png

4. AFW を経由した通信確認

仮想マシンから Google へアクセスする AFWポリシールールを作成。
仮想マシンにログインし、下記コマンドを実行してGoogleにアクセス

cmd
curl -I http://www.google.com
  • 起動前
    • アクセス失敗
      画像12.png
  • 起動後
    • アクセス成功
      画像11.png

以上

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0