The "Invalid expression" error in Terraform occurs when the HCL parser encounters malformed syntax in your configuration. This is typically caused by incorrect interpolation syntax, missing or extra brackets, or incomplete expressions.
This error occurs when Terraform's HCL2 parser encounters invalid syntax that it cannot interpret. The parser expects valid HCL syntax but finds something unexpected instead. This can happen in variable references, string interpolations, resource arguments, or computed values. The error message typically includes a line number pointing to where Terraform stopped parsing, helping you locate the exact syntax problem. Common contexts where this appears include variable interpolation (missing string interpolation syntax), expression assignments (incomplete values), and function calls (incorrect syntax).
The error output includes a line number (e.g., "line 42"). Open your configuration file and navigate to that line to identify the problem.
Use the built-in formatter to catch common syntax problems:
terraform fmt main.tfThe formatter will rewrite your configuration to correct style. Review the changes.
Ensure variable references use proper HCL2 syntax:
# WRONG - causes invalid expression error
Name = $var.instance_name
# CORRECT - proper interpolation
Name = "${var.instance_name}"
# ALSO CORRECT - direct reference (for simple cases)
name = var.instance_nameCheck that all variable assignments have values:
# WRONG - incomplete
variable "example" {
default =
}
# CORRECT - has value
variable "example" {
default = "value"
}Review the line with the error for:
- Matching opening and closing brackets: { } [ ] ( )
- Proper string quoting: "string" or 'string'
- No invalid special characters
# WRONG - mismatched brackets
resource "aws_instance" "example" {
tags = {
Name = "my-instance"
# Missing closing bracket above
# CORRECT - matched brackets
resource "aws_instance" "example" {
tags = {
Name = "my-instance"
}
}After fixing the syntax, run validation to catch any other errors:
terraform validateNote: terraform validate stops at the first error. After fixing one, run it again to find the next issue.
In Terraform 0.12+, HCL2 requires strict syntax. If upgrading from 0.11, variable interpolation syntax changed from ${var.name} to var.name in most contexts. The depends_on block does not support dynamic expressions - only static variable references are allowed there. When debugging, use terraform console to test expressions interactively. In CI/CD pipelines, always run terraform validate before apply to catch syntax errors early.
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