Secrets in Terraform State
The password is in no .tf file. The state file still has it.
What Is Secrets in Terraform State?
Terraform records the real value of every resource attribute in its state file, in plaintext, including a password the configuration only ever read from a secrets manager. You'll pull an unprotected state file, read a database password that appears in no .tf file, and open the production database with it. Then you'll encrypt and lock the backend, switch the password to a write-only argument so it never enters state again, and rotate the credential that already leaked, because securing the file does not recall the copy already taken.
What You'll Learn in Secrets in Terraform State
- State records every attribute it manages, so a secret reaches the state file even when the code only read it from a secrets manager.
- A readable state backend is a credential store: anyone who can read the bucket can read the secrets inside it.
- Write-only arguments and ephemeral values keep a secret out of state entirely, which is stronger than encrypting it there.
- A leaked state hands over the real value, so the exposed credential must be rotated, not just protected.
Secrets in Terraform State — Training Steps
-
Where the state lives
Bob does not break into Pellacorn. He reads its infrastructure code, which is public, and looks for where Terraform keeps its state. The backend block tells him exactly that. Pellacorn runs a subscription billing platform. Its Terraform provisions the production database, and the state file records what it built.
-
A state bucket anyone can read
The backend names the bucket the state file sits in. The state records every attribute of every resource, so whoever can read that bucket can read the secrets inside it.
-
List the bucket
Bob asks the bucket what is in it, without signing the request. An unprotected bucket answers.
-
Pull the state file
The state is a plain file. Bob copies it to his own machine, still without credentials.
-
The password is not in the code
Before reading the state, Bob checks where the database password comes from. The module reads it from Secrets Manager at apply time and never writes it into a file. So nothing in the repository contains it.
-
Read the password out of state
Terraform will print any resource's recorded attributes straight from the state file. Bob asks it for the database.
-
Open the database
A password and an endpoint are everything needed to connect. Bob points a client at the production billing database.
-
Read the subscribers
The connection is live. Bob reads the customer table the billing platform runs on.
-
Why the state had it
-
Reads from an address we do not know
Pellacorn's monitoring flags the billing database being read from an address it has never seen, and a storage scan flags the state bucket as publicly readable.
Security Framework Coverage
CWE
- CWE-312 Cleartext Storage of Sensitive Information
- CWE-522 Insufficiently Protected Credentials
MITRE ATT&CK
- T1552.001 Unsecured Credentials: Credentials In Files
- T1530 Data from Cloud Storage
CIS Controls
- CIS 3 Data Protection
- CIS 4 Secure Configuration of Enterprise Assets and Software
NIST CSF
- PR.AT-02 Individuals in specialized roles are provided with awareness and training so that they possess the knowledge and skills to perform relevant tasks with cybersecurity risks in mind
- PR.DS Data Security
- PR.PS Platform Security
NICE Framework
- DD-WRL-004 Secure Systems Development
- IO-WRL-005 Systems Administration