Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for HashiCorp Vault Difficulty: Advanced ClaudeChatGPT

Safe Vault + Terraform Integration Prompt

Wire Terraform to Vault with short-lived provider auth and patterns that keep secret values out of the state file and CI logs.

Target user
Infrastructure engineers managing Vault or consuming its secrets from Terraform
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior infrastructure engineer who has cleaned up more than one Terraform state file full of plaintext database passwords, and who now designs Vault integrations so that cannot happen again.

I will provide:
- What we are doing with the Vault provider: managing Vault config (mounts, policies, roles), reading secrets for other resources, or both
- Terraform version, how it runs (local, GitHub Actions, GitLab CI, HCP Terraform/Terraform Enterprise, Atlantis), and the state backend
- Our Vault auth methods and whether the runner has a workload identity (OIDC token, instance profile, service account)
- Which resources need secret values injected

Your job:

1. **Fix provider authentication first.** Replace any static `token` or `VAULT_TOKEN` in CI with a `auth_login_jwt` block using the runner's OIDC token (GitHub Actions `id-token`, GitLab ID tokens), or `auth_login_aws`/`auth_login_kubernetes`/`auth_login_azure`/`auth_login_gcp` where a platform identity exists. Show the provider block, the matching Vault auth role with tight `bound_claims`/`bound_subject` and `token_policies`, and a short `token_ttl` sized to the plan+apply window.

2. **Explain the child-token behaviour.** Describe how the provider creates a short-lived child token per run, what `max_lease_ttl_seconds` and `skip_child_token` actually change, and why a run that outlives its token fails mid-apply — with the sizing guidance to avoid it.

3. **State the state problem bluntly.** Any secret Terraform reads or generates is stored **in plaintext in state**, regardless of whether the output is marked `sensitive` — `sensitive` only hides it from CLI output, not from the state file or a `terraform show -json`. The same applies to plan files. Say this once, clearly, and design around it.

4. **Keep values out of state where possible.** Rank the options for my Terraform version: ephemeral resources and write-only arguments (Terraform 1.10/1.11+) where the provider supports them; having the downstream system fetch from Vault itself at runtime via Vault Agent, the Secrets Operator, or the CSI driver, rather than Terraform brokering the value; Vault dynamic secrets scoped to the run so anything landing in state expires quickly; and only as a last resort, reading a static secret into state with compensating controls.

5. **Harden the state and plan artefacts.** Require an encrypted remote backend with restricted IAM, state locking, versioning with a retention policy (old versions still contain the old secrets), no state files in git or CI artefacts, and no plan files persisted between jobs. Note that anyone with state read access effectively has every secret in it.

6. **Write the least-privilege policy for the pipeline.** Produce HCL for two separate roles: one that manages Vault objects (`sys/mounts`, `sys/policies/acl`, auth roles) and one that only reads the specific paths a run needs. Keep the Vault-managing role out of the secret-reading paths, avoid `sudo`, and scope KV paths exactly — remembering the `data/` segment for KV v2 reads and `metadata/` for deletes and listing.

7. **Protect the CI output.** Ensure secrets never reach logs: no `terraform output` of raw values, `TF_LOG` off in shared pipelines, provider debug logging disabled, masking configured, and PR-comment automation that prints plans reviewed for sensitive diffs.

8. **Give a remediation path for existing exposure.** If secrets are already in state: rotate them first (they are compromised), then clean or roll forward the state, prune old state versions, audit who had backend access during the exposure window, and only then adopt the safer pattern. Order matters — rotate before you clean.

Output as: (a) the provider auth block plus the Vault auth role config, (b) the two Terraform policies in HCL, (c) a decision table of secret-handling patterns for my Terraform version, (d) state-backend hardening settings, (e) CI logging guardrails, (f) the exposure remediation runbook.

Never commit state or plan files, never disable TLS verification against Vault to make a pipeline pass, and treat any secret that has ever been in state as needing rotation.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

The single most useful thing this prompt does is state the state problem once, bluntly, and then design around it. Engineers reliably believe that marking an output sensitive protects the value, because that is what the word implies and because the CLI stops printing it. It does not touch the state file, terraform show -json, or a persisted plan. Getting that misconception out of the way in step three is what makes the rest of the output useful — every subsequent recommendation is an answer to “so how do we avoid putting it there at all”.

Ranking the avoidance options against the user’s actual Terraform version prevents the two failure modes at either end. Ephemeral resources and write-only arguments are the right answer on 1.10/1.11+ and simply unavailable below that, so a generic recommendation either doesn’t apply or quietly assumes an upgrade nobody planned. Meanwhile the genuinely best pattern is often not a Terraform feature at all — let the downstream system fetch from Vault itself via Vault Agent, the Secrets Operator, or the CSI driver, and Terraform never brokers the value. Reading a static secret into state sits last on the list, with compensating controls, because sometimes it is where you end up.

Fixing provider authentication first, and the ordering of the remediation runbook, are the two places where sequence is the whole point. A pipeline holding a static VAULT_TOKEN has a long-lived credential in CI regardless of how carefully state is handled, so OIDC or platform-identity login with a TTL sized to the plan-and-apply window comes before anything else. And when secrets are already exposed in state, rotating before cleaning is not a preference — a scrubbed state file with live credentials in it is a tidier version of the same breach.

Related prompts

More HashiCorp Vault prompts & error guides

Browse every HashiCorp Vault prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.