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_v2data source leaks the secret - The ephemeral read: the
ephemeral "vault_kv_secret_v2"block - The write-only write:
data_json_woand 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 -devis 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
sensitivehides, 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:
| Argument | Persisted 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_versionproduces no plan: Terraform does not track what it does not store; - raising the version from
1to2produces 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_v2data source leaks the secret into the state (and is deprecated);sensitivechanges nothing. - The
ephemeral "vault_kv_secret_v2"block reads without persisting; its value only goes into an ephemeral context. data_json_wowrites without persisting;data_json_woanddata_jsonarenullanddatais empty in the state.- Only
data_json_wo_versionremains 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
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Policy as code and governance in HCP Terraform
A new full quiz with random questions
Work again only on the questions you missed
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.
Short answer
Because it copies the secret in clear into the state, and it is deprecated.Details
sensitive_values marks the value true but hides nothing: the password stays readable in terraform.tfstate and terraform show -json. Use an ephemeral "vault_kv_secret_v2" block instead, which reads without persisting.Short answer
Anephemeral "vault_kv_secret_v2" block (vault provider 5.x).Details
ephemeral "vault_kv_secret_v2" "source" {
mount = "kvv2"
name = "app/db"
}
The value is available during the operation but never written to the state or the plan. It can only go into an ephemeral context: provider, provisioner, or a write-only argument.Short answer
ephemeral.vault_kv_secret_v2.<name>.data["<key>"].Details
ephemeral.vault_kv_secret_v2.source.data["password"]
The data map holds the keys of the KV v2 secret. This reference can only feed an ephemeral context (a data_json_wo argument for instance); anywhere else, Terraform raises "Invalid use of ephemeral value".Short answer
data_json persists in the state; data_json_wo (write-only) never persists.Details
Withdata_json_wo, terraform show -json shows data_json_wo = null, data_json = null and data = {}: only data_json_wo_version remains. The write-only argument forms a mandatory pair with that version number.Short answer
Becausedata_json_wo_version did not change: the write-only value is only sent again when the version changes.Details
Terraform does not store the secret, so it cannot detect that it changed on the Vault side: aplan stays empty. To push the new value, increment data_json_wo_version (1 to 2). That is the rotation mechanism.Short answer
No, with the ephemeral read plusdata_json_wo write combination.Details
Theephemeral block produces no entry in the state; data_json_wo leaves null there. The secret's value therefore appears nowhere in terraform show -json or in terraform.tfstate. Check it by searching for the value in both: zero occurrences.Short answer
No.vault server -dev is enough: in-memory server, unsealed, root token of your choice.Details
vault server -dev -dev-root-token-id=root
export VAULT_ADDR=http://127.0.0.1:8200 VAULT_TOKEN=root
vault secrets enable -path=kvv2 kv-v2
vault kv put kvv2/app/db username=app password=change-me
Vault Community Edition is free and open, with no licence or sign-up.