Skip to main content

HashiCorp Vault

Centralize secrets management across cloud and on-premises. Store static secrets, generate dynamic credentials that expire automatically, and control access with fine-grained policies and full audit logging.

✓ KV v2 versioned secrets ✓ Dynamic database credentials ✓ Kubernetes auth + Agent sidecar ✓ Full audit logging

Dev Server Quickstart

# Install Vault CLI
brew install vault    # macOS
# Linux: download from releases.hashicorp.com/vault/

# Start a dev server (in-memory, do NOT use in production)
vault server -dev -dev-root-token-id="root"

# In a new terminal, set env vars
export VAULT_ADDR='http://127.0.0.1:8200'
export VAULT_TOKEN='root'

# Verify connectivity
vault status

KV v2 Secrets Engine

# Enable KV v2 at a path
vault secrets enable -path=secret -version=2 kv

# Write a secret
vault kv put secret/my-app/config \
  db_url="postgres://user:pass@db:5432/myapp" \
  api_key="sk-abc123"

# Read the latest version
vault kv get secret/my-app/config

# Read a specific version
vault kv get -version=1 secret/my-app/config

# Update (creates new version)
vault kv put secret/my-app/config \
  db_url="postgres://user:newpass@db:5432/myapp" \
  api_key="sk-abc123"

# List all versions (metadata)
vault kv metadata get secret/my-app/config

# Delete (soft delete — still recoverable)
vault kv delete secret/my-app/config

# Permanently destroy version 1
vault kv destroy -versions=1 secret/my-app/config

Dynamic Database Credentials

# Enable the database secrets engine
vault secrets enable database

# Configure PostgreSQL connection (Vault connects with a superuser to create creds)
vault write database/config/my-postgres \
  plugin_name=postgresql-database-plugin \
  allowed_roles="app-role" \
  connection_url="postgresql://{{username}}:{{password}}@postgres:5432/myapp?sslmode=disable" \
  username="vault_superuser" \
  password="superpassword"

# Create a role with a SQL template for credential creation
vault write database/roles/app-role \
  db_name=my-postgres \
  creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';
    GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" \
  default_ttl="1h" \
  max_ttl="24h"

# Request dynamic credentials (each call returns unique creds)
vault read database/creds/app-role
# Key                Value
# lease_duration     1h
# username           v-app-role-xK3mQ9pZ2r-1234567890
# password           A1b2C3d4E5f6G7h8I9j0

Kubernetes Auth and Vault Agent Sidecar

# 1. Enable Kubernetes auth method in Vault
vault auth enable kubernetes

# 2. Configure with cluster details (run inside the cluster)
vault write auth/kubernetes/config \
  kubernetes_host="https://$KUBERNETES_PORT_443_TCP_ADDR:443" \
  token_reviewer_jwt="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
  kubernetes_ca_cert=@/var/run/secrets/kubernetes.io/serviceaccount/ca.crt

# 3. Create a Vault policy for the app
vault policy write my-app - <<EOF
path "secret/data/my-app/*" {
  capabilities = ["read"]
}
path "database/creds/app-role" {
  capabilities = ["read"]
}
EOF

# 4. Create a Vault role binding Kubernetes SA to policy
vault write auth/kubernetes/role/my-app \
  bound_service_account_names=my-app-sa \
  bound_service_account_namespaces=production \
  policies=my-app \
  ttl=24h
# Pod with Vault Agent sidecar annotations
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    metadata:
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/role: "my-app"
        # Inject KV secret to /vault/secrets/config.txt
        vault.hashicorp.com/agent-inject-secret-config.txt: "secret/data/my-app/config"
        # Custom template for the secret file format
        vault.hashicorp.com/agent-inject-template-config.txt: |
          {{- with secret "secret/data/my-app/config" -}}
          DB_URL={{ .Data.data.db_url }}
          API_KEY={{ .Data.data.api_key }}
          {{- end -}}
    spec:
      serviceAccountName: my-app-sa
      containers:
        - name: my-app
          image: my-app:latest
          # App reads secrets from /vault/secrets/config.txt

AppRole Authentication (for CI/CD)

# Enable AppRole auth
vault auth enable approle

# Create AppRole for a CI/CD pipeline
vault write auth/approle/role/ci-pipeline \
  secret_id_ttl=10m \
  token_num_uses=10 \
  token_ttl=20m \
  token_max_ttl=30m \
  policies=my-app

# Get the Role ID (static, safe to store)
vault read auth/approle/role/ci-pipeline/role-id
# role_id: 675a50e7-cfe0-be28-...

# Generate a Secret ID (rotate frequently)
vault write -f auth/approle/role/ci-pipeline/secret-id
# secret_id: ed0a642f-2acf-...

# In CI/CD: login with role_id + secret_id to get a Vault token
vault write auth/approle/login \
  role_id="675a50e7-cfe0-be28-..." \
  secret_id="ed0a642f-2acf-..."
# Returns: token, lease_duration, etc.

Vault vs AWS Secrets Manager vs Doppler

Feature HashiCorp Vault AWS Secrets Manager Doppler
Dynamic secrets Yes (DB, AWS, GCP, SSH, PKI) Limited (RDS rotation) No
Cloud agnostic Yes AWS only Yes
Kubernetes integration Native (Agent, Operator) Via CSI driver Via operator
Encryption-as-a-service Yes (Transit engine) Via KMS No
Operational overhead High (self-managed) Low (managed) Low (managed)
Cost Free OSS / HCP Vault $0.40/secret/month Free tier / $6.99+/user

Frequently Asked Questions

What is HashiCorp Vault and what problems does it solve?▼

HashiCorp Vault is a secrets management tool that centralizes the storage, access control, and auditing of sensitive data: API keys, database passwords, TLS certificates, SSH keys, and cloud provider credentials. Without Vault, secrets typically live in environment variables, .env files, or config maps — scattered, often version-controlled by accident, with no audit trail. Vault solves this by providing a single encrypted store with fine-grained access policies, dynamic secret generation (credentials that expire automatically), full audit logging, and multiple authentication methods.

What is the difference between KV v1 and KV v2 secrets engines?▼

KV (Key-Value) v1 stores secrets without versioning — each write overwrites the previous value. KV v2 adds versioning: every write creates a new version, and you can read, restore, or delete specific versions. KV v2 also adds metadata like created_time and deletion_time. For most production use cases, use KV v2 because it gives you an audit trail of how secrets changed over time and the ability to roll back to a previous version if a secret is accidentally overwritten. Enable with: vault secrets enable -version=2 kv.

What are dynamic secrets and why are they more secure?▼

Dynamic secrets are credentials generated on-demand by Vault and automatically revoked after a configurable TTL (time-to-live). For example, the Vault database secrets engine can create a unique PostgreSQL username and password when an application requests credentials. These credentials expire after 1 hour and are automatically deleted. This is far more secure than static credentials because: compromised credentials expire quickly, each application gets unique credentials (breach isolation), and Vault's audit log shows exactly who requested which credential and when.

How does Vault Kubernetes authentication work?▼

The Kubernetes auth method lets pods authenticate to Vault using their Kubernetes service account JWT token (automatically mounted at /var/run/secrets/kubernetes.io/serviceaccount/token). The pod sends this token to Vault, Vault verifies it with the Kubernetes API server, and if valid, returns a Vault token with the policies you configured for that service account. This means pods authenticate without needing any pre-shared secrets. The Vault Agent sidecar or the Vault Secrets Operator automates this flow, injecting secrets directly into the pod's filesystem.

What is the Vault Agent sidecar and how does it work?▼

The Vault Agent is a daemon that runs alongside your application as a sidecar container (or init container). It authenticates to Vault automatically, requests secrets, renders them to a file or environment variable on a shared volume, and renews them before they expire. You configure it with annotations on your pod: vault.hashicorp.com/agent-inject: 'true', vault.hashicorp.com/role: 'my-app', vault.hashicorp.com/agent-inject-secret-config.txt: 'secret/my-app/config'. The agent writes the secret to /vault/secrets/config.txt which your app reads at runtime.

How does Vault compare to AWS Secrets Manager and Doppler?▼

AWS Secrets Manager is AWS-native, tightly integrated with RDS (automatic rotation), Lambda, and IAM — easiest for AWS-only workloads with a simple UI. It costs $0.40/secret/month. Vault is cloud-agnostic with a broader feature set: dynamic secrets for many backends (databases, AWS, GCP, Azure, SSH), PKI engine, Transit encryption-as-a-service, and support for on-premises deployments. Vault requires more operational overhead. Doppler is a developer-friendly SaaS secrets manager with excellent DX (CLI, GitHub integration, environment syncing) but fewer advanced features than Vault. Choose Vault for multi-cloud or complex secrets workflows, AWS Secrets Manager for pure AWS, Doppler for simplicity.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →