Last Updated on August 7, 2025 by Arnav Sharma
Terraform, developed by HashiCorp, is an open-source tool that enables developers to define and provision infrastructure using a high-level configuration language. It simplifies managing infrastructure as code, allowing efficient deployment and maintenance across various cloud platforms such as AWS, Azure, and Google Cloud.
Understanding the Flatten Function
In Terraform, the flatten function is a powerful tool used to manipulate complex data structures. This function simplifies nested lists by transforming them into a single flat list, making data management more efficient and code more readable.
The Purpose of the Flatten Function
The flatten function in Terraform takes a list and replaces any nested lists within it with their individual elements. This built-in function is particularly useful for developers dealing with nested data structures in their Terraform configurations.
Working with Lists in Terraform
The primary role of the flatten function is to handle lists. Whether you’re working with a simple list of values or more complex nested lists, the flatten function ensures that all elements are brought to the same level, resulting in a single, flat list.
Structuring Your Terraform Configuration
Understanding and manipulating data structures is crucial in Terraform. By using the flatten function, developers can simplify complex structures, making it easier to manage resources and configurations.
Applying the Flatten Function in Resource Blocks
A typical use case for the flatten function is within resource blocks. When defining resources that require lists of items, the flatten function can streamline these lists, reducing complexity and potential errors in your Terraform code.
Step-by-Step Guide to Using the Flatten Function
Here’s a step-by-step guide to using the flatten function in Terraform, particularly with Azure:
Define Your Data Structure
Start by defining a nested list in your Terraform configuration.
variable "nested_list" {
type = list(list(string))
default = [["subnet1", "subnet2"], ["subnet3", "subnet4"]]
}
Use the Flatten Function
Apply the flatten function to the nested list.
locals {
flat_list = flatten(var.nested_list)
}
Output the Result
Output the flattened list to see the transformation.
output "flattened_list" {
value = local.flat_list
}
Practical Example with Azure
Let’s take a practical example where you might need to create multiple subnets in an Azure Virtual Network (VNet).
Define Variables
First, define a nested list of subnets.
variable "subnets" {
type = list(list(object({
name = string
address = string
})))
default = [
[
{ name = "subnet1", address = "10.0.1.0/24" },
{ name = "subnet2", address = "10.0.2.0/24" }
],
[
{ name = "subnet3", address = "10.0.3.0/24" },
{ name = "subnet4", address = "10.0.4.0/24" }
]
]
}
Flatten the Nested List
Flatten the nested list to get a single list of subnets.
locals {
flat_subnets = flatten(var.subnets)
}
Create Subnets in Azure
Use the flattened list to create subnets in an Azure VNet.
resource "azurerm_virtual_network" "example" {
name = "example-vnet"
address_space = ["10.0.0.0/16"]
location = "East US"
resource_group_name = "example-resources"
}
resource "azurerm_subnet" "example" {
count = length(local.flat_subnets)
name = local.flat_subnets[count.index].name
address_prefix = local.flat_subnets[count.index].address
resource_group_name = azurerm_virtual_network.example.resource_group_name
virtual_network_name = azurerm_virtual_network.example.name
}