The Invalid value for input variable error occurs when you pass a value that doesn't match the expected type or fails validation rules defined for a Terraform input variable. Fix it by ensuring your values match the variable's type constraints and any custom validation conditions.
This error occurs when Terraform validates input variables during plan or apply and finds that the provided value doesn't match the variable's declared type or fails custom validation rules. Terraform input variables have strict type constraints. When you provide a value to a variable, Terraform checks that it matches the declared type (string, number, bool, list, map, object, etc.). Additionally, variables can include validation blocks that enforce business logic constraints. If either check fails, Terraform rejects the value with this error. This validation happens early in the Terraform workflow, before any infrastructure planning occurs, helping catch configuration errors immediately.
First, find the variable definition and verify what type it expects. Look at your variables.tf file or wherever the variable is declared:
variable "environment" {
type = string # String type expected
}
variable "instance_count" {
type = number # Number type expected
}
variable "tags" {
type = map(string) # Map with string values
}The type parameter determines what kind of value the variable accepts. Common types are string, number, bool, list, map, and object.
Ensure the values you're providing match the variable's type:
# WRONG - providing a number when string is expected
variable "environment" {
type = string
}
environment = 123 # Error! Should be a string
# CORRECT - provide a string
environment = "production"
# WRONG - mixing types in a list
variable "allowed_ports" {
type = list(number)
}
allowed_ports = [80, "443", 8080] # Error! "443" is a string
# CORRECT - all numbers
allowed_ports = [80, 443, 8080]
# WRONG - mixing types in a map
variable "settings" {
type = map(string)
}
settings = {
name = "MyApp"
count = 5 # Error! 5 is a number, not a string
}
# CORRECT - all values are strings
settings = {
name = "MyApp"
count = "5"
}If using a .tfvars file, ensure the syntax is correct for the variable type:
# Strings must be quoted
environment = "dev"
region = "us-east-1"
# Numbers are unquoted
replicas = 3
port = 8080
# Lists use square brackets with commas
availability_zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
# Maps use curly braces with key = value pairs
labels = {
team = "platform"
env = "staging"
}
# Boolean values are lowercase
enable_monitoring = trueCommon mistakes: forgetting quotes around strings, using commas instead of proper list syntax, or mixing types in maps.
If the variable has validation blocks, check that your value satisfies the conditions:
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be dev, staging, or prod."
}
}
# Valid values: "dev", "staging", "prod"
# Invalid: "production", "test", "development"
environment = "prod" # OK
environment = "development" # ERROR - not in allowed listReview the condition statement to understand what values are allowed, then verify your input satisfies it.
When using -var flags, ensure proper syntax for complex types:
# String
terraform apply -var="name=MyApp"
# Number
terraform apply -var="count=5"
# Boolean
terraform apply -var="enabled=true"
# List (use JSON syntax)
terraform apply -var='ports=[80,443,8080]'
# Map (use JSON syntax)
terraform apply -var='labels={"team":"platform","env":"prod"}'
# Object (use JSON syntax)
terraform apply -var='config={"name":"app","replicas":3}'For complex types with special characters, wrap the value in single quotes and use valid JSON syntax.
Use terraform validate to catch type errors before planning:
terraform validateThis command checks variable type constraints and validation rules without requiring actual infrastructure. It provides clear error messages if values don't match the expected types.
Create a test .tfvars file to verify your variables work correctly:
# Create a test file
cat > test.tfvars <<EOF
environment = "dev"
instance_count = 3
enabled = true
tags = {
team = "platform"
managed = "true"
}
EOF
# Test with terraform plan
terraform plan -var-file="test.tfvars"If the plan succeeds, your variable values are correct. The error messages will point to exactly what's wrong if validation fails.
Type coercion in Terraform is limited. While Terraform automatically converts between number and string in some contexts, map and list types have stricter requirements. All elements in a map must have the same type, and all elements in a list must be homogeneous.
For complex object types, ensure every field matches the schema defined in the variable's type declaration. Omitting optional fields is allowed, but providing a field with the wrong type will fail validation.
Terraform 1.9+ enhanced variable validation capabilities, allowing references to data sources in validation conditions. If using older versions, validation conditions are limited to comparing against hardcoded values.
When validating module inputs, the error will reference the module call site. Check both the variable definition in the module AND the value passed from the calling module to identify the type mismatch.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform