This error occurs when Terraform files are checked out or formatted with different line endings (Windows CRLF vs Unix LF). The mismatch causes Terraform to detect phantom changes in your configuration even though the actual content is identical. This typically happens when working across different operating systems or when git is not configured to handle line endings consistently.
Line endings are invisible characters that mark the end of a line in text files. Windows uses CRLF (carriage return + line feed), while Unix/Linux/Mac use just LF (line feed). When Terraform detects that files have changed line endings, it treats this as a modification to the configuration, even though the functional content is identical. This causes terraform plan to report unwanted changes and can interfere with CI/CD pipelines. The issue is particularly problematic because line ending differences are not visually obvious in most editors, making the root cause hard to identify.
Run this command to set git to handle line endings automatically:
On Windows:
git config --global core.autocrlf trueOn Linux/Mac:
git config --global core.autocrlf inputThis tells git to convert CRLF to LF when committing (ensuring the repository always has LF) and automatically convert LF to the system default when checking out.
Create a .gitattributes file in your repository root and add:
* text=auto eol=lf
*.tf text eol=lf
*.tfvars text eol=lf
*.sh text eol=lf
*.bat text eol=crlfThis ensures all Terraform files always use LF line endings regardless of the developer's OS or git settings. The .gitattributes file takes precedence over individual git configs.
If your repository already has mixed line endings, renormalize all files after setting .gitattributes:
git add --renormalize .
git commit -m "Normalize line endings to LF"This converts all existing files in the repository to use the line endings specified in .gitattributes.
For individual files already showing the error, convert them manually:
Using dos2unix (Linux/Mac):
dos2unix *.tfUsing sed (Unix/Linux):
sed -i 's/\r$//' *.tfUsing PowerShell (Windows):
(Get-Content file.tf) -replace "`r`n", "`n" | Set-Content file.tf -NoNewlineUsing VSCode:
Open the file, look at the status bar (bottom right), click "CRLF" and change to "LF", then save.
Run terraform fmt to ensure it doesn't change line endings:
terraform fmt -checkIf this passes without changes, your line endings are now consistent. If it reports changes, verify your editor is set to use LF endings:
In VSCode:
1. Open settings (Ctrl + ,)
2. Search for "End of Line"
3. Set "Files: Eol" to \n
4. Restart VSCode or reload the window
After fixing line endings, the phantom changes should disappear. Verify with:
terraform planIf terraform plan still shows changes to files you haven't touched, you may need to:
1. Stage all files: git add .
2. Run: terraform refresh to resync state
3. Re-run: terraform plan
If the issue persists, check that all files have been converted to LF using: git diff --cached | grep -E '^[+-]' | head -20
Why this matters for Terraform specifically: Terraform uses file hashing to detect changes in configuration files. When line endings differ, the hash changes even though the logical content is identical. This is especially problematic in CI/CD pipelines where the build agent (usually Linux) has LF endings, but developers on Windows have CRLF. The solution is to ensure line endings are normalized before they ever enter the repository using .gitattributes.
Cloud-Init and User-Data scripts: If you're using heredoc blocks for AWS user-data or Cloud-Init scripts, CRLF line endings will break script execution on Linux instances. These scripts expect LF line endings and will fail with cryptic 'command not found' errors if CRLF is present.
terraform fmt behavior: Since Terraform 0.7, terraform fmt automatically converts all line endings to LF regardless of your system. If you're on Windows and terraform fmt keeps changing your line endings, this is expected behaviorโyour editor or git settings are reverting them back to CRLF.
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: Error making HTTP request: 500 Internal Server Error
HTTP 500 Internal Server Error in Terraform