In Terraform, the for_each loop is a powerful construct that allows you to iterate over a collection of values and perform actions on each item. This is particularly useful when you have two different maps and you want to loop over them simultaneously. In this article, we will explore how to achieve this using the for_each loop in Terraform.
Before we dive into the implementation, let's first understand what maps are in Terraform. A map is a collection of key-value pairs where each key is unique. It is similar to a dictionary in other programming languages. In Terraform, maps are commonly used to define variables and resources.
Now, let's say we have two maps: map1 and map2. Our goal is to loop over both maps simultaneously and perform some action on each item. Here's how we can achieve this:
// Define the maps
variable "map1" {
type = map
default = {
key1 = "value1"
key2 = "value2"
}
}
variable "map2" {
type = map
default = {
key3 = "value3"
key4 = "value4"
}
}
// Loop over the maps using for_each
resource "some_resource" "example" {
for_each = merge(var.map1, var.map2)
// Access the keys and values
key = each.key
value = each.value
// Perform some action on each item
// ...
}
In the above example, we define two maps: map1 and map2. We then use the merge function to combine both maps into a single map. This allows us to loop over both maps simultaneously using the for_each argument of the resource block.
Within the resource block, we can access the keys and values of each item using the each.key and each.value syntax. This allows us to perform some action on each item within the loop.
Now, let's see a practical example of how this can be used. Suppose we have two maps: aws_instances and tags. The aws_instances map contains the instance names as keys and the instance types as values. The tags map contains the instance names as keys and a list of tags as values. Our goal is to create an AWS EC2 instance for each item in the aws_instances map and assign the corresponding tags from the tags map. Here's how we can achieve this:
// Define the maps
variable "aws_instances" {
type = map
default = {
instance1 = "t2.micro"
instance2 = "t3.micro"
}
}
variable "tags" {
type = map
default = {
instance1 = ["tag1", "tag2"]
instance2 = ["tag3", "tag4"]
}
}
// Loop over the maps using for_each
resource "aws_instance" "example" {
for_each = var.aws_instances
// Set the instance type
instance_type = each.value
// Set the tags
tags = {
Name = each.key
Tags = var.tags[each.key]
}
// ...
}
In the above example, we define two maps: aws_instances and tags. We then use the for_each argument of the aws_instance resource block to loop over the aws_instances map.
Within the resource block, we set the instance_type attribute to the value of each.value, which represents the instance type from the aws_instances map.
We also set the tags attribute to a map that contains the instance name as the Name tag and the list of tags from the tags map as the Tags tag. We access the list of tags using var.tags[each.key], where each.key represents the instance name.
This way, we can create an AWS EC2 instance for each item in the aws_instances map and assign the corresponding tags from the tags map.
Using the for_each loop in Terraform, we can easily iterate over two different maps simultaneously and perform actions on each item. This allows us to automate complex infrastructure setups and manage resources more efficiently.
The for_each loop in Terraform is a powerful construct that allows us to loop over two different maps simultaneously. By combining the maps using the merge function and using the for_each argument of the resource block, we can perform actions on each item within the loop. This enables us to automate infrastructure setups and manage resources more efficiently.
References
| Link | Description |
|---|---|
| Terraform Documentation - for_each | Terraform documentation on the for_each meta-argument |
| Terraform Documentation - merge | Terraform documentation on the merge function |