### Initialize, Plan, and Apply Terraform Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_application_security_groups/_footer.md Standard commands to initialize a Terraform project, review planned changes, and apply them to your Azure environment. Ensure you have Terraform installed and are authenticated to your Azure subscription. ```bash terraform init terraform plan terraform apply ``` -------------------------------- ### Configure Azure Load Balancers Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Sets up Azure Load Balancers with different SKUs and frontend IP configurations. This example uses `for_each` to create multiple load balancers based on a map. ```terraform resource "azurerm_lb" "this" { for_each = local.load_balancers location = azurerm_resource_group.this.location name = each.value.sku resource_group_name = azurerm_resource_group.this.name sku = each.value.sku frontend_ip_configuration { name = "${each.key}-frontendip" private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv4" subnet_id = azurerm_subnet.this[1].id } } ``` -------------------------------- ### Configure Azure Application Gateway Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Sets up an Azure Application Gateway, a web traffic load balancer. This example defines backend pools, HTTP settings, frontend configurations, listeners, and routing rules. ```terraform resource "azurerm_application_gateway" "this" { location = azurerm_resource_group.this.location name = local.example["application_gateway"].name resource_group_name = azurerm_resource_group.this.name backend_address_pool { name = "${local.example["application_gateway"].name}-backend-pool" } backend_http_settings { cookie_based_affinity = "Disabled" name = "${local.example["application_gateway"].name}-backend-http" port = 80 protocol = "Http" request_timeout = 1 } frontend_ip_configuration { name = "${local.example["application_gateway"].name}-frontend-ip" public_ip_address_id = azurerm_public_ip.this["application_gateway"].id } frontend_port { name = "${local.example["application_gateway"].name}-frontend-port" port = 80 } gateway_ip_configuration { name = "example" subnet_id = azurerm_subnet.this[0].id } http_listener { frontend_ip_configuration_name = "${local.example["application_gateway"].name}-frontend-ip" frontend_port_name = "${local.example["application_gateway"].name}-frontend-port" name = "${local.example["application_gateway"].name}-listener-http" protocol = "Http" } request_routing_rule { http_listener_name = "${local.example["application_gateway"].name}-listener-http" name = "${local.example["application_gateway"].name}-rule" rule_type = "Basic" backend_address_pool_name = "${local.example["application_gateway"].name}-backend-pool" backend_http_settings_name = "${local.example["application_gateway"].name}-backend-http" priority = 25 } sku { name = "Standard_v2" tier = "Standard_v2" capacity = 2 } } ``` -------------------------------- ### Configure Dual Stack IPv4 and IPv6 on Network Interface Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/_header.md This example demonstrates configuring both IPv4 and IPv6 for dual stack networking on a single network interface. Each IP configuration requires a unique name and specifies its IP version. ```terraform module "nic-app-gateway-backend-pool" { source = "Azure/avm-res-compute-network/azurerm" resource_group_name = "myResourceGroup" location = "East US" network_interface_name = "myNetworkInterface" ip_configurations = { "dualstackIPv4config" = { name = "internal" subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet/subnets/myInternalSubnet" private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv4" primary = true } "dualstackIPv6config" = { name = "external" subnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet/subnets/myExternalSubnet" private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv6" } } } ``` -------------------------------- ### Terraform Configuration for Network Interface with All Settings Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md This HCL code defines local variables for example resources, configures Terraform requirements including providers for azurerm and random, and sets up the azurerm provider features. It also includes modules for random region selection and naming conventions. ```hcl locals { example = { standard_load_balancer = { name = "example-standard-lb" sku = "Standard" } gateway_load_balancer = { name = "example-gateway-lb" sku = "Gateway" } application_gateway = { name = "example-application-gateway" } network_interface = { name = "example-nic" } } load_balancers = { for key, value in local.example : key => value if strcontains(key, "load_balancer") } } terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } ``` -------------------------------- ### Terraform Configuration for Network Interface and NSG Association Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/associate_network_security_groups/README.md This HCL code defines the Terraform configuration to create a network interface and associate it with multiple network security groups. It includes provider requirements, resource group creation, virtual network and subnet setup, and the network interface module with NSG IDs. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } resource "azurerm_network_security_group" "this" { count = 3 location = azurerm_resource_group.this.location name = "example-${count.index}" resource_group_name = azurerm_resource_group.this.name } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "ipconfig1" = { name = "internal" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true network_security_group_ids = azurerm_network_security_group.this[*].id } ``` -------------------------------- ### Configure Network Interface with Associations Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Use this snippet to create a network interface with dynamic IP allocation, public IP, and associations for application gateway backend, load balancer backend, and NAT rules. It also includes DNS servers and telemetry settings. ```terraform module "nic" { source = "../../" ip_configurations = { "ipconfig1" = { name = "external" primary = true subnet_id = azurerm_subnet.this[1].id private_ip_address_allocation = "Dynamic" gateway_load_balancer_frontend_ip_configuration_id = azurerm_lb.this["gateway_load_balancer"].frontend_ip_configuration[0].id private_ip_address_version = "IPv4" public_ip_address_id = azurerm_public_ip.this["network_interface"].id } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name accelerated_networking_enabled = true application_gateway_backend_address_pool_association = { application_gateway_backend_address_pool_id = lookup({ for pool in azurerm_application_gateway.this.backend_address_pool : pool.name => pool.id }, "${local.example["application_gateway"].name}-backend-pool", null) ip_configuration_name = "external" } application_security_group_ids = azurerm_application_security_group.this[*].id dns_servers = ["10.0.1.5", "10.0.1.6", "10.0.1.7"] enable_telemetry = true internal_dns_name_label = "myinternaldnsnamelabel" ip_forwarding_enabled = true load_balancer_backend_address_pool_association = { "association1" = { load_balancer_backend_address_pool_id = azurerm_lb_backend_address_pool.standard.id ip_configuration_name = "external" } } nat_rule_association = { "association1" = { nat_rule_id = azurerm_lb_nat_rule.rdp.id ip_configuration_name = "external" } } network_security_group_ids = azurerm_network_security_group.this[*].id tags = { environment = "example" } } ``` -------------------------------- ### Create Azure Public IP Addresses Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Configures static Public IP addresses for use with other network resources. The `for_each` meta-argument allows creating multiple IPs based on a map. ```terraform resource "azurerm_public_ip" "this" { for_each = local.example allocation_method = "Static" location = azurerm_resource_group.this.location name = "${each.key}-pip" resource_group_name = azurerm_resource_group.this.name } ``` -------------------------------- ### Network Interface with Gateway Load Balancer Integration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Creates a network interface with telemetry enabled, configuring its IP configuration to use the Gateway Load Balancer's frontend IP. ```hcl module "nic" { source = "../../" ip_configurations = { "example" = { name = "internal" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.this.id gateway_load_balancer_frontend_ip_configuration_id = azurerm_lb.this.frontend_ip_configuration[0].id } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true } ``` -------------------------------- ### Terraform Configuration for Gateway Load Balancer Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Sets up the Terraform version, required providers (azurerm, random), and configures the Azure provider. Includes a feature to prevent accidental deletion of resource groups. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ``` -------------------------------- ### Azure Load Balancer Probe Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Configures an HTTP probe for the load balancer, specifying the port, interval, threshold, and request path. ```hcl resource "azurerm_lb_probe" "this" { loadbalancer_id = azurerm_lb.this.id name = "example" port = 80 interval_in_seconds = 5 probe_threshold = 2 protocol = "Http" request_path = "/" } ``` -------------------------------- ### Azure Gateway Load Balancer Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Creates an Azure Gateway Load Balancer with a dynamic frontend IP configuration using the previously defined subnet. ```hcl resource "azurerm_lb" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name sku = "Gateway" frontend_ip_configuration { name = "example" private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv4" subnet_id = azurerm_subnet.this.id } } ``` -------------------------------- ### Terraform Configuration for Network Interface with ASG Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_application_security_groups/README.md This Terraform configuration sets up the necessary network infrastructure, including resource groups, virtual networks, subnets, and application security groups, before creating a network interface and associating it with these security groups. It utilizes modules for naming and region selection to ensure CAF compliance and dynamic region assignment. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } resource "azurerm_application_security_group" "this" { count = 3 location = azurerm_resource_group.this.location name = "example-${count.index}" resource_group_name = azurerm_resource_group.this.name } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "example" = { name = "internal" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name application_security_group_ids = azurerm_application_security_group.this[*].id enable_telemetry = true } ``` -------------------------------- ### Configure Azure Load Balancer Probe Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Sets up a health probe for an Azure Load Balancer. This probe checks the health of backend instances by sending HTTP requests to a specified path and port. ```terraform resource "azurerm_lb_probe" "this" { loadbalancer_id = azurerm_lb.this["gateway_load_balancer"].id name = "example" port = 80 interval_in_seconds = 5 probe_threshold = 2 protocol = "Http" request_path = "/" } ``` -------------------------------- ### Create Azure Virtual Network and Subnets Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Configures an Azure Virtual Network with specified address spaces and creates multiple subnets within it. Subnets are used to segment the virtual network. ```terraform resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16", "2001:db8:abcd::/56"] } resource "azurerm_subnet" "this" { count = 2 address_prefixes = ["10.0.${count.index + 1}.0/24", "2001:db8:abcd:001${count.index + 2}::/64"] name = "example-${count.index + 1}" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } ``` -------------------------------- ### Terraform Configuration for Network Interface and Application Gateway Integration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_application_gateway_backend_pool/README.md This Terraform configuration sets up the necessary providers, modules for naming and region selection, and random integer generation for resource deployment. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } ## End of section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } ``` -------------------------------- ### Azure Subnet Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Creates a subnet within the virtual network, defining its address prefix and associating it with the resource group. ```hcl resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } ``` -------------------------------- ### Azure Virtual Network Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Defines a virtual network with a specified address space in the created resource group and location. ```hcl resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } ``` -------------------------------- ### Terraform Configuration for Dual-Stack Network Interface Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_IPv4_IPv6_dual_networking/README.md This Terraform configuration sets up the necessary Azure resources, including a resource group, virtual network, and subnet, to support a dual-stack network interface. It defines two IP configurations for the network interface: one for IPv4 and one for IPv6. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16", "fd00:db8:deca::/48"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.0.0/24", "fd00:db8:deca:deed::/64"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "dualstackIPv4config" = { name = "dsIP4Config" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv4" primary = true } "dualstackIPv6config" = { name = "dsIP6Config" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" private_ip_address_version = "IPv6" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true } ``` -------------------------------- ### Network Interface IP Configuration Object Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/README.md Defines the structure for an IP configuration within a network interface. Supports IPv4/IPv6, dynamic/static private IP allocation, and public IP association. ```hcl map(object({ name = string gateway_load_balancer_frontend_ip_configuration_id = optional(string, null) subnet_id = string private_ip_address_version = optional(string, "IPv4") private_ip_address_allocation = optional(string, "Dynamic") public_ip_address_id = optional(string, null) primary = optional(bool, null) private_ip_address = optional(string, null) })) ``` -------------------------------- ### Terraform Configuration for Network Interface and Load Balancer Association Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_standard_load_balancer_backend_pool/README.md This HCL code defines the necessary Terraform resources to create a virtual network, subnet, public IP, load balancer, and backend pools. It then provisions a network interface with multiple IP configurations and associates them with the load balancer's backend pools. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } resource "azurerm_public_ip" "this" { allocation_method = "Static" location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name } resource "azurerm_lb" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name frontend_ip_configuration { name = "primary" public_ip_address_id = azurerm_public_ip.this.id } } resource "azurerm_lb_backend_address_pool" "this" { count = 2 loadbalancer_id = azurerm_lb.this.id name = "example-${count.index}" } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "ipconfig1" = { name = "internal" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" primary = "true" } "ipconfig2" = { name = "external" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true load_balancer_backend_address_pool_association = { "association1" = { load_balancer_backend_address_pool_id = azurerm_lb_backend_address_pool.this[0].id ip_configuration_name = "internal" } "association2" = { load_balancer_backend_address_pool_id = azurerm_lb_backend_address_pool.this[1].id ip_configuration_name = "external" } } } ``` -------------------------------- ### Terraform AzureRM Application Gateway Configuration Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_application_gateway_backend_pool/README.md This snippet defines an Azure Application Gateway with two backend pools, HTTP settings, frontend configurations, and request routing rules. It's used to set up the gateway before associating network interfaces. ```terraform resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { count = 2 address_prefixes = ["10.0.${count.index + 1}.0/24"] name = "example_${count.index + 1}" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } resource "azurerm_public_ip" "this" { allocation_method = "Static" location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name } resource "azurerm_application_gateway" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name backend_address_pool { name = "${azurerm_virtual_network.this.name}-backend-pool-1" } backend_address_pool { name = "${azurerm_virtual_network.this.name}-backend-pool-2" } backend_http_settings { cookie_based_affinity = "Disabled" name = "${azurerm_virtual_network.this.name}-backend-http-80" port = 80 protocol = "Http" request_timeout = 20 } backend_http_settings { cookie_based_affinity = "Disabled" name = "${azurerm_virtual_network.this.name}-backend-http-8080" port = 8080 protocol = "Http" request_timeout = 20 } frontend_ip_configuration { name = "${azurerm_virtual_network.this.name}-frontend-ip" public_ip_address_id = azurerm_public_ip.this.id } frontend_port { name = "${azurerm_virtual_network.this.name}-frontend-port-80" port = 80 } frontend_port { name = "${azurerm_virtual_network.this.name}-frontend-port-8080" port = 8080 } gateway_ip_configuration { name = "example" subnet_id = azurerm_subnet.this[0].id } http_listener { frontend_ip_configuration_name = "${azurerm_virtual_network.this.name}-frontend-ip" frontend_port_name = "${azurerm_virtual_network.this.name}-frontend-port-80" name = "${azurerm_virtual_network.this.name}-listener-80" protocol = "Http" } http_listener { frontend_ip_configuration_name = "${azurerm_virtual_network.this.name}-frontend-ip" frontend_port_name = "${azurerm_virtual_network.this.name}-frontend-port-8080" name = "${azurerm_virtual_network.this.name}-listener-8080" protocol = "Http" } request_routing_rule { http_listener_name = "${azurerm_virtual_network.this.name}-listener-80" name = "${azurerm_virtual_network.this.name}-rule-1" rule_type = "Basic" backend_address_pool_name = "${azurerm_virtual_network.this.name}-backend-pool-1" backend_http_settings_name = "${azurerm_virtual_network.this.name}-backend-http-80" priority = 15 } request_routing_rule { http_listener_name = "${azurerm_virtual_network.this.name}-listener-8080" name = "${azurerm_virtual_network.this.name}-rule-2" rule_type = "Basic" backend_address_pool_name = "${azurerm_virtual_network.this.name}-backend-pool-2" backend_http_settings_name = "${azurerm_virtual_network.this.name}-backend-http-8080" priority = 25 } sku { name = "Standard_v2" tier = "Standard_v2" capacity = 2 } } ``` -------------------------------- ### Create Azure Load Balancer Backend Address Pools Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/configure_all_settings/README.md Defines backend address pools for Azure Load Balancers. This includes a standard pool and a pool with a VXLAN tunnel interface for gateway load balancers. ```terraform resource "azurerm_lb_backend_address_pool" "standard" { loadbalancer_id = azurerm_lb.this["standard_load_balancer"].id name = "example-standard-backend" } resource "azurerm_lb_backend_address_pool" "gateway" { loadbalancer_id = azurerm_lb.this["gateway_load_balancer"].id name = "example-gateway-backend" tunnel_interface { identifier = 901 port = 10801 protocol = "VXLAN" type = "External" } } ``` -------------------------------- ### Random Azure Region and Naming Modules Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Utilizes the Azure/regions module to select a random Azure region and the Azure/naming module for generating unique, CAF-compliant resource names. ```hcl module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } ``` -------------------------------- ### Azure Resource Group Creation Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Creates an Azure resource group using the selected random region and a unique name generated by the naming module. ```hcl resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } ``` -------------------------------- ### Terraform Configuration for Load Balancer NAT Rule Connection Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/connect_to_load_balancer_nat_rule/README.md This Terraform configuration sets up the necessary Azure resources including a resource group, virtual network, public IP, load balancer, and NAT rules. It then creates a network interface and associates it with the defined NAT rules. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } resource "azurerm_public_ip" "this" { allocation_method = "Static" location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name } resource "azurerm_lb" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name frontend_ip_configuration { name = "primary" public_ip_address_id = azurerm_public_ip.this.id } } resource "azurerm_lb_nat_rule" "rdp" { backend_port = 3389 frontend_ip_configuration_name = "primary" loadbalancer_id = azurerm_lb.this.id name = "RDPAccess" protocol = "Tcp" resource_group_name = azurerm_resource_group.this.name frontend_port = 3389 } resource "azurerm_lb_nat_rule" "ssh" { backend_port = 22 frontend_ip_configuration_name = "primary" loadbalancer_id = azurerm_lb.this.id name = "SSHAccess" protocol = "Tcp" resource_group_name = azurerm_resource_group.this.name frontend_port = 22 } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "ipconfig1" = { name = "rdp" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" primary = "true" } "ipconfig2" = { name = "ssh" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true nat_rule_association = { "association1" = { nat_rule_id = azurerm_lb_nat_rule.rdp.id ip_configuration_name = "rdp" } "association2" = { nat_rule_id = azurerm_lb_nat_rule.ssh.id ip_configuration_name = "ssh" } } } ``` -------------------------------- ### Azure Load Balancer Rule Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/add_to_gateway_load_balancer_backend_pool/README.md Defines a load balancing rule that directs all protocols from the frontend to the backend, linked to the configured probe. ```hcl resource "azurerm_lb_rule" "this" { backend_port = 0 frontend_ip_configuration_name = "example" frontend_port = 0 loadbalancer_id = azurerm_lb.this.id name = "example" protocol = "All" probe_id = azurerm_lb_probe.this.id } ``` -------------------------------- ### Terraform Configuration for Network Interface Module Source: https://github.com/azure/terraform-azurerm-avm-res-network-networkinterface/blob/main/examples/default/README.md This HCL code defines the Terraform configuration, including required providers, resource group, virtual network, subnet, and the network interface module. It sets up dynamic resource naming and region selection. ```hcl terraform { required_version = "~> 1.9" required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 3.116.0, < 5.0.0" } random = { source = "hashicorp/random" version = "~> 3.5" } } } provider "azurerm" { features { resource_group { prevent_deletion_if_contains_resources = false } } } ## Section to provide a random Azure region for the resource group # This allows us to randomize the region for the resource group. module "regions" { source = "Azure/regions/azurerm" version = "0.5.0" } # This allows us to randomize the region for the resource group. resource "random_integer" "region_index" { max = length(module.regions.regions) - 1 min = 0 } ## End of section to provide a random Azure region for the resource group # This ensures we have unique CAF compliant names for our resources. module "naming" { source = "Azure/naming/azurerm" version = "0.4.2" } # This is required for resource modules resource "azurerm_resource_group" "this" { location = module.regions.regions[random_integer.region_index.result].name name = module.naming.resource_group.name_unique } resource "azurerm_virtual_network" "this" { location = azurerm_resource_group.this.location name = "example" resource_group_name = azurerm_resource_group.this.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "this" { address_prefixes = ["10.0.1.0/24"] name = "example" resource_group_name = azurerm_resource_group.this.name virtual_network_name = azurerm_virtual_network.this.name } # Creating a network interface with a unique name, telemetry settings, and in the specified resource group and location module "nic" { source = "../../" ip_configurations = { "ipconfig1" = { name = "internal" subnet_id = azurerm_subnet.this.id private_ip_address_allocation = "Dynamic" } } location = azurerm_resource_group.this.location name = module.naming.network_interface.name_unique resource_group_name = azurerm_resource_group.this.name enable_telemetry = true } ```