Lesson 2 of 3 Sensitive Data and Secrets Management

Ephemeral Values and Write-Only Arguments

Terraform 1.10 and 1.11 added ways to use secrets without ever storing them. This lesson covers ephemeral variables, outputs and resources, write-only arguments and how they work together.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain ephemeral variables, outputs and resources.
  • Describe how write-only arguments and their version arguments work.
  • Identify the contexts where ephemeral values are allowed.

Ephemeral input variables and outputs

An input variable declared with ephemeral = true accepts a value for the current run only; Terraform does not record it in plan or state files. An output in a child module can also be ephemeral, which lets a module pass an ephemeral value to its caller. Root module outputs cannot be ephemeral.

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

Ephemeral values may be used only in ephemeral contexts: other ephemeral variables and outputs, local values (which then become ephemeral), ephemeral resources, provider and provisioner blocks, and write-only arguments. Using one in an ordinary resource argument is an error, because that argument would be stored in state.

Sort into groups

Can an ephemeral value go here?

Exam skills 4.8.2

Decide whether an ephemeral value may be used in each place.

Log in to try this activity Interactive activities are free with an account.

Ephemeral resources

An ephemeral resource is declared with an ephemeral block and referenced with the ephemeral. prefix. Terraform opens it during each plan and apply to obtain a temporary value, may renew it during a long run, and closes it at the end; nothing is stored. Providers offer ephemeral resources for generated passwords, secrets from secret stores and short-lived tokens.

ephemeral "random_password" "db" {
  length  = 24
  special = true
}

ephemeral "aws_secretsmanager_secret_version" "api_key" {
  secret_id = "prod/api-key"
}

provider "example" {
  token = ephemeral.aws_secretsmanager_secret_version.api_key.secret_string
}
Ephemeral resource
A block that obtains a temporary value during a run and is never stored in plan or state.
Ephemeral context
A place where ephemeral values are allowed: ephemeral variables and outputs, locals, ephemeral resources, provider and provisioner blocks, write-only arguments.
Write-only argument
A managed resource argument that is sent to the provider but never persisted.
True or false

Ephemeral resources

Exam skills 4.8.2

Check how ephemeral resources differ from managed resources and data sources.

Log in to try this activity Interactive activities are free with an account.

Write-only arguments

Managed resources still need secrets, such as a database's master password. A write-only argument, available from Terraform 1.11, accepts a value, usually an ephemeral one, passes it to the provider during apply and never stores it in plan or state. By convention their names end in _wo, such as password_wo on aws_db_instance or value_wo on aws_ssm_parameter.

resource "aws_db_instance" "main" {
  identifier          = "app-db"
  engine              = "postgres"
  instance_class      = "db.t4g.micro"
  allocated_storage   = 20
  username            = "app_admin"
  password_wo         = ephemeral.random_password.db.result
  password_wo_version = 1
  skip_final_snapshot = true
}

Because Terraform keeps no copy of the value, it cannot tell when it changes. Each write-only argument therefore has a companion version argument, here password_wo_version, which is stored. Terraform sends the write-only value on create and again whenever the version changes, so rotating the password means increasing the version to 2 and applying.

Regular argument
Stored in state and planYes
Accepts ephemeral valuesNo
Detects changes to the valueYes
Write-only argument
Stored in state and planNo
Accepts ephemeral valuesYes
Detects changes to the valueNo; change the _wo_version argument

The following diagram shows an ephemeral password reaching two write-only arguments.

Creating a PostgreSQL database whose password never reaches Terraform state. An ephemeral random_password feeds two write-only arguments, the database's password_wo and the Secrets Manager secret version's secret_string_wo, each with a version argument. Neither plan nor state contains the password. Increasing both versions generates and writes a new password in one apply.
The resources are examples.
Scenario questions

Write-only arguments

Exam skills 4.8.3

Apply write-only arguments and their version arguments.

Log in to try this activity Interactive activities are free with an account.
Worked example

A secret that never touches state

A security policy forbids database passwords in Terraform state. A team must create a PostgreSQL database with Terraform and make the password available to applications through AWS Secrets Manager.

The decisive requirement is that the password is never persisted by Terraform.

The team generates the password with ephemeral "random_password", passes it to the database's password_wo argument and to the secret_string_wo write-only argument of an aws_secretsmanager_secret_version, each with a version argument set to 1. Neither the plan nor the state contains the password. To rotate it, they increase both version arguments: a new ephemeral value is generated and written to both places in one apply.

Check yourself

How do you make Terraform send a new value to a write-only argument?

Show answer

Change its companion version argument, such as password_wo_version, because Terraform keeps no copy of the value to compare.

Exam Tips

Remember for the exam

  • ephemeral = true on variables and child module outputs keeps values out of state.
  • Ephemeral resources use ephemeral blocks and the ephemeral. prefix.
  • Write-only (_wo) arguments are never stored; bump _wo_version to send a new value.
  • Ephemeral values only work in ephemeral contexts.