How to Use for_each in Terraform: Syntax and Examples
In Terraform,
for_each lets you create multiple resource instances from a map or set of strings by looping over each item. You define for_each inside a resource block, and Terraform creates one resource per item with unique keys.Syntax
The for_each argument is used inside a resource or module block to create multiple instances based on a collection. It accepts a map or set of strings. Each instance gets a unique key from the collection.
Parts explained:
resource: Defines the resource type and name.for_each: The collection to loop over (map or set).each.key: The current key in the loop.each.value: The current value in the loop.
terraform
resource "aws_instance" "example" { for_each = { server1 = "10.0.0.1" server2 = "10.0.0.2" } ami = "ami-123456" instance_type = "t2.micro" private_ip = each.value tags = { Name = each.key } }
Example
This example creates two AWS EC2 instances with different private IPs and names using for_each. Terraform will create one instance per map entry.
terraform
provider "aws" { region = "us-east-1" } resource "aws_instance" "example" { for_each = { server1 = "10.0.1.10" server2 = "10.0.1.11" } ami = "ami-0c55b159cbfafe1f0" # Example AMI instance_type = "t2.micro" private_ip = each.value tags = { Name = each.key } }
Output
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when using for_each include:
- Using a list instead of a map or set of strings, which causes errors.
- Not using
each.keyoreach.valueproperly inside the resource. - Changing keys in the map between runs, which can cause resource replacement.
Always use stable keys in the map to avoid unexpected resource recreation.
terraform
/* Wrong: Using a list with for_each causes error */ resource "aws_instance" "wrong" { for_each = toset(["10.0.0.1", "10.0.0.2"]) # This is a set of strings, valid for for_each ami = "ami-123456" instance_type = "t2.micro" private_ip = each.value } /* Right: Use a map with keys */ resource "aws_instance" "right" { for_each = { server1 = "10.0.0.1" server2 = "10.0.0.2" } ami = "ami-123456" instance_type = "t2.micro" private_ip = each.value }
Quick Reference
| Term | Description |
|---|---|
| for_each | Loops over a map or set to create multiple resource instances. |
| each.key | The current key in the loop, used to identify the instance. |
| each.value | The current value in the loop, used for resource properties. |
| Map | A collection of key-value pairs, recommended for stable keys. |
| Set of strings | A collection of unique strings, can also be used with for_each. |
Key Takeaways
Use for_each with maps or sets of strings to create multiple resource instances.
Always reference each.key and each.value inside the resource block for dynamic values.
Avoid using lists directly with for_each to prevent errors.
Keep keys stable between runs to avoid resource replacement.
for_each helps write cleaner, reusable Terraform code for multiple similar resources.