Terraform fails when reading files with invalid UTF-8 encoding, BOM markers, or binary content. This guide helps you identify the cause and fix encoding issues in your HCL configuration files.
When Terraform encounters a file with invalid UTF-8 byte sequences, it stops processing and throws an "Invalid UTF-8 sequence" error. This occurs when the `file()` or `templatefile()` functions attempt to read files that aren't properly UTF-8 encoded. UTF-8 is the standard text encoding for Terraform HCL files. Files saved in legacy encodings (like Windows-1252 or ISO-8859-1), corrupted binary files, or those with a Byte Order Mark (BOM) prefix will cause this error. The Terraform parser strictly validates UTF-8 compliance before processing your configuration.
Most modern text editors allow you to view and change file encoding. In VS Code, click the "UTF-8" indicator in the bottom status bar to see and change encoding options.
Ensure your .tf files are saved as UTF-8 without BOM. This is the default in most editors but may be overridden if you explicitly set encoding options.
If your file has a UTF-8 BOM, you'll see a special '' or 'ï' character at the very beginning when opened in a hex editor. Most editors have a way to remove the BOM.
In VS Code:
1. Open the file
2. Click "UTF-8" in the status bar
3. Select "UTF-8 with BOM" to see if it's enabled
4. Switch to "UTF-8" (without BOM) to remove it
5. Save the file
If the encoding is still problematic, the simplest fix is to re-create the file:
1. Open your problematic .tf file
2. Copy all the content
3. Create a new file (ensuring it's UTF-8)
4. Paste the content
5. Delete the old file and save the new one
Use text editors like VS Code, Sublime Text, or Vim. Avoid Notepad on Windows, which often uses system encoding instead of UTF-8.
If you're trying to read binary content (zip archives, encrypted files, images), the file() function cannot handle it. Use filebase64() instead:
# Instead of:
locals {
binary = file("archive.zip") # ERROR
# Use:
binary_encoded = filebase64("archive.zip") # Works
}For providers that need binary content, use attributes like content_base64 instead of content.
After fixing the encoding, validate your Terraform configuration:
terraform fmt -recursive # Auto-format all .tf files
terraform validate # Check syntax and provider validityThe terraform fmt command also helps ensure consistent formatting and can catch some encoding issues.
If you have multiple files, check for encoding issues across your project:
# On Linux/Mac, check file encoding:
file *.tf | grep -i "utf"
# On Windows, use PowerShell (ensure UTF-8):
Get-ChildItem *.tf | ForEach-Object { $_.FullName }Convert files from other encodings using iconv (Linux/Mac) or online tools. Ensure all .tf, .tfvars, and related files use UTF-8 without BOM.
UTF-8 and Character Encoding in Terraform:
Terraform applies Unicode Normalization Form C (NFC) to all string inputs. This means that strings with equivalent representations are normalized to a canonical form. If the exact byte sequence matters (for binary data), providers may support base64-encoded variants (content_base64, source_base64) to bypass encoding assumptions.
Windows Encoding Differences:
On Windows systems, the default system encoding is CP1252 (Windows Latin-1), not UTF-8. If you have UTF-8 characters in your configuration (like descriptions or resource names), Windows Terraform may fail differently than on Linux. Always ensure files are explicitly saved as UTF-8 on Windows.
Multibyte Character Truncation:
In rare cases, when Terraform plan output contains very long IDs with multibyte characters (like emoji or international characters), the plan truncates output at 80 bytes without respecting character boundaries. This can create invalid UTF-8 in the plan file itself. Upgrade Terraform to the latest version to resolve this edge case.
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