How to fix Error: line endings have changed from CRLF to LF in Terraform

TerraformINTERMEDIATEMEDIUMUpdated Dec 31, 2025· 144 views

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.

Quick fix

Fixes Error: line endings have changed from CRLF to LF

git config --global core.autocrlf true
  1. Configure git to normalize line endings globally
  2. Create a .gitattributes file to enforce consistent line endings
  3. Renormalize existing repository files
  4. Convert existing files from CRLF to LF (if needed)
terraform — error
git config --global core.autocrlf true
Error: line endings have changed from CRLF to LF

What this error means

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.

Typical symptoms

  • terraform plan shows changes to files even though you haven't modified the content
  • Terraform always detects the same resources need updating after switching operating systems
  • terraform fmt automatically changes line endings unexpectedly
  • Git shows entire files as changed when only line endings differ
  • CI/CD pipelines fail with mysterious plan differences between developers

Common causes

  1. Git is not configured to normalize line endings across your team (most common)
  2. Working on the same Terraform files across Windows and Linux/Mac without consistent settings
  3. terraform fmt command changes line endings from CRLF to LF on Windows
  4. Cloning a repository on Windows without proper git autocrlf configuration
  5. Using different text editors with different line ending defaults (VSCode, Notepad++, etc)
  6. Module fetch from git repository converts line endings differently on different platforms

How to fix "Error: line endings have changed from CRLF to LF"

1Configure git to normalize line endings globally

Run this command to set git to handle line endings automatically:

On Windows:

bash
git config --global core.autocrlf true

On Linux/Mac:

bash
git config --global core.autocrlf input

This 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.

2Create a .gitattributes file to enforce consistent line endings

Create a .gitattributes file in your repository root and add:

bash
* text=auto eol=lf
*.tf text eol=lf
*.tfvars text eol=lf
*.sh text eol=lf
*.bat text eol=crlf

This 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.

3Renormalize existing repository files

If your repository already has mixed line endings, renormalize all files after setting .gitattributes:

bash
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.

4Convert existing files from CRLF to LF (if needed)

For individual files already showing the error, convert them manually:

Using dos2unix (Linux/Mac):

bash
dos2unix *.tf

Using sed (Unix/Linux):

bash
sed -i 's/\r$//' *.tf

Using PowerShell (Windows):

powershell
(Get-Content file.tf) -replace "`r`n", "`n" | Set-Content file.tf -NoNewline

Using VSCode:
Open the file, look at the status bar (bottom right), click "CRLF" and change to "LF", then save.

5Verify terraform fmt respects your settings

Run terraform fmt to ensure it doesn't change line endings:

bash
terraform fmt -check

If 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

6Clear your terraform state and re-run plan

After fixing line endings, the phantom changes should disappear. Verify with:

bash
terraform plan

If 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

Advanced notes

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.

Frequently asked questions

What does "Error: line endings have changed from CRLF to LF" mean?
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.
How do I fix "Error: line endings have changed from CRLF to LF"?
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.
What causes "Error: line endings have changed from CRLF to LF"?
Git is not configured to normalize line endings across your team (most common) Working on the same Terraform files across Windows and Linux/Mac without consistent settings terraform fmt command changes line endings from CRLF to LF on Windows
Is the "Error: line endings have changed from CRLF to LF" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading