In this Terraform example, I explored two approaches to working with map variables. Firstly, I used a for loop to iterate over the key-value pairs in the address_prefixes map, extracting the prefix values and enclosing the loop within square brackets [] to generate list from map.
Alternatively, I discovered that I can leverage the values() function to extract the list of values from the map and directly assign it to an attribute that expects a list value, streamlining the process.
Additionally, I found that I can also use a list variable directly to supply values, providing another convenient option for working with collections in Terraform.
provider "azurerm" { features {} subscription_id = "9exxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx" } variable "subnet" { type = object({ name = string virtual_network_name = string resource_group_name = string address_prefixes = map(string) }) default = { name = "test_subnet" virtual_network_name = "vcloud_lab_global_vnet01" resource_group_name = "vcloud-lab.com" address_prefixes = { subnet1 = "10.0.5.0/24" subnet2 = "10.0.6.0/24" } } } # variable "address_prefixes" { # type = list(string) # default = ["10.0.1.0/24", "10.0.2.0/24"] # } resource "azurerm_subnet" "example" { name = var.subnet.name virtual_network_name = var.subnet.virtual_network_name resource_group_name = var.subnet.resource_group_name address_prefixes = [ for subnet_name, prefix in var.subnet.address_prefixes : prefix ] #values(var.subnet.address_prefixes) #var.address_prefixes }
Below is the plan output of azurerm_subnet, note the address_prefixes, It all looks good.
terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_subnet.example will be created + resource "azurerm_subnet" "example" { + address_prefixes = [ + "10.0.5.0/24", + "10.0.6.0/24", ] + default_outbound_access_enabled = true + id = (known after apply) + name = "test_subnet" + private_endpoint_network_policies = "Disabled" + private_link_service_network_policies_enabled = true + resource_group_name = "vcloud-lab.com" + virtual_network_name = "vcloud_lab_global_vnet01" } Plan: 1 to add, 0 to change, 0 to destroy. ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
Useful Articles
Terraform variable multiple validation advanced blocks example
Terraform variable type list with for_each for loop examples
Terraform convert single string to list or set
Terraform workspaces with example
Terraform map of object for loop with if condition example
Terraform for_each for loop list of object without count example
Hashicorp Terraform map and object inside module and variable example
Terraform one module deploy null or multiple resources based on input
Terraform A reference to a resource type must be followed by at least one attribute access, specifying the resource name
Terraform fore_each for loop filter with if condition example
Terraform remote-exec provisioner with ssh connection in null_resource
Terraform count vs for_each for examples with map of objects
Terraform one module deploy null or multiple resources based on input (nested for loop) Example of Terraform functions flatten() and coalesce()
Terraform Azure Create Private Endpoint to existing Storage Account with Custom Private DNS zone record link
Creating a Private Endpoint for Azure Storage Account with required sub services using Terraform Example Terraform functions of lookup() and lower()
Using element function with count meta argument example Terraform Azure subnets Example Terraform functions of element(), count() and sum()
Terraform create Azure Virtual Network subnets from map of object and show name in the header Header name change in the output
Creating a Private Endpoint for Azure Storage Account with Terraform example 2 Example of for_each with toset() function
Creating a Private Endpoint for Azure Storage Account with Terraform example 3