How to fix Error: Invalid character - This character is not used within the language in Terraform
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.
Quick fix
Fixes Error: Invalid character - This character is not used within the language
- Check the error line and column carefully
- Run terraform fmt to auto-format
- Check for smart quotes and special characters
- Remove BOM if present
- Check for invalid characters in identifiers
# Error: Invalid character - This character is not used within the languageError: Invalid character - This character is not used within the language
What this error means
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.
Typical symptoms
- Terraform plan or apply fails immediately with 'Invalid character' error
- Error message points to a specific line number in your .tf file
- The problematic character is not visually obvious in your text editor
- Error occurs after copy-pasting code from documentation or websites
- Files encoded with different character sets cause parsing failures
Common causes
- Special characters in resource or variable names (!, @, #, $, %, etc.)
- Smart/curly quotes (“”) instead of straight quotes (") copied from Word or web browsers
- Hidden Unicode characters or BOM (Byte Order Mark) at file start
- Invalid character encoding (UTF-8 BOM, mixed encodings)
- Copy-pasting code that includes non-printable or invisible characters
- Using hyphens in variable or resource names instead of underscores
How to fix "Error: Invalid character - This character is not used within the language"
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.
Advanced notes
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.
Frequently asked questions
- What does "Error: Invalid character - This character is not used within the langu" mean?
- 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.
- How do I fix "Error: Invalid character - This character is not used within the langu"?
- 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.
- What causes "Error: Invalid character - This character is not used within the langu"?
- Special characters in resource or variable names (!, @, #, $, %, etc.) Smart/curly quotes (“”) instead of straight quotes (") copied from Word or web browsers Hidden Unicode characters or BOM (Byte Order Mark) at file start
- Is the "Error: Invalid character - This character is not used within the langu" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.