Protect Terraform state
Ensure .tfstate and backup files never leave your local machine.
Explore resourceProfessional .gitignore tools
Complete Terraform .gitignore template for Infrastructure as Code. Secure your .tfvars, ignore .terraform/ directory, and follow DevOps best practices.
Never commit sensitive files! Always ignore .tfvars, terraform.tfstate, and any files containing secrets, API keys, or credentials.
Embed these checks into your pull-request template or CI job to avoid leaking infrastructure secrets.
terraform.tfstate* and *.backup never leave your local machine..tfvars in a secret manager; commit the .tfvars.example stub instead..terraform/ before zipping modules or publishing packages.# Terraform .gitignore Template - Updated 2025 # Local .terraform directories **/.terraform/* # .tfstate files *.tfstate *.tfstate.* # Crash log files crash.log crash.*.log # Exclude all .tfvars files, which are likely to contain sensitive data *.tfvars *.tfvars.json # Ignore override files as they are usually used to override resources locally override.tf override.tf.json *_override.tf *_override.tf.json # Include override files you do wish to add to version control using negation # The commented out entries below show how to achieve this: # !example_override.tf # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan *.tfplan # Ignore CLI configuration files .terraformrc terraform.rc # Ignore Mac .DS_Store files .DS_Store # Ignore Terraform lock file (optional, see note below) .terraform.lock.hcl # Ignore Terraform Cloud/Enterprise run data .terraform/ # Ignore backup files *.backup *.bak # Ignore log files *.log # Ignore temporary files *.tmp *.temp # Ignore IDE and editor files .vscode/ .idea/ *.swp *.swo *~ # Ignore OS generated files Thumbs.db ehthumbs.db Desktop.ini $RECYCLE.BIN/ # Ignore terragrunt cache .terragrunt-cache/ # Ignore Terraform workspaces terraform.tfstate.d/ # Ignore provider cache .terraform.d/ # Ignore custom Terraform modules (if using local development) # modules/ # .terraform-modules/ # Ignore environment-specific files .env .env.local .env.*.local # Ignore terraform documentation terraform-docs.yml .terraform-docs.yml
*.tfvars - Variable files with secrets*.tfstate - State files with sensitive data.env - Environment variablesterraform.rc - CLI configurationcrash.log - May contain sensitive info.terraform/ - Provider cache*.tfplan - Plan output files.terragrunt-cache/ - Terragrunt cacheoverride.tf - Local overrides*.backup - Backup filesRemove from .gitignore: Comment out or delete the.terraform.lock.hcl line
Keep in .gitignore: Leave the.terraform.lock.hcl line as-is
Create a template file with dummy values for team members:
# terraform.tfvars.example (commit this) aws_region = "us-west-2" environment = "production" database_password = "your-password-here" # terraform.tfvars (ignore this) aws_region = "us-west-2" environment = "production" database_password = "actual-secret-password"
Store terraform.tfstate remotely (S3, Terraform Cloud) instead of locally. This ensures state files are never accidentally committed and enables team collaboration.
Use separate variable files for each environment:
# All ignored in .gitignore dev.tfvars staging.tfvars production.tfvars # Use with: terraform apply -var-file="dev.tfvars"
If you accidentally committed terraform.tfstate files:
# Remove from tracking but keep locally git rm --cached *.tfstate git rm --cached *.tfstate.* # Update .gitignore and commit git add .gitignore git commit -m "Remove terraform state from tracking"
The .terraform directory can become large. Clean it up and ensure it's ignored:
# Clean local cache rm -rf .terraform/ # Reinitialize terraform init # Verify .terraform/ is in .gitignore
Always ignore .tfplan files as they can contain sensitive information from your infrastructure state.
If you're using Terragrunt, add these additional patterns:
# Terragrunt specific ignores .terragrunt-cache/ terragrunt.hcl.backup .terragrunt/
Never! State files contain sensitive information about your infrastructure and can include secrets. Always use remote state backends (S3, Terraform Cloud, etc.) and ignore *.tfstate in your .gitignore.
Only if they contain no sensitive data. Create terraform.tfvars.example with dummy values for the team, but always ignore actual .tfvars files. Use environment variables or secure secret management instead.
Never hardcode credentials in .tf files. Use environment variables, AWS profiles, or CI/CD secrets. Ignore any files containing actual credentials like .aws/credentials.
Yes, override files are typically used for local development and testing. They can contain temporary configurations that shouldn't be shared with the team.
Ensure .tfstate and backup files never leave your local machine.
Explore resourceLearn how to store secrets outside of Git and validate before commit.
Explore resourcePrevent Terraform cache and plan artifacts from breaking pipelines.
Explore resourceRun the validator on your repository to be sure sensitive files stay out of Git.