Fix Terraform Error - Invalid Template Interpolation
Fix the Terraform invalid template interpolation error when embedding complex types in strings. Covers jsonencode, join, format, and type conversion patterns.
Troubleshooting
Fix the Terraform invalid function argument error. Covers type mismatches for join, lookup, cidrsubnet, file, and other built-in functions with examples.
A Terraform built-in function received the wrong type or invalid value. Check the function's expected parameter types in the docs, use type conversion functions (tostring, tolist, tomap), and test expressions in terraform console before using them in config.
Error: Invalid function argument
on main.tf line 5, in resource "aws_instance" "web":
5: subnet_id = element(var.subnets, 0)
Invalid value for "list" parameter: list of string required.Error: Invalid function argument
on main.tf line 3, in locals:
3: cidr = cidrsubnet(var.vpc_cidr, 8, "one")
Invalid value for "netnum" parameter: a whole number is required.# BAD — join expects a list, got a string
join(", ", "hello")
# BAD — tonumber expects a string, got a list
tonumber(["1", "2"])
# BAD — cidrsubnet expects a number, got a string
cidrsubnet("10.0.0.0/16", 8, "one")# BAD — length() can't handle null
length(null)
# BAD — upper() can't handle null
upper(var.name) # when var.name is null# BAD — lookup takes 2-3 args
lookup(var.tags) # Missing key argument
# BAD — format needs matching verbs and args
format("%s %s", "one") # 2 verbs, 1 argCommon functions and their expected types:
| Function | Parameters | Example |
|---|---|---|
join(sep, list) | string, list(string) | join(", ", ["a", "b"]) |
split(sep, str) | string, string | split(",", "a,b,c") |
lookup(map, key, default) | map, string, any | lookup(var.tags, "Name", "") |
element(list, index) | list, number | element(var.subnets, 0) |
cidrsubnet(prefix, bits, num) | string, number, number | cidrsubnet("10.0.0.0/16", 8, 1) |
file(path) | string | file("${path.module}/data.txt") |
templatefile(path, vars) | string, map | templatefile("tpl.tftpl", {k="v"}) |
length(value) | list/map/string | length(var.subnets) |
format(spec, values...) | string, any... | format("Hello %s", var.name) |
# Convert string to number
cidrsubnet(var.vpc_cidr, 8, tonumber(var.subnet_number))
# Convert set to list (element requires list)
element(tolist(var.availability_zones), 0)
# Convert number to string
join("-", [var.prefix, tostring(var.count)])
# Convert map to list of values
join(", ", values(var.tags))# Use coalesce to provide defaults for null
length(coalesce(var.subnets, []))
upper(coalesce(var.name, "default"))
# Or use try()
try(length(var.subnets), 0)terraform console
> join(", ", ["a", "b", "c"])
"a, b, c"
> cidrsubnet("10.0.0.0/16", 8, 1)
"10.0.1.0/24"
> lookup({Name = "web"}, "Name", "unknown")
"web"
> element(["us-east-1a", "us-east-1b"], 0)
"us-east-1a"
> format("Instance %s in %s", "i-123", "us-east-1")
"Instance i-123 in us-east-1"# WRONG: file() with variable (must be known at plan time)
file(var.config_path) # ERROR if path is computed
# RIGHT: use local path
file("${path.module}/config.json")
# WRONG: regex without proper escaping
regex("192.168.\d+.\d+", var.ip)
# RIGHT: double-escape backslashes
regex("192\\.168\\.\\d+\\.\\d+", var.ip)
# WRONG: lookup on a non-map
lookup(var.subnet_ids, "private") # var.subnet_ids is a list
# RIGHT: use index for lists
var.subnet_ids[0]terraform console to check)coalesce() or try() wrapperfile(), is the path known at plan time?terraform console firstterraform console — instant feedback without running plantry() for fallbacks — handles null and missing attributes gracefullyterraform validate — catches function argument errors without API callsInvalid function argument errors mean you passed the wrong type or value to a Terraform function. Check the function's expected parameters, use type conversion functions when needed, handle nulls with coalesce() or try(), and test expressions in terraform console before using them in your config.
Fix the Terraform invalid template interpolation error when embedding complex types in strings. Covers jsonencode, join, format, and type conversion patterns.
Fix the Terraform inconsistent conditional result types error. Covers type conversion, null handling, tostring, tolist, and splitting complex conditionals.
Fix the Terraform 'Backend configuration changed' error. Migrate state between backends (local to S3, S3 to S3), resolve lock conflicts
Fix Terraform provider version conflicts between modules, lock files, and constraint mismatches. Resolve dependency lock errors, upgrade providers safely