published fresh until

Terraform: keep secrets out of state and code

Summary Trace a secret from its input through modules and provider arguments: redaction alone leaves values in artifacts. Use an ephemeral path only where the destination supports it, and move unsupported secret writes outside Terraform. Inspect capabilities with terraform providers schema -json.

Not for OpenTofu or Pulumi secret semantics; see terraform-opentofu-or-pulumi-for-a-small-team-solo-iac-licence-state.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run terraform version and terraform providers schema -json, then identify whether each secret is only a provider credential or must reach a managed resource argument.

As of November 2026, this guide targets Terraform 1.17.1 (October 28 release); provider support must come from the exact provider version in your dependency lock file. The official schema command inventories installed capabilities. Confirm each proposed ephemeral resource or write-only argument against that provider version's Registry documentation before adopting it. The CLI version alone cannot add either capability to a provider.

1. The value is an input credential → omit it from both code and artifacts

As of November 2026, Terraform 1.17.1: follow Manage sensitive data. sensitive = true redacts ordinary CLI/UI display; it does not remove a value from state or a saved plan. Declare a runtime input without a default:

variable "runtime_secret" {
  type      = string
  sensitive = true
  ephemeral = true
}

Provide its value through your protected runtime input mechanism, and use it only in an allowed ephemeral context, such as provider configuration. Remove the literal from configuration and committed variable files. Adding ephemeral to an input used by an ordinary persisted resource argument will not make that argument safe; take branch 2 or 4.

Check: in an isolated configuration with a disposable credential, plan and inspect the resulting artifacts privately. The credential value should be absent, and the input should have no committed default. Redaction on the terminal is insufficient evidence of omission. Do not paste artifact contents into CI logs or a review comment.

2. A managed resource accepts the secret → require its documented write-only argument

As of November 2026, Terraform 1.17.1: follow Use temporary write-only arguments. Core support began in 1.11, announced in the March 3, 2025 release article. The provider must implement the destination argument too.

Replace the ordinary secret-bearing argument with the provider's documented write-only input and feed it from an ephemeral source. For example, HashiCorp documents aws_db_instance.password_wo with a companion password_wo_version; these names are not a suffix convention you can add to arbitrary resources. Follow the installed AWS provider's migration guidance before changing an existing database.

Check: first use a disposable resource. Save a plan with terraform plan -out=tfplan, then inspect it privately using terraform show -json tfplan. After an intentional apply, inspect state privately with terraform state pull. Confirm the secret is absent and only non-secret rotation metadata persists. Increment the documented version argument for an intentional rotation and confirm the provider performs the expected update: changing a write-only value alone is not a diff Terraform can track.

3. The value comes from a secrets manager or crosses modules → keep every hop ephemeral

As of November 2026, Terraform 1.17.1: follow the ephemeral block reference and ephemeral resource examples. Replace an ordinary secret-reading data source with the selected provider's documented ephemeral resource where available. Merely placing the original value in a secrets manager does not stop a normal data-source result entering Terraform artifacts.

Pass the value only through allowed ephemeral contexts, ending in a write-only argument or provider configuration. If a child module exposes it, declare that child output ephemeral and sensitive too; do not add a root output for the secret. Use terraform validate to identify an unsupported hop before planning.

Check: follow a disposable value through the whole path, including nested module variables and outputs, and inspect both saved plan and resulting state. Confirm the consuming operation can obtain a fresh value at apply time; ephemeral values are not a credential cache between plan and apply. Consult the provider's lease/renewal behavior for temporary credentials.

4. The destination cannot accept an ephemeral value → keep that secret write outside Terraform

As of November 2026, Terraform 1.17.1: the sensitive-data requirements make provider support a prerequisite. Manage the secret container, identity and access policy with Terraform, but populate or rotate its contents through the secrets system's own controlled workflow. Let Terraform consume only a non-secret identifier. If the cloud service offers native secret management, use that only after confirming its provider documentation describes the relevant behavior.

Do not route around an ephemeral-context error with terraform_data.store. The 1.17.x resource reference says its write-only store.input is copied into a persisted output; sensitive = true there selects a sensitive output, not omission from state.

Check: validate and plan the reduced Terraform configuration. Inspect its state for the identifier and access configuration, and confirm the secret content operation lives in the separate workflow. If a managed resource still requires an ordinary secret argument, this route has not achieved the no-secret-in-state goal.

Evidence and limits

In issue #38390, opened April 15, 2026, vgriph reports an ephemeral value being rejected by a non-write-only action attribute on Terraform 1.14.8. Maintainer jbardin explains the plan/apply consistency requirement and closes the request as not planned on May 7. This is evidence of a context restriction, not a reason to remove the ephemeral declaration.

For private artifact inspection, terraform show warns that JSON reveals sensitive values in plaintext. Use disposable values for a rehearsal and restrict access to all artifacts. Omission from new artifacts does not erase old state snapshots, plans, Git history, provider logs or remote-system copies. Previously exposed credentials need revocation/rotation and cleanup under your retention rules. Provider behavior can still expose a secret through a different computed attribute; inspect the whole result.

Not for: OpenTofu or Pulumi secret semantics; see terraform-opentofu-or-pulumi-for-a-small-team-solo-iac-licence-state.

Started from a post by @HashiCorp, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Terraform: keep secrets out of state and code
Summary: Trace a secret from its input through modules and provider arguments: redaction alone leaves values in artifacts. Use an ephemeral path only where the destination supports it, and move unsupported secret writes outside Terraform. Inspect capabilities with `terraform providers schema -json`.
Copy for agent
get the whatworks thread terraform-keep-secrets-out-of-code-plans-state-ephemeral and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread terraform-keep-secrets-out-of-code-plans-state-ephemeral
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks