Skip to main content
In this lesson we move from protecting secrets inside your Terraform configuration to protecting where those secrets live: the Terraform state file. Terraform state captures the full configuration and current values for every resource Terraform manages. That often includes highly sensitive data such as database passwords, API keys, private keys, and TLS certificates. By default, state is plain JSON with no encryption, so secrets you configure with Terraform will frequently appear in cleartext inside the state file. Why this matters for security and compliance:
  • Exposed state can lead to credential compromise, privilege escalation, and data breaches.
  • State files are frequently shared between users, CI systems, and cloud providers — protecting access is essential.
  • Production infrastructure requires central, auditable, and encrypted state storage.
Why Terraform state often contains secrets (example) Below is a simplified example showing how secrets can appear in state:
Key takeaways:
  • Terraform does not encrypt state files automatically; state is stored as raw JSON unless you configure a secure backend.
  • Anyone who can read the state file can discover sensitive values.
  • Protecting state storage and access is critical for secure infrastructure management.
Risks of local state
  • Local state lives on your workstation: it’s easy to accidentally commit to Git, hard to share securely, and lacks centralized access controls or audit logging.
  • Local files do not provide server-side encryption, locking, or IAM integration.
Do not commit state files (for example terraform.tfstate) or crash backups to version control. If local state contains secrets, a repository push can lead to immediate exposure.
Benefits of remote state backends Remote backends (S3, Azure Storage, GCS, Terraform Cloud, etc.) give you:
  • Encryption at rest (SSE-S3, SSE-KMS, customer-managed keys).
  • Access control via IAM/RBAC and the ability to separate read vs write rights.
  • State locking to prevent concurrent applies and state corruption.
  • Centralized audit logging (e.g., AWS CloudTrail) for visibility and incident response.
How to configure encrypted remote state (AWS S3 example) Place a backend block in your Terraform configuration or provide it during initialization. This example uses S3 with server-side encryption and a DynamoDB lock table:
Backend field summary Notes on encryption, KMS and locking
A purple presentation slide titled "Controlling Who Can Access Terraform State" showing a defense-in-depth diagram with three stacked layers: IAM/RBAC Policies, State Locking, and File Encryption. A bulleted list on the right summarizes best practices like restricting read/write access, audit logging, lockfiles, and using KMS/customer-managed keys.
Defense-in-depth: combine multiple controls Each layer reduces risk if another control fails. Combine these protections for robust security:
  • File encryption (at rest): protects data in storage so it’s unreadable without keys.
  • State locking: avoids concurrent operations that could corrupt state.
  • IAM / RBAC policies: limit who and what can read or write state; consider separating read and write roles (e.g., developers read, CI/CD writes).
  • Audit logging: enable provider logs (e.g., AWS CloudTrail) so you can track who accessed state and when.
Security checklist for Terraform state Use this checklist to secure state in production.
For new projects, configure encrypted remote state and locking from the start. If you have existing local state, terraform init can migrate it to the remote backend when you add a backend configuration.
Additional recommendations
  • Minimize storing secrets in outputs. Use provider-specific secure storage or secret managers where possible.
  • Consider using Terraform Cloud or an enterprise backend that offers built-in state encryption, RBAC, and audit logging if you need managed controls.
  • Regularly rotate keys and credentials referenced by resources in state.
  • Review and tighten IAM policies that control access to the backend (S3/GCS/Azure Blob) and KMS keys.
Final thoughts State files will often contain secrets — that’s unavoidable because Terraform needs those values to manage and recreate infrastructure. Your responsibility is to secure the state storage and control who can access it. By combining encrypted remote storage, locking, fine-grained access control, and auditing, you can safely manage Terraform state in production. Provider-specific security features (for example, write-only arguments or provider-managed secrets) can offer additional protections at the resource level — evaluate those per provider. External references and further reading

Watch Video