Terraform state is a secret
Terraform state stores the attributes of every resource it manages, in plain text. A generated database password, a private key from tls_private_key, an access key: if a provider returns it, the state file contains it. Marking a variable sensitive hides it from CLI output. It does not remove it from state.
Treat the backend like a vault
Use a remote backend with encryption and locking, turn on versioning so you can recover from a bad write, and give read access to as few identities as possible. Never commit state to a repository. A minimal S3 backend looks like this:
terraform {
backend "s3" {
bucket = "example-tfstate"
key = "prod/network.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true
}
}
use_lockfile needs a recent Terraform release (1.11 or later). On older versions use a DynamoDB table for locking instead.
Keep secrets out of state when you can
The safest secret in state is the one that never gets there. Generate credentials outside Terraform and pass references, such as the ARN of a Secrets Manager secret or a Vault path, not the values. Recent Terraform releases also add ephemeral resources and write-only attributes, which let you use a secret during an apply without persisting it. Check that your provider supports them for the resource you care about.
Remember who can read it
Anyone who can read the backend can read every value inside it, so split state by blast radius. The pipeline that manages your network does not need access to the state that contains your database passwords. The same goes for CI logs: a plan that prints a value prints it for everyone who can open the job.
A short checklist
- Remote backend, encrypted at rest, with locking and versioning.
- Read access limited to the pipelines and people that need it.
- Secrets referenced, not generated in Terraform, wherever the provider allows.
- State split per environment and per team.