This error occurs when you try to include a non-string value (like a list or tuple) directly in a string template using ${} interpolation syntax. Use the join() function or remove unnecessary interpolation syntax to fix it.
Terraform template interpolation has strict type requirements. When you use the ${} syntax to embed values in strings, Terraform expects those values to be strings or types that can be safely converted to strings. If you try to interpolate a list, tuple, map, or null value directly into a template, Terraform throws this error because these complex types cannot be automatically converted to strings. This commonly occurs after upgrading to Terraform v0.12+, which enforces stricter type checking than older versions. The error message may include details like "Cannot include the given value in a string template: string required" or "The expression result is null. Cannot include a null value in a string template."
Review your Terraform configuration files (.tf files) and look for the error message location. The error will point to a specific line and resource. Common patterns to look for include:
# Wrong - trying to interpolate a list directly
resource "example" "test" {
config = "\${aws_subnet.example.*.id}"
}
# Wrong - using a null value in string
config = "\${var.optional_value}"
# Wrong - interpolating a map directly
output = "\${local.settings}"Check the error message in your terraform output to see exactly which line and attribute has the problem.
If you're trying to interpolate a list or splat expression, use the join() function to convert it to a comma-separated string:
# Right - join the list into a string
resource "example" "test" {
config = join(",", aws_subnet.example.*.id)
}
# Right - or if you need it in a template, use join inside
message = "Subnets: \${join(", ", aws_subnet.example.*.id)}"The join() function takes a delimiter (first argument) and a list (second argument) and combines them into a single string.
If the error involves null values, use coalesce() to provide a default, or use a conditional expression:
# Right - provide a default if value is null
config = coalesce(var.optional_value, "default")
# Right - use conditional expression
config = var.optional_value != null ? var.optional_value : "default"
# Right - or simply omit the value if it's optional
optional_setting = var.optional_valuecoalesce() returns the first non-null argument, making it useful for providing defaults.
If you're using Terraform 0.12 or newer, remove the ${} wrapper when a value is already an expression. This is a deprecated pattern from v0.11:
# Old style (Terraform 0.11) - now deprecated
instance_id = "\${aws_instance.example.id}"
# New style (Terraform 0.12+) - preferred
instance_id = aws_instance.example.idIn Terraform 0.12+, you can reference values directly without the interpolation syntax. Only use ${} when you actually need to embed an expression inside a string.
If you need to convert maps, objects, or other complex types to strings for templates, use jsonencode() or yamlencode():
# Right - convert map to JSON string
config = jsonencode(var.settings)
# Right - convert to YAML if needed
yaml_config = yamlencode(var.settings)
# Right - combine with template if needed
message = "Config: \${jsonencode(local.settings)}"These functions handle the serialization of complex types safely.
Type Safety in Terraform v0.12+: Terraform v0.12 introduced stricter type checking compared to v0.11. The interpolation syntax ${} was overloaded in v0.11 to handle both string embedding and general expressions, but v0.12 separated these concerns. Now, ${} should only be used when you need to embed an expression within a string literal.
Escaping Dollar Signs: If you need a literal ${} in your output (for example, in a CloudFormation template or environment variable), use $${} (double dollar sign) to escape it:
config = "Use $${var_name} for substitution" # Outputs: Use ${var_name}Performance Consideration: When building complex strings from many interpolations, consider using templatefile() instead for better readability and performance:
config = templatefile("\${path.module}/config.tpl", {
instances = aws_instance.example.*.id
})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