Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Reading Vault secrets from Terraform without freezing them in state

By updated

Improve this page

Logo Terraform

Reading a HashiCorp Vault secret from Terraform takes a few lines. The real problem is reading it without it ending up in clear in the state (the file where Terraform records the state of your infrastructure). The historical way of doing it, a data source, copies the value into terraform.tfstate, and the sensitive_values field of the state marks it true without hiding anything at all.

This guide shows the trap, then the right combination: an ephemeral read and a write-only write, so that an external secret travels from Vault to its recipient without leaving a trace on the Terraform side. Every example was run on Terraform v1.15.4 with the hashicorp/vault provider 5.x, against a vault server -dev server.

This topic matches sub-objective 2f of the Terraform Professional certification, "Analyze best practices for managing sensitive data, such as using Vault for secrets management". This guide covers one precise practice, keeping a Vault secret out of the state; the other practices (ephemeral values, write-only arguments, sensitive) belong to the guides listed in the prerequisites.

Two terms come up right away. An ephemeral read provides a value that only exists for the duration of the operation. A write-only write is an argument that Terraform sends to the provider without ever remembering it.

What you will learn

  • The trap: why the vault_kv_secret_v2 data source leaks the secret
  • The ephemeral read: the ephemeral "vault_kv_secret_v2" block
  • The write-only write: data_json_wo and its version argument
  • The pair ephemeral plus write-only, end to end without a trace
  • Rotation: driving the resend through a version integer

Prerequisites

  • Terraform 1.11 or later (installing Terraform, in French): ephemeral resources and write-only arguments require it.
  • A Vault server: vault server -dev is enough, in memory, unsealed, with a root token. Vault Community Edition is free for this use, but licensed under BUSL 1.1 (since Vault 1.15), so not open source: production use is allowed, except offering Vault to third parties in competition with the vendor's paid product. The examples read a secret from a KV v2 engine, the Vault engine that stores key-value pairs and keeps their versions.
  • Ephemeral values and write-only arguments (ephemeral values, write-only, in French).
  • What sensitive hides, and what it does not (sensitive in Terraform, in French): the warning box in the section on the trap assumes the notion.

The trap: the data source copies the secret in clear

The historical way of reading a KV v2 secret is a data source:

data "vault_kv_secret_v2" "source" {
mount = "kvv2"
name = "app/db"
}

It works, but it copies the value into the state. The provider also issues a deprecation warning on this data source.

The ephemeral read: ephemeral "vault_kv_secret_v2"

The hashicorp/vault provider 5.x exposes the same read in ephemeral form. An ephemeral block produces a value available during the operation, but never written to the state or the plan:

ephemeral "vault_kv_secret_v2" "source" {
mount = "kvv2"
name = "app/db"
}

You reference its result with ephemeral.vault_kv_secret_v2.source.data["password"]. This value can only go into an ephemeral context: a provider configuration, a provisioner, or a write-only argument. The latter completes the chain. The general mechanism is described in the official reference on ephemeral resources.

The write-only write: data_json_wo

The vault_kv_secret_v2 resource has two ways of writing its content:

ArgumentPersisted in the state?
data_json (ordinary)yes (marked sensitive, but present)
data_json_wo (write-only)no, never

The write-only argument forms a mandatory pair with a version number:

resource "vault_kv_secret_v2" "cible" {
mount = "kvv2"
name = "app/copie"
data_json_wo = jsonencode({ password = ephemeral.vault_kv_secret_v2.source.data["password"] })
data_json_wo_version = 1
}

After the apply, in terraform show -json: data_json_wo is null, data_json is null, data is an empty object; only data_json_wo_version remains, with a very real number.

The ephemeral plus write-only pair

End to end, the secret never touches the disk on the Terraform side: read by an ephemeral block (no state), written by a data_json_wo (no state). It travels in memory during the operation, from the source KV to the target KV. This combination leaves no trace, neither in clear nor in derived form.

Rotation: driving the resend through the version

Like any write-only argument, data_json_wo is only sent again to the provider when its version number changes. A direct consequence, checked at runtime:

  • changing the secret in the source KV without touching data_json_wo_version produces no plan: Terraform does not track what it does not store;
  • raising the version from 1 to 2 produces a plan, and the apply pushes the new value into the target, whose KV metadata moves to version 2.

That is the rotation mechanism: you increment an integer, you never handle the secret in clear in the configuration.

Put it into practice

You fetch secrets from HashiCorp Vault with the vault provider, and you consume them without freezing them in clear in the state. It is the use case that brings Terraform closer to a secret manager rather than a value store, and validation checks what the state really holds at the end.

Key points

  • The vault_kv_secret_v2 data source leaks the secret into the state (and is deprecated); sensitive changes nothing.
  • The ephemeral "vault_kv_secret_v2" block reads without persisting; its value only goes into an ephemeral context.
  • data_json_wo writes without persisting; data_json_wo and data_json are null and data is empty in the state.
  • Only data_json_wo_version remains and drives rotation: without incrementing it, a new source secret is ignored.
  • The ephemeral plus write-only pair leaves no trace on the Terraform side.

Knowledge check

Check that the essentials of this lesson are in place. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

3 questions
3 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer

FAQ: frequently asked questions

The questions below cover points that tend to block people: the data source that leaks, how to read without persisting, and why a secret changed on the Vault side is not picked up without a version change. Each answer gives the matching check.