Terraform State Encryption at Rest Prompt
Design end-to-end encryption for Terraform state — backend-side KMS, OpenTofu native state encryption, secrets that leak into state, and a key-rotation plan that won't lock you out.
- Target user
- Platform engineers hardening Terraform state against credential exposure
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a security-focused platform engineer who treats Terraform state as a secrets store, because it is — every password, private key, and token a resource emits lands in state in plaintext unless you act.
I will provide:
- Backend type and config (S3+DynamoDB, GCS, azurerm, Terraform Cloud, or OpenTofu)
- Terraform/OpenTofu version
- Compliance requirements (FIPS, customer-managed keys, data residency)
- Which resources are known to emit sensitive attributes
- Current key management (KMS, Vault, none)
Your job:
1. **Threat model** — enumerate who can read state today: backend bucket readers, anyone with `terraform show`, anyone with CI logs, anyone with local `.tfstate` on a laptop. Rank each by likelihood and blast radius.
2. **Layered encryption** — distinguish (a) at-rest bucket encryption (SSE-KMS) which protects storage, from (b) state-payload encryption (OpenTofu native `terraform { encryption {} }` or client-side) which protects against backend readers. Recommend both where supported; never let bucket SSE be mistaken for payload encryption.
3. **OpenTofu native encryption** — if applicable, write the `encryption` block: key_provider (aws_kms / gcp_kms / pbkdf2), method (aes_gcm), and the `state` + `plan` enforcement. Show the `enforced = true` cutover and the dual-key migration window.
4. **Secrets that leak into state** — identify resource attributes that write secrets (db passwords, IAM access keys, `random_password`, TLS private keys). Recommend moving generation to Vault/SSM where the value never enters state, or accept-and-encrypt with documented rotation.
5. **Key rotation without lockout** — the failure mode is rotating/deleting a key while state is still sealed with it. Give the ordered procedure: add new key → re-encrypt state → verify decrypt → retire old key, with a rollback checkpoint at each step.
6. **Access controls** — least-privilege bucket policy, KMS key policy split (encrypt vs decrypt vs admin), deny `s3:GetObject` to humans, CI-only decrypt role.
7. **Verification** — show how to prove state is actually encrypted (hexdump the object, attempt read without key), and a CI check that fails if `enforced` drifts to false.
Output: (a) backend + encryption HCL, (b) KMS/key-policy JSON, (c) step-by-step rotation runbook, (d) CI guard, (e) one-page "who can read state" matrix.
Bias toward: defense in depth, fail-closed enforcement, and never trusting that bucket SSE is enough.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
OpenTofu State Encryption Key Rotation Prompt
Rotate OpenTofu state-encryption keys safely using the `fallback` method so existing encrypted state can still be read while new writes use the new key — with zero downtime.
-
Terraform Write-Only Argument Migration Prompt
Migrate existing secret arguments that linger in state (passwords, tokens) to write-only arguments and their `_wo_version` triggers, so sensitive values stop being persisted in the state file.
-
Terraform Checkov Custom Policy Authoring Prompt
Write custom Checkov policies for Terraform — Python checks extending BaseResourceCheck and YAML-based policies, a .checkov.yaml config, inline skip suppressions and baselines, then wire soft-fail vs hard-fail gating into CI.
-
Terraform jsonencode Policy Rendering Review Prompt
Review IAM/resource policies built with jsonencode and templatefile in Terraform for correctness, injection risk, and plan-time diff noise.
More Terraform prompts & error guides
Browse every Terraform prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.