- State may contain resource attributes that include secrets (access keys, connection strings, certificates).
- It often represents a complete inventory of infrastructure that attackers can use for lateral movement.
- Backing up or copying state without encryption or access control can create additional attack surfaces.
- Store Terraform state in a secure, access-controlled, encrypted, and versioned remote backend for team or production environments (examples below).
- Use backend locking to prevent concurrent
applyconflicts (the AzureRM backend supports locking via blob leases when configured properly). - Apply RBAC and least-privilege permissions for the backend. Prefer Azure AD principals or Managed Identities scoped narrowly rather than broad storage account keys.
- Enable encryption at rest for the backend. For stronger control and auditability, consider customer-managed keys (CMK) stored in Key Vault.
- Never commit
terraform.tfstateor plaintext copies to source control. Add it to.gitignore. - Use the Terraform CLI subcommands to inspect state safely instead of opening raw files.
Commands for safe inspection
terraform state pull— fetch the raw state from the configured backend.terraform show— produce a human-readable representation of a plan or state.terraform state listandterraform state show <resource>— query resources and attributes from state.
Mark sensitive outputs so they are redacted from CLI output and logs:
- Example backend configuration that stores state in an Azure Storage Account container. This configuration should be paired with proper RBAC on the storage account, blocking public access, and enabling encryption and soft-delete as required.
Never commit
terraform.tfstate or copies of state to source control. Treat state files like any other secret: add them to .gitignore, ensure CI logs do not capture their contents, and limit access to the minimal set of principals that need it.- Prefer short-lived credentials, managed identities, or federated authentication over embedding long-lived secrets in automation.
- Rotate credentials and keys regularly and ensure access to state is auditable.
- Consider Terraform Cloud/Enterprise for built-in remote state management, locking, encryption, access controls, and audit logs.
- When automating state inspection or migration, avoid writing sensitive values to logs or insecure temporary files. If you must write state to disk, restrict file permissions and securely delete artifacts after use.
If you must inspect values from the state, prefer
terraform show or terraform state subcommands instead of opening the raw state file. Mark outputs with sensitive = true to prevent accidental exposure in CLI output and logs.- Terraform backends: https://www.terraform.io/docs/language/settings/backends/index.html
- Terraform state documentation: https://www.terraform.io/docs/state/index.html
- Azure Storage security best practices: https://learn.microsoft.com/azure/storage/common/storage-security-guide
- Terraform Cloud: https://www.hashicorp.com/products/terraform/cloud