このクイック スタートでは、Terraform を使用して、3 つの Availability Zones に Azure Firewall をデプロイします。
Terraformはクラウドインフラストラクチャの定義、プレビュー、そしてデプロイメントを可能にします。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 あなたの設定ファイルを作成した後、実行計画を作成します。これにより、インフラストラクチャの変更をデプロイする前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
Terraform 構成では、ファイアウォールを使用してテスト ネットワーク環境が作成されます。 ネットワークには、 AzureFirewallSubnet、 subnet-server、 subnet-jump の 3 つのサブネットを持つ 1 つの仮想ネットワーク (VNet) があります。 サブネット サーバーとサブネットジャンプ サブネットには、それぞれ 1 つの 2 コア Windows Server 仮想マシンがあります。
ファイアウォールは AzureFirewallSubnet サブネット内にあり、 www.microsoft.com
へのアクセスを許可する単一の規則を持つアプリケーション規則コレクションがあります。
ユーザー定義ルートは、ファイアウォール規則が適用されているファイアウォールを経由して 、サブネット サーバー サブネットからのネットワーク トラフィックを指します。
Azure Firewall の詳細については、Azure portal を使用した Azure Firewall のデプロイと構成に関するページを参照してください。
この記事では、次の方法について説明します。
- random_petを使用してランダムな値 (リソース グループ名で使用される) を作成する
- azurerm_resource_group を使って Azure リソース グループを作成する
- azurerm_virtual_networkを使用して Azure Virtual Network を作成する
- azurerm_subnetを使用して 3 つの Azure サブネットを作成する
- azurerm_public_ipを使用して Azure パブリック IP を作成する
- azurerm_firewall_policyを使用して Azure Firewall ポリシーを作成する
- azurerm_firewall_policy_rule_collection_groupを使用して Azure Firewall ポリシー規則コレクション グループを作成する
- azurerm_firewallを使用して Azure Firewall を作成する
- azurerm_network_interfaceを使用してネットワーク インターフェイスを作成する
- azurerm_network_security_groupを使用してネットワーク セキュリティ グループを作成する (ネットワーク セキュリティ規則の一覧を含む)
- ネットワーク インターフェイスとネットワーク セキュリティ グループを関連付けるには、azurerm_network_interface_security_group_association を使用します。
- azurerm_route_tableを使用してルート テーブルを作成する
- を使用してルート テーブルとサブネット間の関連付けを作成 azurerm_subnet_route_table_association
- random_stringを使用してランダムな値 (ストレージ名として使用) を作成する
- azurerm_storage_accountを使用してストレージ アカウントを作成する
- random_passwordを使用して Windows VM のランダム なパスワードを作成する
- azurerm_windows_virtual_machineを使用して Azure Windows 仮想マシンを作成する
[前提条件]
Terraform コードを実装する
注
この記事のサンプル コードは、 Azure Terraform GitHub リポジトリにあります。 Terraform の現在および以前のバージョンからのテスト結果を含むログ ファイルを表示できます。
Terraform を使用して Azure リソースを管理する方法を示すその他の記事とサンプル コードを参照してください
サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。
providers.tf
という名前のファイルを作成し、次のコードを挿入します。terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
main.tf
という名前のファイルを作成し、次のコードを挿入します。resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "random_string" "storage_account_name" { length = 8 lower = true numeric = false special = false upper = false } resource "random_password" "password" { length = 20 min_lower = 1 min_upper = 1 min_numeric = 1 min_special = 1 special = true } resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id location = var.resource_group_location } resource "azurerm_public_ip" "pip_azfw" { name = "pip-azfw" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name allocation_method = "Static" sku = "Standard" zones = ["1", "2", "3"] } resource "azurerm_storage_account" "sa" { name = random_string.storage_account_name.result resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location account_tier = "Standard" account_replication_type = "LRS" account_kind = "StorageV2" } resource "azurerm_virtual_network" "azfw_vnet" { name = "azfw-vnet" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name address_space = ["10.10.0.0/16"] } resource "azurerm_subnet" "azfw_subnet" { name = "AzureFirewallSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.azfw_vnet.name address_prefixes = ["10.10.0.0/26"] } resource "azurerm_subnet" "server_subnet" { name = "subnet-server" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.azfw_vnet.name address_prefixes = ["10.10.1.0/24"] } resource "azurerm_subnet" "jump_subnet" { name = "subnet-jump" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.azfw_vnet.name address_prefixes = ["10.10.2.0/24"] } resource "azurerm_public_ip" "vm_jump_pip" { name = "pip-jump" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name allocation_method = "Static" sku = "Standard" } resource "azurerm_network_interface" "vm_server_nic" { name = "nic-server" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "ipconfig-workload" subnet_id = azurerm_subnet.server_subnet.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_network_interface" "vm_jump_nic" { name = "nic-jump" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "ipconfig-jump" subnet_id = azurerm_subnet.jump_subnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.vm_jump_pip.id } } resource "azurerm_network_security_group" "vm_server_nsg" { name = "nsg-server" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_network_security_group" "vm_jump_nsg" { name = "nsg-jump" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name security_rule { name = "Allow-TCP" priority = 1000 direction = "Inbound" access = "Allow" protocol = "Tcp" source_port_range = "*" destination_port_range = "3389" source_address_prefix = "*" destination_address_prefix = "*" } } resource "azurerm_network_interface_security_group_association" "vm_server_nsg_association" { network_interface_id = azurerm_network_interface.vm_server_nic.id network_security_group_id = azurerm_network_security_group.vm_server_nsg.id } resource "azurerm_network_interface_security_group_association" "vm_jump_nsg_association" { network_interface_id = azurerm_network_interface.vm_jump_nic.id network_security_group_id = azurerm_network_security_group.vm_jump_nsg.id } resource "azurerm_windows_virtual_machine" "vm_server" { name = "server-vm" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location computer_name = "server" size = var.virtual_machine_size admin_username = var.admin_username admin_password = random_password.password.result network_interface_ids = [azurerm_network_interface.vm_server_nic.id] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" disk_size_gb = "128" } source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } boot_diagnostics { storage_account_uri = azurerm_storage_account.sa.primary_blob_endpoint } } resource "azurerm_windows_virtual_machine" "vm_jump" { name = "jump-vm" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location computer_name = "jumpbox" size = var.virtual_machine_size admin_username = var.admin_username admin_password = random_password.password.result network_interface_ids = [azurerm_network_interface.vm_jump_nic.id] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" disk_size_gb = "128" } source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } boot_diagnostics { storage_account_uri = azurerm_storage_account.sa.primary_blob_endpoint } } resource "azurerm_firewall_policy" "azfw_policy" { name = "azfw-policy" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location sku = var.firewall_sku_tier threat_intelligence_mode = "Alert" } resource "azurerm_firewall_policy_rule_collection_group" "prcg" { name = "prcg" firewall_policy_id = azurerm_firewall_policy.azfw_policy.id priority = 300 application_rule_collection { name = "appRc1" priority = 101 action = "Allow" rule { name = "appRule1" protocols { type = "Http" port = 80 } protocols { type = "Https" port = 443 } destination_fqdns = ["www.microsoft.com"] source_addresses = ["10.10.1.0/24"] } } network_rule_collection { name = "netRc1" priority = 200 action = "Allow" rule { name = "netRule1" protocols = ["TCP"] source_addresses = ["10.10.1.0/24"] destination_addresses = ["*"] destination_ports = ["8000", "8999"] } } } resource "azurerm_firewall" "fw" { name = "azfw" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name sku_name = "AZFW_VNet" sku_tier = var.firewall_sku_tier zones = ["1", "2", "3"] ip_configuration { name = "azfw-ipconfig" subnet_id = azurerm_subnet.azfw_subnet.id public_ip_address_id = azurerm_public_ip.pip_azfw.id } firewall_policy_id = azurerm_firewall_policy.azfw_policy.id } resource "azurerm_route_table" "rt" { name = "rt-azfw-eus" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name disable_bgp_route_propagation = false route { name = "azfwDefaultRoute" address_prefix = "0.0.0.0/0" next_hop_type = "VirtualAppliance" next_hop_in_ip_address = azurerm_firewall.fw.ip_configuration[0].private_ip_address } } resource "azurerm_subnet_route_table_association" "jump_subnet_rt_association" { subnet_id = azurerm_subnet.server_subnet.id route_table_id = azurerm_route_table.rt.id }
variables.tf
という名前のファイルを作成し、次のコードを挿入します。variable "resource_group_location" { type = string description = "Location for all resources." default = "eastus" } variable "resource_group_name_prefix" { type = string description = "Prefix for the Resource Group Name that's combined with a random id so name is unique in your Azure subcription." default = "rg" } variable "firewall_sku_tier" { type = string description = "Firewall SKU." default = "Premium" # Valid values are Standard and Premium validation { condition = contains(["Standard", "Premium"], var.firewall_sku_tier) error_message = "The SKU must be one of the following: Standard, Premium" } } variable "virtual_machine_size" { type = string description = "Size of the virtual machine." default = "Standard_D2_v3" } variable "admin_username" { type = string description = "Value of the admin username." default = "azureuser" }
outputs.tf
という名前のファイルを作成し、次のコードを挿入します。output "resource_group_name" { value = azurerm_resource_group.rg.name } output "firewall_name" { value = azurerm_firewall.fw.name }
Terraform を初期化する
terraform init
terraform init -upgrade
重要なポイント:
-
-upgrade
パラメーターは、必要なプロバイダー プラグインを、構成のバージョン制約に準拠する最新バージョンにアップグレードします。
Terraform実行計画を作成する
実行計画を作成するために terraform plan を実行してください。
terraform plan -out main.tfplan
重要なポイント:
-
terraform plan
コマンドは実行プランを作成しますが、実行はしません。 代わりに、それは設定ファイルで指定された設定を作成するために必要な手順を決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが期待と一致するかどうかを確認できます。 - 任意の
-out
パラメーターを使用すると、プランの出力ファイルを指定することができます。-out
パラメーターを使用すると、レビューしたプランがそのまま適用されることが保証されます。
Terraform 実行プランを適用する
クラウドインフラストラクチャに対して実行計画を適用するには、terraform apply を実行してください。
terraform apply main.tfplan
重要なポイント:
-
terraform apply
コマンドの例では、以前にterraform plan -out main.tfplan
を実行していることを前提としています。 -
-out
パラメーターに別のファイル名を指定した場合は、terraform apply
への呼び出しで同じファイル名を使用してください。 -
-out
パラメーターを使用しなかった場合は、パラメーターを指定せずにterraform apply
を呼び出します。
結果を確認してください。
Azure リソース グループ名を取得します。
resource_group_name=$(terraform output -raw resource_group_name)
ファイアウォール名を取得します。
firewall_name=$(terraform output -raw firewall_name)
JMESPath クエリを使用して az network firewall show を実行し、ファイアウォールの可用性ゾーンを表示します。
az network firewall show --name $firewall_name --resource-group $resource_group_name --query "{Zones:zones"}
リソースをクリーンアップする
Terraform を使用して作成されたリソースが不要になったら、次の手順を実行します。
terraform plan を実行し、
destroy
フラグを指定してください。terraform plan -destroy -out main.destroy.tfplan
重要なポイント:
-
terraform plan
コマンドは実行プランを作成しますが、実行はしません。 代わりに、それは設定ファイルで指定された設定を作成するために必要な手順を決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが期待と一致するかどうかを確認できます。 - 任意の
-out
パラメーターを使用すると、プランの出力ファイルを指定することができます。-out
パラメーターを使用すると、レビューしたプランがそのまま適用されることが保証されます。
-
terraform applyを実行して、実行プランを適用します。
terraform apply main.destroy.tfplan
Azure での Terraform のトラブルシューティング
Azure で Terraform を使用する際の一般的な問題をトラブルシュートする
次のステップ
次に、Azure Firewall のログを監視することができます。