Lesson 3 of 3 Sensitive Data and Secrets Management

Secrets Management with Vault

HashiCorp Vault is the secrets management tool most often paired with Terraform. This lesson explains what Vault provides and how Terraform reads secrets from it safely.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the static and dynamic secrets that Vault provides.
  • Distinguish reading Vault secrets through data sources and ephemeral resources.

What Vault provides

HashiCorp Vault stores and controls access to secrets: passwords, API keys, certificates and encryption keys. Access is governed by policies attached to identities, every access is audit-logged, and secrets can have leases that expire. Vault's secrets engines provide different kinds of secrets.

  • Static secrets are stored and versioned in the key/value version 2 (KV v2) engine.
  • Dynamic secrets are generated on demand with a short lease, such as temporary AWS credentials from the AWS secrets engine or a database user from the database secrets engine; they are revoked automatically when the lease expires.
  • Encryption as a service through the transit engine, and certificates through the PKI engine.
Match the pairs

Vault secrets engines

Exam skills 4.8.4

Match each Vault secrets engine with what it provides.

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

Reading secrets with the Vault provider

The hashicorp/vault provider connects Terraform to Vault, typically with the VAULT_ADDR and VAULT_TOKEN environment variables or another auth method. Older configurations read secrets with data sources, such as data "vault_kv_secret_v2". That works, but the secret is then stored in state like any data source result. With Terraform 1.10 and later, use the provider's ephemeral resources instead.

ephemeral "vault_kv_secret_v2" "db" {
  mount = "secret"
  name  = "app/db"
}

resource "aws_db_instance" "main" {
  # ...
  password_wo         = ephemeral.vault_kv_secret_v2.db.data["password"]
  password_wo_version = 1
}

ephemeral "vault_aws_access_credentials" "deploy" {
  backend = "aws"
  role    = "deploy"
}

provider "aws" {
  access_key = ephemeral.vault_aws_access_credentials.deploy.access_key
  secret_key = ephemeral.vault_aws_access_credentials.deploy.secret_key
}

The first ephemeral resource reads a static secret from KV v2 and passes it to a write-only argument. The second requests dynamic AWS credentials, valid for a short lease, and uses them to configure the AWS provider, so no long-lived AWS key exists anywhere in the configuration.

Vault data source
Secret stored in stateYes
Minimum Terraform versionAny
Where the value can be usedAnywhere
Choose when Legacy configurations only
Vault ephemeral resource
Secret stored in stateNo
Minimum Terraform version1.10
Where the value can be usedEphemeral contexts, such as write-only arguments and provider blocks
Choose when New configurations

The following diagram shows how each run obtains short-lived AWS credentials from Vault.

Replacing a long-lived AWS key with credentials that expire after each run. The pipeline authenticates to Vault with its own identity; Vault's AWS secrets engine issues credentials for a role scoped to the deployment permissions; the ephemeral vault_aws_access_credentials resource passes them to the AWS provider for the run only. The static key is deleted and nothing is written to state.
The credentials expire after the run.
Scenario questions

Vault with Terraform

Exam skills 4.8.4

Choose how Terraform should obtain secrets from Vault.

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

Vault and HCP Terraform

Terraform can also manage Vault itself, creating mounts, policies, roles and auth methods as code. In HCP Terraform, dynamic provider credentials can give each run a short-lived Vault token through workload identity, and Vault-backed dynamic credentials can in turn issue cloud credentials, so no static secret is stored in the workspace.

Worked example

Removing a static key

A pipeline stores a long-lived AWS access key as a CI secret so that Terraform can deploy. The security team wants credentials that expire after each run, and the company already runs Vault.

The decisive requirement is short-lived credentials without a stored key.

The team enables Vault's AWS secrets engine with a role scoped to the deployment permissions, and the configuration uses ephemeral "vault_aws_access_credentials" to obtain credentials for each run and pass them to the AWS provider block. The pipeline authenticates to Vault with its own identity, the long-lived key is deleted, and the credentials are never written to state.

Check yourself

Why are Vault ephemeral resources preferred over Vault data sources?

Show answer

Data sources store the secret in state; ephemeral resources provide it only for the run.

Exam Tips

Remember for the exam

  • Vault stores static secrets (KV v2) and generates dynamic, leased secrets.
  • Vault data sources put secrets in state; ephemeral resources do not.
  • Pass ephemeral secrets to write-only arguments and provider blocks.