Ephemeral resources and write-only arguments

Advanced · 19 min

Terraform 1.10 and 1.11 can read a secret and pass it to a resource without ever writing it to state or plan files. How ephemeral blocks and _wo arguments work.

Before thisSensitive values and secrets in statePreconditions, postconditions and check blocks

Ned in the Cloud · 19 min

Ephemeral resources and write-only arguments

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Ned in the CloudReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An `ephemeral` block declares a resource that exists only during one Terraform run. It is opened when needed, renewed if it expires, closed afterwards, and never stored in state or plan files. It arrived in Terraform 1.10.
  • Ephemeral values may be used only in specific places: write-only arguments, other ephemeral blocks, `locals`, ephemeral variables and child-module outputs, provider blocks, and provisioner or connection blocks. Anywhere else is an error.
  • A write-only argument, with `_wo` in its name by convention and added in Terraform 1.11, accepts a value on create or update and stores nothing. Terraform cannot tell whether it changed, so a companion `_wo_version` argument is bumped to trigger an update.
  • OpenTofu 1.11 supports ephemeral values, ephemeral resources and write-only attributes too. Providers must opt in, so check the provider's docs for which resources have them.
  • The secret is gone from state, but it is still in the secret manager, in your CI logs if you print it, and in the real resource. Ephemerality removes one copy, not the need for access control.

Removing the copy, not hiding it

Marking a value sensitive leaves the secret in state and only hides it on screen. Everything you do afterwards, from encrypted buckets to narrow IAM, assumes the secret is in there. The better fix is to stop writing it. Terraform’s problem was that it records every value it handles. The answer, added in two steps, is a class of values and resources that Terraform uses during a run and then forgets.

Ephemeral resources

An ephemeral block looks like a data block and behaves differently. The Terraform docs say it defines temporary resources that Terraform does not store in state or plan files. It has its own lifecycle: Terraform opens it when it needs the result, renews access periodically if it needs it longer than the remote system’s expiry, and closes it after the providers that depend on it finish their work for that run phase. The docs give a Vault secret as the example, where opening takes out a lease, renewal extends it, and closing revokes it.

ephemeral "aws_secretsmanager_secret_version" "db" {
  secret_id = data.aws_secretsmanager_secret.db.id
}

That is the example in the AWS provider’s docs, which I read and did not run, since there is no Terraform or AWS account on this machine. The provider ships ephemeral versions of aws_secretsmanager_secret_version and aws_ssm_parameter, among others, in its docs folder.

Ephemeral blocks join the dependency graph like data sources. Terraform provisions them first when something depends on them. Terraform 1.10 introduced them, along with ephemeral variables and child-module outputs and the ephemeralasnull function.

Where an ephemeral value may go

This is the part that catches people. The docs list the contexts where you can refer to an ephemeral resource, and anywhere else Terraform errors at plan time:

  • a managed resource’s write-only argument
  • another ephemeral block
  • the locals block
  • a variable block with ephemeral = true
  • a child module output block with ephemeral = true
  • a provider block
  • a provisioner or connection configuration, from Terraform 1.10

That list is the point. Each is a place where the value is used and thrown away. A normal resource argument is a place where it would be stored, so it is not allowed.

Write-only arguments

Terraform 1.11 added write-only attributes. A resource can declare an argument that Terraform sends to the provider and never records. By convention it ends in _wo. The Terraform docs show a database whose password comes from an ephemeral random password:

ephemeral "random_password" "db_password" {
  length           = 16
  override_special = "!#$%&*()-_=+[]{}<>:?"
}

resource "aws_db_instance" "example" {
  instance_class        = "db.t3.micro"
  allocated_storage     = "5"
  engine                = "postgres"
  username              = "example"
  skip_final_snapshot   = true
  password_wo           = ephemeral.random_password.db_password.result
  password_wo_version   = 1
  # ...
}

That is adapted from the Terraform docs, not run by me. The random provider’s changelog confirms an ephemeral random_password was added, and says that with a write-only attribute the password is not stored in the plan or state. The AWS docs note password_wo is available in Terraform 1.11 and later, and that it requires password_wo_version.

I can show the effect with a stand-in. This script builds what a state entry would hold for each approach. It is hand-built JSON, not Terraform output.

password = random_password.db.result                    secret in state: True
password_wo = ephemeral.random_password.db.result       secret in state: False

Why a version argument

The version argument looks odd until you ask what Terraform would compare. A normal argument is in state, so a change shows as a diff. A write-only value is not in state, and the plan has nothing to compare it against. The provider documents a companion, here password_wo_version, which is stored. To rotate the password, change the version from 1 to 2, and Terraform sends the new value. Without it, a changed secret would be silent.

What this does not fix

A secret absent from state is still in the secret manager, and in the database. If your pipeline prints the value, it is in the logs. Ephemeral values can’t be exported through a normal output either. And a provider has to opt in. Many resources have no write-only argument yet, so you may still have a secret in state somewhere. Check the provider docs, and keep the controls from the previous lesson for what remains.

OpenTofu’s changelog for 1.11 lists ephemeral values, ephemeral resources and write-only attributes as supported, so the same configuration idea works there, and 1.11.x fixed several bugs around them. In either tool, this is a newer feature, and the first provider you rely on is the first place to look for gaps.

About the video

The embed is Ned Bellavance’s “Exploring Ephemeral Resources”, from his Terraform Tuesdays series, published December 2024 for Terraform 1.10, 18 minutes. His description says he explains ephemeral resources and their current limitations, and uses an Azure Key Vault secret and provider authentication as examples. Write-only arguments came a release later, and his 1.11 video on them is in deeper, from July 2025. Both use Azure, while the examples above use AWS. English was judged from the title, description and the channel’s English auto-caption track.

Where this fits

This closes the lifecycle and safety group. Next is the other half of reuse: packaging configuration so teams stop copying it, starting with what modules are and the root module.

Check yourself

  1. 01Why does a write-only argument need a separate `_wo_version` argument?

    Terraform does not store the write-only value, so on the next run it has nothing to compare and cannot see that you changed it. Changing the version number is a stored change that tells Terraform to send the new value.

  2. 02You try to use `ephemeral.aws_secretsmanager_secret_version.db.secret_string` in a normal resource argument. What happens?

    Terraform errors during planning. Ephemeral values are allowed only in contexts that do not persist them, such as write-only arguments, locals and provider blocks.

  3. 03How does a generated database password stay out of state with this feature?

    An `ephemeral "random_password"` block creates it for the run, and the database's write-only `password_wo` argument receives it. Neither the ephemeral resource nor the write-only argument is stored.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark