State Is a Secret
Terraform state is a secret because it holds the full attributes of every object it manages. Sensitive values can sit inside it in plain text.
Ignore state from the first commit
This file keeps state and backups out of version control.
# .gitignore
terraform.tfstate
terraform.tfstate.backup
.terraform/A sensitive variable still lands in state
Sensitive hides the value on screen. State still stores it.
variable "db_password" {
type = string
sensitive = true
}Check that the file is ignored
Git prints the file name when the ignore rule works.
git check-ignore terraform.tfstateExample
# .gitignore
terraform.tfstate
terraform.tfstate.backup
.terraform/
# main.tf
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.0"
}
}
}
resource "local_file" "hello" {
content = "Hello."
filename = "${path.module}/hello.txt"
}
# commands
git check-ignore terraform.tfstate
terraform init
terraform apply -auto-approveKey points
- Passwords, keys, and tokens can appear as attributes. State stores them as they are.
- Anyone who can read the state file can read those values. Treat the file like a password.
- Add terraform.tfstate to .gitignore from the first commit. A secret that is committed is hard to erase.
- Also ignore the .terraform folder and backup files. They hold state copies and provider binaries.
- Marking a variable sensitive hides it from the terminal. It does not remove it from state.
The check-ignore command prints the file name, which proves the rule works. State stays on the laptop and never reaches version control. Add the ignore file before the first commit, not after.
Note
state belongs in a private backend. A shared backend needs access controls.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Why must terraform.tfstate stay out of version control?