Error: Invalid template interpolation value: "Cannot include the given value in a string template"
Fixes Terraform's "Error: Invalid template interpolation value ... Cannot include the given value in a string template", caused by interpolating null or non-string values in ${ } sequences. Use when plan fails on a template while validate passed. Guard with coalesce or a conditional; not for function-argument type errors.
TL;DR
You put a null (or a non-string like an object) inside ${ ... }. String templates only accept strings. Guard with a default: "${var.name != null ? var.name : ""}" or coalesce(var.name, ""), then re-plan.
The error
Error: Invalid template interpolation value
on main.tf line 6, in locals:
6: templated = "foo=${var.foo}"
├────────────────
│ var.foo is null
The expression result is null. Cannot include a null value in a string
template.Another common shape:
Error: Invalid template interpolation value: Cannot include the given value in a string template: string required.Steps to fix
- Find the interpolation the error names and check the value: the
├line tells you it is null or an object.
- Expected: you see
var.foo is nullor similar.
- Decide what the string should be when the value is absent. Usual fixes:
templated = "foo=${coalesce(var.foo, "default")}"or for preconditions/error messages, render nulls explicitly:
error_message = "tag must match spec (got ${coalesce(local.user_tag, "[unset]")})."- Expected: the template always receives a string.
- If you interpolated a whole object (
"${module.foo}"), pick the specific attribute instead (module.foo.id).
- Expected: scalar value in the template.
- Run
terraform validatethenplan.
- Expected: both pass.
When to use this
plan/validatefails withInvalid template interpolation valueon a${ }sequence, very often insideerror_messagestrings of preconditions or intemplatefilevars.
When NOT to use this
Invalid function argumentis a function-call type problem, not a template problem.terraform validatedoes NOT catch this (it reports success while plan fails), so always re-plan after fixing.
Compatibility
- All Terraform versions. Note:
validatepasses whileplanfails for this error, a known sharp edge.
Root cause
String templates require every interpolated value to be a string (or null-free convertible). Terraform renders error_message before evaluating the condition, so even a passing precondition dies if its message interpolates a null. Optional variables default to null, which is why this bites optional inputs so often.
Edge cases
nullable = falseon the variable prevents null at the boundary, but the error can still come from locals or data sources.join()with a null in the list fails the same way; filter nulls first.- In
templatefile, pass pre-stringified values (tostring()) rather than raw objects.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.