Hey everyone ๐
If you're working with Terraform and using Git to version your infrastructure code (which you absolutely should!), there's one simple file that can save you from major headaches:
โก๏ธ .gitignore
When I first started with Terraform, I was focused on writing .tf files and deploying resources. But I quickly learned that if you donโt handle .gitignore properly, you can accidentally leak sensitive info โ and clutter your repo with auto-generated noise.
Let me walk you through it the way I wish someone had done for me ๐
๐งน Think of It Like a โDo Not Packโ List
Youโre moving houses. You label boxes you want to move โ clothes, books, tech.
But you donโt pack everything. You leave out garbage, pizza boxes, and broken cables.
.gitignore is your "Do Not Pack" list for Git โ telling it what not to commit.
๐ฃ What Happens If You Donโt Use It?
Terraform creates a lot of behind-the-scenes files โ like plugin folders, state files, and even crash logs.
If you push them to Git:
- Your repo gets bloated ๐พ
- Secrets might be exposed ๐ฑ
- Other devs will see things they shouldnโt ๐
โ ๏ธ Common Terraform Files You Should Ignore
| ๐ File or Folder | โ ๏ธ Why It Should Be Ignored |
|---|---|
.terraform/ |
Contains downloaded provider binaries and plugin data โ recreated with terraform init
|
terraform.tfstate |
Stores your actual infrastructure state โ including cleartext secrets |
terraform.tfstate.backup |
A backup copy of your state โ same risks as above |
*.tfvars |
May include environment-specific variables like passwords, API keys, etc. |
crash.log |
Only created on errors โ not useful for version control |
โ๏ธ Example .gitignore File for Terraform
# Ignore Terraform system files
.terraform/
*.tfstate
*.tfstate.backup
*.tfvars
crash.log
๐ก Pro tip: You can grab a prebuilt Terraform .gitignore from GitHubโs github/gitignore repo.
๐ฌ Quick Lab Recap: Setting It Up
In the demo I followed:
- Cloned a sample Terraform repo containing
demo.tf - Created an
example.tfvarsfile with this content:
username = "admin"
password = "password"
- Ran
terraform initandterraform apply - Noticed these files appeared:
.terraform/terraform.tfstateexample.tfvars
Then created a .gitignore file with the entries listed above.
After that, running git status showed:
โ
Git ignored all the sensitive/noisy files
โ
Only .gitignore was being staged
๐ง Why This Matters (Especially for Teams)
Without .gitignore, you might:
- Accidentally leak credentials in
tfstatefiles - Share huge plugin binaries no one needs
- Cause version conflicts from files that change every run
๐ก Analogy: Committing Terraform system files is like sending your dirty laundry to your boss instead of just the project folder.
โ Final Checklist
| โ Good Practice | โ Bad Practice |
|---|---|
Use .gitignore to keep your repo clean |
Commit .tfstate or .tfvars files |
| Store secrets securely (not in Git) | Hardcode passwords in .tf files |
| Push only the code โ not the mess | Include local .terraform/ or crash logs |
๐งฉ Final Thoughts
Using .gitignore isnโt just about being tidy โ itโs about:
- Keeping your infrastructure secure
- Making your repo easier to work with
- Protecting your team from accidentally leaking secrets
Itโs a small file with a big job. And once you understand it, it becomes second nature in every Terraform project.
If youโre working with Terraform, Iโd love to hear your .gitignore setup or lessons youโve learned. Drop a comment or connect with me on LinkedIn โ letโs keep building clean, secure infrastructure together โ๏ธ๐ง
Top comments (0)