この記事では、Azure PowerShell を使用して複数の Azure Firewall 規則を効率的に追加および変更する方法について説明します。 この記事の手順に従うことで、更新時間を短縮し、構成の競合を回避できます。
[前提条件]
開始する前に、以下の項目があることを確認します:
- 既存の Azure Firewall または Azure Firewall ポリシーを使用する Azure サブスクリプション
- Azure PowerShell のインストールと構成
- ファイアウォール ポリシーを変更するための適切なアクセス許可
- 運用環境に適用する前に変更を検証するテスト環境
概要
Azure Firewall または Azure Firewall Policy に新しい規則を追加する場合は、次の方法を使用して、合計更新時間を短縮し、潜在的な競合を回避します。
- Azure Firewall または Azure Firewall Policy オブジェクトを取得します。
- すべての新しいルールを追加し、ローカル オブジェクトで他の必要な変更を実行します。 既存のルール コレクションに追加することも、必要に応じて新しいルール コレクションを作成することもできます。
- すべての変更が完了した場合にのみ、ファイアウォールまたはファイアウォール ポリシーの更新をプッシュします。
この方法により、アトミックな更新が保証され、構成の競合のリスクが軽減されます。
次のセクションでは、新しい DNAT 規則を追加し、ファイアウォール ポリシーの既存の規則を更新する方法について説明します。 次の場合は、次の同じ原則に従う必要があります。
- アプリケーションまたはネットワークルールを更新する
- クラシック ルールで管理されているファイアウォールを更新する
- 既存のルール構成を変更する
Important
テスト ポリシーでこれらの手順を必ず最初にテストし、特定の要件に対して期待どおりに動作することを確認します。
Azure に接続してコンテキストを設定する
Azure アカウントに接続し、コンテキストをターゲット サブスクリプションに設定します。
Connect-AzAccount
Set-AzContext -Subscription "<Subscription ID>"
ファイアウォール ポリシー オブジェクトを取得する
ファイアウォール ポリシー、ルール コレクション グループ、および変更する特定のルール コレクションのローカル オブジェクトを作成します。
$policy = Get-AzFirewallPolicy -Name "<Policy Name>" -ResourceGroupName "<Resource Group Name>"
$natrulecollectiongroup = Get-AzFirewallPolicyRuleCollectionGroup -Name "<Rule Collection Group Name>" -ResourceGroupName "<Resource Group Name>" -AzureFirewallPolicyName "<Firewall Policy Name>"
$existingrulecollection = $natrulecollectiongroup.Properties.RuleCollection | Where-Object {$_.Name -eq "<rule collection name>"}
新しいルールを追加する
新しいルールを定義する
ルール コレクションに追加する新しい DNAT ルールを定義します。
$newrule1 = New-AzFirewallPolicyNatRule -Name "dnat-rule1" -Protocol "TCP" -SourceAddress "<Source Address>" -DestinationAddress "<Destination Address>" -DestinationPort "<Destination Port>" -TranslatedAddress "<Translated Address>" -TranslatedPort "<Translated Port>"
$newrule2 = New-AzFirewallPolicyNatRule -Name "dnat-rule2" -Protocol "TCP" -SourceAddress "<Source Address>" -DestinationAddress "<Destination Address>" -DestinationPort "<Destination Port>" -TranslatedAddress "<Translated Address>" -TranslatedPort "<Translated Port>"
ルールコレクションにルールを追加する
新しい規則をローカル ルール コレクション オブジェクトに追加します。
$existingrulecollection.Rules.Add($newrule1)
$existingrulecollection.Rules.Add($newrule2)
注
この手順では、必要な数のルールを追加できます。 最後の手順で Azure を更新するまで、ローカル オブジェクトに対するすべての変更が行われます。
既存のルールを更新する
ルール コレクション内の既存のルールを変更するには:
既存のルールを検索して変更する
既存のルールを名前で検索し、そのプロパティを変更します。
# Find the existing rule
$existingRule = $existingrulecollection.Rules | Where-Object {$_.Name -eq "existing-rule-name"}
# Modify rule properties
if ($existingRule) {
$existingRule.SourceAddresses = @("10.0.0.0/8", "192.168.0.0/16")
$existingRule.DestinationPorts = @("80", "443")
$existingRule.TranslatedAddress = "10.1.1.100"
$existingRule.TranslatedPort = "8080"
Write-Host "Rule '$($existingRule.Name)' updated successfully."
} else {
Write-Warning "Rule 'existing-rule-name' not found."
}
既存のルールを削除する
コレクションからルールを削除するには:
# Find and remove the rule
$ruleToRemove = $existingrulecollection.Rules | Where-Object {$_.Name -eq "rule-to-delete"}
if ($ruleToRemove) {
$existingrulecollection.Rules.Remove($ruleToRemove)
Write-Host "Rule '$($ruleToRemove.Name)' removed successfully."
} else {
Write-Warning "Rule 'rule-to-delete' not found."
}
コレクション内のすべてのルールを一覧表示する
コレクション内のすべての現在のルールを表示するには:
# Display all rules
$existingrulecollection.Rules | Select-Object Name, Protocol, SourceAddresses, DestinationAddresses, DestinationPorts, TranslatedAddress, TranslatedPort | Format-Table
Azure に変更を適用する
ローカル オブジェクトに対するすべての変更を行った後、Azure のルール コレクション グループを更新します。
try {
Set-AzFirewallPolicyRuleCollectionGroup -Name "<Rule Collection Group Name>" -FirewallPolicyObject $policy -Priority 200 -RuleCollection $natrulecollectiongroup.Properties.RuleCollection
Write-Host "Firewall policy updated successfully."
} catch {
Write-Error "Failed to update firewall policy: $($_.Exception.Message)"
}
Important
この手順では、すべての変更が Azure に適用されます。 運用環境でこのコマンドを実行する前に、構成を十分にテストしていることを確認します。
変更を確認する
ポリシーを更新した後、変更が正しく適用されたことを確認します。
# Refresh the policy object and verify changes
$updatedPolicy = Get-AzFirewallPolicy -Name "<Policy Name>" -ResourceGroupName "<Resource Group Name>"
$updatedRuleGroup = Get-AzFirewallPolicyRuleCollectionGroup -Name "<Rule Collection Group Name>" -ResourceGroupName "<Resource Group Name>" -AzureFirewallPolicyName "<Firewall Policy Name>"
# Display the updated rules
$updatedRuleCollection = $updatedRuleGroup.Properties.RuleCollection | Where-Object {$_.Name -eq "<rule collection name>"}
$updatedRuleCollection.Rules | Select-Object Name, Protocol, SourceAddresses, DestinationAddresses | Format-Table
リソースをクリーンアップする
このチュートリアルのテスト リソースを作成した場合は、課金されないように削除できます。 リソース グループを削除すると、ファイアウォールとすべての関連リソースも削除されます。
Remove-AzResourceGroup -Name "<Resource Group Name>" -Force
注意事項
このコマンドは、リソース グループ内のすべてのリソースを削除する場合にのみ実行します。 このアクションは取り消すことができません。
次のステップ
Azure Firewall ポリシーとルール管理の詳細については、次の記事を参照してください。