Terraform Failing with Invalid for_each Argument / The Given "for_each" Argument Value is Unsuitable
TLDR
The "Invalid for_each argument" error in Terraform occurs when the value provided to for_each is not a valid map or set. Learn how to troubleshoot and resolve this issue.
Understanding the Error
The for_each meta-argument in Terraform requires a map or a set of strings. If the provided value does not meet these criteria, Terraform will throw an error like:
Error: Invalid for_each argument
The given "for_each" argument value is unsuitable.
This error often occurs due to:
- Using a list instead of a set.
- Providing a value that is not iterable.
- Incorrect data types.
Common Scenarios and Fixes
As you work with Terraform, you may encounter the "Invalid for_each argument" error. This typically happens when the value provided to for_each is not a valid map or set. Here are some common scenarios and how to fix them:
1. Using a List Instead of a Set
Terraform requires a set or map for for_each. If you use a list, you need to convert it to a set:
variable "instances" {
default = ["web", "db", "cache"]
}
resource "aws_instance" "example" {
for_each = toset(var.instances)
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
Here, toset converts the list to a set, making it compatible with for_each.
2. Using a Map for Key-Value Pairs
If you need to iterate over key-value pairs, use a map:
variable "instance_types" {
default = {
web = "t2.micro",
db = "t2.small",
cache = "t2.medium"
}
}
resource "aws_instance" "example" {
for_each = var.instance_types
ami = "ami-12345678"
instance_type = each.value
tags = {
Name = each.key
}
}
3. Handling Null or Empty Values
If the value for for_each is null or empty, Terraform will throw an error. Use a default value to handle this:
variable "instances" {
default = null
}
resource "aws_instance" "example" {
for_each = var.instances != null ? toset(var.instances) : {}
ami = "ami-12345678"
instance_type = "t2.micro"
}
4. Debugging Data Types
Use terraform console to inspect the data type of your variable:
> var.instances
> toset(var.instances)
This helps identify issues with the input data.
Best Practices
- Validate Input Data: Use
validationblocks in variables to ensure correct data types. - Use Default Values: Provide sensible defaults to avoid null or empty values.
- Test Iterations: Use
terraform consoleto test yourfor_eachlogic before applying changes.
By understanding and addressing the root cause of the "Invalid for_each argument" error, you can create more robust and error-free Terraform configurations.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
How to for_each through a list(objects) in Terraform
Learn how to use the for_each construct in Terraform to iterate through lists of objects and dynamically create resources.
Terraform State Management
What is Terraform state, why is it important, and how do you manage state in a team environment?
mid
Terraform Repository Structure Checklist
Best practices for organizing and structuring your Terraform projects for maintainability and scalability.
30-45 minutes