Module 4: Terraform Configuration

Lesson 4.8: Managing Sensitive Data

Where values end up

Technique Hidden in CLI output? Stored in state? Stored in plan file?
Plain value ✗ ✓ ✓
sensitive = true (variable, output, or provider schema) ✓ ✓ plaintext ✓
Classic data source read (for example, a Vault secret) if the schema marks it sensitive ✓ ✓
Ephemeral variable (ephemeral = true, 1.10+) ✓ ✗ ✗
Ephemeral resource (ephemeral block, 1.10+) ✓ ✗ ✗
Write-only argument (1.11+, provider-supported) ✓ ✗ (stored as null) ✗

Ephemeral values (1.10+)

Ephemeral values exist only for the duration of one Terraform operation.

Ephemeral resources are opened each run, and they can renew and close leases. They're declared with an ephemeral block and referenced as ephemeral.<type>.<name>.

You can use ephemeral values only in contexts that don't persist:

  • Other ephemeral variables and outputs
  • Locals, which themselves become ephemeral
  • provider blocks
  • Provisioner and connection blocks
  • Write-only arguments

Putting an ephemeral value into a normal resource argument or a root module output is an error. Because ephemeral variables aren't stored in saved plans, they must be supplied again when you apply a saved plan.

Write-only arguments (1.11+)

Write-only arguments accept a value during apply, pass it to the provider, and are never persisted.

  • By convention they end in _wo, for example password_wo.
  • They are usually paired with a _wo_version argument, because Terraform can't diff a value it never stored. Increment the version to push a new value.
  • They exist only where the provider implements them.
HCL
ephemeral "random_password" "app" { # random provider ≥ 3.7
  length = 24
}

resource "vault_kv_secret_v2" "app" {
  mount                = "secret"
  name                 = "app"
  data_json_wo         = jsonencode({ password = ephemeral.random_password.app.result })
  data_json_wo_version = 1 # bump to rotate
}

Secrets-management practices

  1. Never commit secrets in .tf or .tfvars files. Supply them through TF_VAR_ environment variables, a secrets manager, or HCP Terraform sensitive variables and variable sets.
  2. Read secrets from Vault using ephemeral resources where the provider supports them. Classic Vault data sources write the secret into state.
  3. Eliminate static cloud credentials. Use HCP Terraform dynamic provider credentials (OIDC workload identity), or Vault-backed dynamic credentials, which issue short-lived credentials per run.
  4. Protect state anyway. Use a remote backend with encryption at rest, strict access control, and locking (Module 6), because many provider attributes are still stored.
  5. Treat saved plan files as sensitive.
  6. nonsensitive() removes sensitivity marking. Use it only when the value is genuinely safe to display.