Prompt

How do I keep secrets out of Terraform state?

Technology / Cybersecurity1 observationLast seen Oct 25, 2026

Latest observation

Oct 25, 2026OpenAI APIWeb search: off

You generally can’t guarantee secrets never touch Terraform state, because Terraform often needs to store resource arguments and outputs to track infrastructure. The goal is to minimize what gets stored, avoid placing secrets in Terraform-managed resources when possible, and protect state tightly.

Best practices

1) Don’t put secrets directly in Terraform variables or resource arguments

If you pass a secret into a resource field, Terraform may write it to state even if the provider marks it as sensitive.

Bad:

variable "db_password" {
  type      = string
  sensitive = true
}

resource "aws_db_instance" "db" {
  password = var.db_password
}

Even with sensitive = true, the value may still exist in state.

2) Use Terraform only to create secret references, not secret values

Store the secret in a dedicated secret manager, then have Terraform configure the app to read from it:

  • AWS Secrets Manager
  • HashiCorp Vault
  • Azure Key Vault
  • Google Secret Manager
  • Kubernetes secrets via external operators/workflows

Example pattern:

  • Terraform creates the secret container / access policy
  • Another system writes the secret value
  • Your app reads it at runtime

3) Mark outputs as sensitive

This prevents accidental display in CLI output, but does not remove it from state.

output "db_password" {
  value     = var.db_password
  sensitive = true
}

4) Prefer data sources or external retrieval at runtime

Instead of feeding secret values through Terraform, let the application or deployment system fetch them when needed.

Examples:

  • App reads from AWS Secrets Manager at startup
  • Kubernetes workload uses CSI driver / external secrets operator
  • CI/CD injects secrets at deploy time, not via Terraform state

5) Use lifecycle ignore_changes only for drift control, not secrecy

This can stop Terraform from managing a field after creation, but it doesn’t erase existing state contents.

6) Encrypt and restrict access to state

If secrets might still end up in state, secure the state backend:

  • Use remote state, not local state
  • Encrypt at rest
  • Restrict IAM/RBAC access
  • Use versioning and audit logging carefully
  • Protect state snapshots/backups too

Common backends:

  • S3 + KMS + tight bucket policy
  • Terraform Cloud/Enterprise
  • Azure Storage with encryption
  • GCS with CMEK if needed

7) Be careful with providers and modules

Some providers or modules store sensitive values in state even when not obvious. Check provider docs and schema:

  • Search for Sensitive
  • Look for resources that store passwords, tokens, private keys, certificates, or connection strings

8) Avoid rendering secrets into files via Terraform

If you use templatefile, local_file, user-data, cloud-init, or provisioners, secrets can land in:

  • state
  • logs
  • generated files on disk
  • instance metadata or startup scripts

9) Use ephemeral credentials where possible

Instead of long-lived secrets:

  • OIDC federation to cloud providers
  • short-lived tokens
  • dynamic DB credentials from Vault
  • temporary access keys

Practical rule of thumb

If a value is a secret, try to make Terraform manage:

  • permissions
  • resource references
  • secret containers
  • access policies

…and let something else manage:

  • the actual secret value

If you must pass a secret through Terraform

Then reduce exposure:

  • mark variables and outputs sensitive
  • use a remote encrypted backend
  • lock down state access
  • avoid printing plan/apply output in CI logs
  • rotate the secret after use if practical

Quick checklist

  • Don’t interpolate raw secrets into resources
  • Use a secret manager
  • Store secret values outside Terraform
  • Mark outputs sensitive
  • Use encrypted remote state
  • Restrict state access
  • Review provider/resource schemas for secret fields
  • Avoid provisioners/local files for secrets

If you want, I can also show you provider-specific patterns for AWS, Azure, GCP, or Vault.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.