This error occurs when Terraform encounters an unsupported character in your HCL configuration file. Common causes include special characters in identifiers, copy-pasted curly quotes, hidden Unicode characters, or encoding issues.
Terraform uses HashiCorp Configuration Language (HCL) which has specific rules about what characters are valid in different contexts. This error means the parser encountered a character that is not part of the HCL syntax specification. This typically happens when invalid characters appear in resource names, variable names, or when special characters are accidentally introduced through copy-pasting or encoding issues.
The error message includes a line number. Open your .tf file and locate that exact line. Look closely for any unusual characters around the column position mentioned.
Run: terraform fmt -recursive .
This reformats your HCL to correct many common syntax issues. If the error persists, the problematic character wasn't auto-fixed.
Look for curly/smart quotes (“ or ”) that should be straight quotes ("). Also check for non-ASCII characters. If you find them, replace with standard ASCII equivalents. For example:
Wrong: variable "name" { }
Correct: variable "name" { }
Check if your file starts with a BOM (Byte Order Mark). On Linux/Mac, use:
file yourfile.tf
If it shows "UTF-8 BOM", remove it with:
sed -i '1s/^\xEF\xBB\xBF//' yourfile.tf
On Windows, use a text editor like VS Code and change encoding to "UTF-8 without BOM".
Resource names and variable names must contain only letters, numbers, underscores, and hyphens (though hyphens are not allowed in variable names). Examples:
Wrong: resource "aws_instance" "my-server@prod" { }
Correct: resource "aws_instance" "my_server_prod" { }
Wrong: variable "my-var-name" { }
Correct: variable "my_var_name" { }
If the above steps don't work, delete the problematic line and re-type it manually. This ensures you avoid any hidden characters that may have been copied from the source.
Run: terraform validate
This performs a more thorough syntax check. If validation passes, your HCL syntax is correct.
Debugging hidden characters: Use cat -A yourfile.tf on Linux/Mac to display all non-printing characters. This helps identify BOM, special Unicode, or invisible whitespace issues.
IDE support: Use VS Code with the HashiCorp Terraform extension, which highlights syntax errors and invalid characters in real-time.
Provider-specific issues: Some provider code generation tools (like importing Terraform configs from existing resources) may produce invalid HCL. In these cases, manually review and fix the generated code.
JSON vs HCL: If using .tf.json files, ensure valid JSON syntax. Mixing JSON and HCL in the same file causes parsing errors.
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