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.
Vault secrets engines
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.
The following diagram shows how each run obtains short-lived AWS credentials from Vault.

Vault with Terraform
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.
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.
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.