InsightsInfrastructure as Code
Keeping secrets out of Terraform state with ephemeral values
How ephemeral resources, ephemeral variables and write-only arguments let Terraform create databases, configure providers and rotate passwords without writing the secret to state or plan files, and the version bumps, plan-time reads and partial applies that trip teams up.
Published October 25, 20267 min read
Every Terraform state file of any age contains something it should not. A database master password passed through a variable, a secret read with a data source to configure a provider, a token generated by random_password and handed to a resource. Marking the value sensitive hides it from plan output, but the documentation is explicit that Terraform still stores sensitive values in both state and plan files, and anyone who can read those files can read the values.
That turns the state bucket, every CI job that runs a plan and every saved plan artifact into places a credential can leak from. Terraform 1.10 and 1.11 added a way out: values that exist only while an operation runs and are never persisted. This article covers how the pieces fit, how to migrate an existing stack, and what goes wrong.
The three pieces
Terraform calls these ephemeral values. They come in three forms:
| Feature | What it does | Terraform version |
|---|---|---|
ephemeral block | A resource that is opened during an operation, used and closed, never written to state or plan | 1.10 |
ephemeral = true on variables and child module outputs | Passes a value through the configuration without persisting it | 1.10 |
Write-only arguments (password_wo, secret_string_wo, ...) | Managed resource arguments that accept ephemeral values; stored in state as null | 1.11 |
OpenTofu shipped ephemeral resources and write-only attributes as stable features in version 1.11.
Ephemeral values can only be referenced in a few places: write-only arguments, other ephemeral blocks, locals, variables and child module outputs that are themselves ephemeral, provider blocks, and provisioner and connection configuration. Anything else is an error at plan time, and any expression that references an ephemeral value becomes ephemeral itself. That restriction is the point: Terraform can prove the value never reaches a place that gets saved.
First choice: do not let Terraform see the secret
Before reaching for ephemeral values, check whether the service can own the secret entirely. For RDS, manage_master_user_password = true has RDS generate the master password and keep it in Secrets Manager, with rotation every seven days by default. Terraform never sees it, so there is nothing to protect:
resource "aws_db_instance" "payments" {
identifier = "payments"
engine = "postgres"
instance_class = "db.t4g.medium"
allocated_storage = 50
username = "payments_admin"
manage_master_user_password = true
}Applications then read the secret at runtime, for example through External Secrets Operator. Use ephemeral values where this is not possible: services without a managed-password option, a secret you need to generate yourself, or a provider that needs credentials to configure itself.
Generating a password without storing it
When Terraform has to create the secret, generate it with an ephemeral resource and send it to write-only arguments. The random provider has an ephemeral random_password, and the AWS provider supports secret_string_wo on aws_secretsmanager_secret_version and password_wo on aws_db_instance:
locals {
db_password_version = 1
}
ephemeral "random_password" "db" {
length = 32
}
resource "aws_secretsmanager_secret" "db" {
name = "payments/db-admin"
}
resource "aws_secretsmanager_secret_version" "db" {
secret_id = aws_secretsmanager_secret.db.id
secret_string_wo = ephemeral.random_password.db.result
secret_string_wo_version = local.db_password_version
}
resource "aws_db_instance" "payments" {
identifier = "payments"
engine = "postgres"
instance_class = "db.t4g.medium"
allocated_storage = 50
username = "payments_admin"
password_wo = ephemeral.random_password.db.result
password_wo_version = local.db_password_version
}A few things about this are not obvious:
- The password is generated on every run. Ephemeral resources are opened again in each plan and each apply. The new value is only used when something tells the provider to apply it.
- The version argument is that something. Terraform cannot diff a value it never stored, so each write-only argument has a companion
_wo_versionthat is stored in state. The provider applies the write-only value when the resource is created or when the version changes. Bumpingdb_password_versionto2rotates the password in both places in one apply. - Both resources get the same value. Within one apply the ephemeral resource is opened once, so the secret and the database receive the same password. That is why both reference the ephemeral resource directly rather than reading the secret back.
- The value is gone after the run. Terraform keeps nothing, so if the password matters it has to land somewhere durable, which is what the Secrets Manager secret is for.
Reading a secret without storing it
The most common leak is not a generated password but a data source. data "aws_secretsmanager_secret_version" writes the decrypted secret_string into state on every refresh. The AWS provider has an ephemeral resource with the same name and arguments that does not:
ephemeral "aws_secretsmanager_secret_version" "db" {
secret_id = "payments/db-admin"
}
locals {
db_admin_password = ephemeral.aws_secretsmanager_secret_version.db.secret_string
}
provider "postgresql" {
host = aws_db_instance.payments.address
username = "payments_admin"
password = local.db_admin_password
sslmode = "require"
}Configuring a provider is the textbook use: the credential is needed while Terraform talks to the database to create roles and grants, and never afterwards. For a JSON secret, jsondecode(ephemeral.aws_secretsmanager_secret_version.db.secret_string)["password"] stays ephemeral too.
Ephemeral resources follow an open, renew, close lifecycle. For a Vault dynamic secret that means a lease is obtained when Terraform needs it, renewed if the run outlasts it, and revoked when Terraform closes the resource after the providers that depend on it have finished. Short-lived database credentials that exist only for the duration of an apply are a large improvement on a static admin password.
Passing secrets in from CI
Tokens that come from outside, such as an API key for a SaaS provider injected by the pipeline, belong in ephemeral variables:
variable "datadog_api_key" {
type = string
sensitive = true
ephemeral = true
}ephemeral = true keeps the value out of state and plan files; sensitive = true still matters, because it keeps the value out of console output. Since the plan file does not contain the value, a pipeline that saves a plan and applies it in a later job has to supply the variable to both jobs, for example as a TF_VAR_datadog_api_key environment variable. Where the cloud supports it, prefer OIDC federation so there is no long-lived token to pass at all.
Modules can hand ephemeral values to each other through outputs marked ephemeral = true. Root module outputs cannot be ephemeral, so a secret cannot be exported from a stack this way. That is deliberate: remote state outputs are exactly the kind of persisted copy you are trying to get rid of.
Migrating an existing stack
Switching password to password_wo stops new copies being written. It does nothing about the old ones.
- Upgrade first. Terraform 1.11 or later for write-only arguments, and a provider release that has the
_woargument for each resource. The registry marks write-only arguments in the resource documentation. - Replace secret data sources with ephemeral resources and remove variables that carried secrets into ordinary arguments.
- Move to write-only arguments with a version of
1. Expect a plan that touches the resource; read it carefully the first time. - Rotate every secret that was ever in state. Previous state versions in a versioned bucket, saved plan artifacts in CI and local copies on laptops still hold the old value. After rotation they are harmless.
- Check the result. Pull the state and search it for the secret's new value or a known prefix. It should not be there.
terraform state pull | jq '.. | objects | select(has("password_wo") or has("secret_string_wo"))
| {password_wo, secret_string_wo}'Both fields should be null.
What goes wrong
- Plans now need secret access. Ephemeral resources are opened during plan as well as apply, so the role that runs
terraform planon pull requests needs read access to those secrets. If plan and apply use different roles, decide deliberately whether the plan role should have it, or move secret-dependent resources into a stack that only plans in trusted pipelines. - No diff, no drift detection. Terraform cannot tell you that someone changed the password in the console, because it has nothing to compare with. The version argument is the only signal, and forgetting to bump it means a changed value is silently ignored.
- The value is sent on every operation. Providers receive write-only values on every plan and apply and decide what to do with them. Behaviour is provider-specific, so read the resource documentation rather than assuming it matches the AWS one.
- Policy checks see less. Plan JSON no longer contains the secret, which is the goal, but rules that inspected it, for example to reject short passwords, need to move to the ephemeral resource's arguments or a
lifecycleprecondition. - Literal strings are accepted. A write-only argument takes a plain string, and a hard-coded password in a write-only argument is still a hard-coded password in git.
Where to start
- Search every stack for
data "aws_secretsmanager_secret_version",data "vault_and arguments namedpassword,tokenorsecretthat take a variable. - Use managed passwords where the service offers them.
- Replace secret data sources with ephemeral resources, starting with provider configurations.
- Move generated passwords to ephemeral
random_passwordand write-only arguments with a version local. - Rotate everything that was ever in state, then verify the state file no longer contains it.