Prompt
How do I use a vault for local dev secrets?
Latest observation
A good pattern is to use a secrets manager as the source of truth, then inject secrets into your local app at runtime instead of hardcoding them in .env files.
Common local-dev approaches
1) Pull secrets from the vault when you start the app
Your app start command fetches secrets and exports them as environment variables.
Example flow:
- authenticate to vault
- read secrets for your dev environment
- load them into env vars
- start the app
Example with Vault CLI:
vault login
export DB_PASSWORD="$(vault kv get -field=password secret/myapp/dev)"
export API_KEY="$(vault kv get -field=api_key secret/myapp/dev)"
npm run dev
2) Use a wrapper script
Create a script like dev.sh that fetches secrets and then launches your app.
#!/usr/bin/env bash
set -euo pipefail
export $(vault kv get -format=json secret/myapp/dev | jq -r '.data.data | to_entries | map("\(.key)=\(.value|tostring)") | .[]')
npm run dev
This is convenient for teams, but be careful with quoting and multiline values.
3) Use Vault Agent or a sidecar to render a .env file
A safer and more flexible option is to have Vault Agent authenticate and template secrets into a local file.
Example template:
template {
source = "dev.env.tpl"
destination = ".env.local"
}
Template:
DB_PASSWORD={{ with secret "secret/myapp/dev" }}{{ .Data.data.password }}{{ end }}
API_KEY={{ with secret "secret/myapp/dev" }}{{ .Data.data.api_key }}{{ end }}
Then your app loads .env.local as usual.
This is often the best choice if:
- you have many secrets
- values change often
- you want to avoid manually exporting variables
Recommended workflow for local dev
-
Store dev secrets in a dedicated path
secret/myapp/dev- keep it separate from prod
-
Use short-lived auth
- Vault token from OIDC, LDAP, or AppRole
- avoid long-lived shared tokens
-
Inject secrets at startup
- via env vars or generated
.env.local
- via env vars or generated
-
Never commit secrets
- add
.env.localto.gitignore
- add
-
Rotate and scope tightly
- dev app should only read the dev secret path
Example setup with Vault CLI
vault kv put secret/myapp/dev DB_PASSWORD=devpass API_KEY=devkey
vault kv get secret/myapp/dev
Then run:
vault login
source <(vault kv get -format=json secret/myapp/dev \
| jq -r '.data.data | to_entries[] | "export \(.key)=\(.value|tostring)"')
npm run dev
Best practices
- Use separate namespaces/paths for dev, staging, prod
- Prefer short-lived credentials over static secret values where possible
- Use least privilege policies
- Avoid printing secrets in logs
- Don’t use vault as a replacement for config files that don’t need secrecy
If you want, I can show you:
- a Docker Compose setup with Vault
- a Node/Python/Go example
- a Vault Agent template for
.env.local