Module 4: Terraform Configuration

Lab 4D: Secrets, Vault, Ephemeral Values, and Write-Only Arguments

Time
25 min
Cost
$0
Prerequisites
None listed
Objectives
0 of 8 checked

Setup

In terminal 1, start the dev server. This dev mode is for labs only: it runs in memory with a known root token.

Shell
vault server -dev -dev-root-token-id=root

In terminal 2, configure the CLI and write a test secret. The dev server mounts KV v2 at secret/.

Shell
export VAULT_ADDR=http://127.0.0.1:8200 VAULT_TOKEN=root
vault kv put secret/db password=S3cr3t-From-Vault

Starting file

Create ~/tf-course/lab4d/main.tf:

~/tf-course/lab4d/main.tfHCL
terraform {
  required_version = "~> 1.12.0"

  required_providers {
    vault = {
      source  = "hashicorp/vault"
      version = ">= 4.7.0, < 6.0.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.7"
    }
  }
}

provider "vault" {} # reads VAULT_ADDR and VAULT_TOKEN from the environment

variable "api_token" {
  type      = string
  sensitive = true
  ephemeral = true # never written to state or plan
}

# CLASSIC read: the secret value WILL be stored in state
data "vault_kv_secret_v2" "db_classic" {
  mount = "secret"
  name  = "db"
}

# EPHEMERAL read: fetched each run, never stored
ephemeral "vault_kv_secret_v2" "db" {
  mount = "secret"
  name  = "db"
}

ephemeral "random_password" "app" {
  length  = 24
  special = false
}

# WRITE-ONLY argument: sent to Vault, stored as null in state
resource "vault_kv_secret_v2" "app" {
  mount = "secret"
  name  = "app"
  data_json_wo = jsonencode({
    app_password = ephemeral.random_password.app.result
    db_password  = ephemeral.vault_kv_secret_v2.db.data["password"]
    api_token    = var.api_token
  })
  data_json_wo_version = 1
}

Steps

Shell
cd ~/tf-course/lab4d && terraform init

Shell
terraform providers schema -json | grep -o '"data_json_wo[a-z_]*"' | sort -u
terraform providers schema -json | grep -c 'ephemeral_resource_schemas'

Expected output: the first command prints "data_json_wo" and "data_json_wo_version". The second prints a count of at least 1. If either is missing, your provider version lacks the feature. Run terraform init -upgrade, or check the Vault provider changelog.

Shell
export TF_VAR_api_token=tok-123-ephemeral
terraform apply -auto-approve

Expected output: Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Ephemeral resources show as opened and closed during the run, but they aren't counted as added resources.

Shell
vault kv get secret/app

Expected output: app_password (24 characters), db_password = S3cr3t-From-Vault, and api_token = tok-123-ephemeral.

Shell
grep -c 'S3cr3t-From-Vault' terraform.tfstate   # expect ≥ 1: leaked by the classic data source
grep -c 'tok-123-ephemeral' terraform.tfstate   # expect 0
terraform state show vault_kv_secret_v2.app | grep data_json_wo

Expected output:

  • The Vault password is in state, because of data.vault_kv_secret_v2.db_classic.
  • The API token is absent.
  • data_json_wo shows as null or absent, while data_json_wo_version = 1 is stored.

Add this to main.tf and run terraform plan:

main.tfHCL
output "leak" {
  value = ephemeral.random_password.app.result
}

Expected output: an error that an ephemeral value isn't allowed in this context. Remove the block.

Change data_json_wo_version = 2, run terraform apply -auto-approve, then run vault kv get secret/app. The app_password changes. Without the version bump, Terraform has no stored value to compare against, so it would never push an update.

Delete the data "vault_kv_secret_v2" "db_classic" block and apply. Run grep -c 'S3cr3t-From-Vault' terraform.tfstate again. Expected output: 0 for the current state. Older copies may still exist in terraform.tfstate.backup. That shows why a secret, once written to state, should be treated as exposed and rotated.

Cleanup

Shell
terraform destroy -auto-approve
unset TF_VAR_api_token
rm -f terraform.tfstate.backup

Stop the Vault dev server with Ctrl-C. Its data is discarded.

Lab 4D complete. Every step is checked.

This lab has no verification checklist in the course file, so it counts as complete when every step is checked.