在本快速入门中,你将使用 Terraform 从公共 IP 前缀部署具有多个公共 IP 地址的 Azure 防火墙。 部署的防火墙具有 NAT 规则收集规则,这些规则允许通过 RDP 连接与两个 Windows Server 2019 虚拟机进行连接。
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 HCL 语法允许你指定云提供商(如 Azure)和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证更改后,应用执行计划来部署基础结构。
若要详细了解具有多个公共 IP 地址的 Azure 防火墙,请参阅使用 Azure PowerShell 部署具有多个公共 IP 地址的 Azure 防火墙。
在这篇文章中,你将学会如何:
- 使用random_pet创建随机值(要在资源组名称中使用)
- 使用 random_password 为 Windows VM 创建随机密码
- 使用 azurerm_resource_group 创建 Azure 资源组
- 使用 azurerm_public_ip_prefix 创建 Azure 公共 IP 前缀
- 使用 azurerm_public_ip 创建 Azure 公共 IP
- 使用 azurerm_virtual_network 创建 Azure 虚拟网络
- 使用 azurerm_subnet 创建 Azure 子网
- 使用 azurerm_network_interface 创建网络接口
- 使用azurerm_network_security_group创建网络安全组(以包含网络安全规则列表)
- 使用 azurerm_network_interface_security_group_association 在网络接口和网络安全组之间创建关联
- 使用 azurerm_windows_virtual_machine 创建 Windows 虚拟机
- 使用 azurerm_firewall_policy 创建 Azure 防火墙策略
- 使用 azurerm_firewall_policy_rule_collection_group 创建 Azure 防火墙策略规则收集组
- 使用 azurerm_firewall 创建 Azure 防火墙
- 使用 azurerm_route_table 创建路由表
- 使用 - azurerm_subnet_route_table_association 在路由表和子网之间创建关联
先决条件
实现 Terraform 代码
注释
本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
providers.tf的文件并插入下列代码:terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features { virtual_machine { delete_os_disk_on_deletion = true skip_shutdown_and_force_delete = true } } }创建名为
main.tf的文件并插入下列代码:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "random_password" "password" { count = 2 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_prefix" "pip_prefix" { name = "pip-prefix" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name sku = "Standard" prefix_length = 31 } resource "azurerm_public_ip" "pip_azfw" { name = "pip-azfw" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name sku = "Standard" allocation_method = "Static" public_ip_prefix_id = azurerm_public_ip_prefix.pip_prefix.id } resource "azurerm_public_ip" "pip_azfw_2" { name = "pip-azfw-1" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name sku = "Standard" allocation_method = "Static" public_ip_prefix_id = azurerm_public_ip_prefix.pip_prefix.id } 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" "backend_subnet" { name = "subnet-backend" 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_network_interface" "backend_nic" { count = 2 name = "nic-backend-${count.index + 1}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "ipconfig-backend-${count.index + 1}" subnet_id = azurerm_subnet.backend_subnet.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_network_security_group" "backend_nsg" { name = "nsg-backend" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name security_rule { name = "RDP" priority = 300 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_backend_nsg_association" { count = 2 network_interface_id = azurerm_network_interface.backend_nic[count.index].id network_security_group_id = azurerm_network_security_group.backend_nsg.id } resource "azurerm_windows_virtual_machine" "vm_backend" { count = 2 name = "vm-backend-${count.index + 1}" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location size = var.virtual_machine_size admin_username = var.admin_username admin_password = random_password.password[count.index].result network_interface_ids = [azurerm_network_interface.backend_nic[count.index].id] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2019-Datacenter" version = "latest" } } 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" "policy_rule_collection_group" { name = "RuleCollectionGroup" firewall_policy_id = azurerm_firewall_policy.azfw_policy.id priority = 300 application_rule_collection { name = "web" priority = 100 action = "Allow" rule { name = "wan-address" protocols { type = "Http" port = 80 } protocols { type = "Https" port = 443 } destination_fqdns = ["getmywanip.com"] source_addresses = ["*"] } rule { name = "baidu" protocols { type = "Http" port = 80 } protocols { type = "Https" port = 443 } destination_fqdns = ["www.baidu.com"] source_addresses = ["10.10.1.0/24"] } rule { name = "wupdate" protocols { type = "Http" port = 80 } protocols { type = "Https" port = 443 } destination_fqdn_tags = ["WindowsUpdate"] source_addresses = ["*"] } } nat_rule_collection { name = "Coll-01" action = "Dnat" priority = 200 rule { name = "rdp-01" protocols = ["TCP"] translated_address = "10.10.1.4" translated_port = "3389" source_addresses = ["*"] destination_address = azurerm_public_ip.pip_azfw.ip_address destination_ports = ["3389"] } rule { name = "rdp-02" protocols = ["TCP"] translated_address = "10.10.1.5" translated_port = "3389" source_addresses = ["*"] destination_address = azurerm_public_ip.pip_azfw.ip_address destination_ports = ["3389"] } } } 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 ip_configuration { name = "azfw-ipconfig" subnet_id = azurerm_subnet.azfw_subnet.id public_ip_address_id = azurerm_public_ip.pip_azfw.id } ip_configuration { name = "azfw-ipconfig-2" public_ip_address_id = azurerm_public_ip.pip_azfw_2.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 = "azfw" address_prefix = "0.0.0.0/0" next_hop_type = "VirtualAppliance" next_hop_in_ip_address = "10.10.0.4" } } resource "azurerm_subnet_route_table_association" "jump_subnet_rt_association" { subnet_id = azurerm_subnet.backend_subnet.id route_table_id = azurerm_route_table.rt.id }创建名为
variables.tf的文件并插入下列代码:variable "resource_group_location" { type = string description = "Location for all resources." default = "chinaeast" } 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 "backend_admin_password" { sensitive = true value = azurerm_windows_virtual_machine.vm_backend[*].admin_password }
初始化 Terraform
运行 terraform init,将 Terraform 部署进行初始化。 此命令下载 Azure 提供程序,以便管理您的 Azure 资源。
terraform init -upgrade
要点:
- 参数
-upgrade可将必要的提供程序插件升级到符合配置版本约束的最新版本。
创建 Terraform 执行计划
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
应用 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)运行 az network ip-group list 以显示两个新的 IP 组。
az network ip-group list --resource-group $resource_group_name
清理资源
不再需要通过 Terraform 创建的资源时,请执行以下步骤:
运行 terraform plan 并指定
destroy标志。terraform plan -destroy -out main.destroy.tfplan运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan