Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Operations and Integration

Vault Agent: Auto-Auth, Caching and Secret Templates

Use Vault Agent so applications need no Vault code: auto-auth, token sinks, caching, rendering secrets into files with templates, exec mode and a template renderer stand-in.

Teaching every application to log in to Vault, renew tokens, re-read leases and handle failures is a lot of code. Vault Agent is a client daemon that does it for you. It runs next to your application (a sidecar, a systemd service or an init container), authenticates automatically, keeps the token alive, and writes secrets to files or environment variables. Applications just read a file.

Vault Agent is not available on this site. The HCL is real configuration; the JavaScript block is a stand-in that renders a simple template so you can see the idea.

What Agent does

  • Auto-auth: logs in with a method (Kubernetes, AppRole, AWS, Azure, GCP, JWT/OIDC...), renews the token and logs in again when it expires.
  • Sinks: writes the token to a file that the app or another tool can read.
  • Caching: acts as a local API proxy that caches tokens and leases, reducing load on Vault.
  • Templating: renders Consul-template-style files from secrets and re-renders when the secret changes or a lease nears expiry.
  • Process supervisor (exec): starts your program with secrets in environment variables and restarts it on change.

A configuration

HCL
pid_file = "/run/vault-agent.pid"

vault {
  address = "https://vault.example.internal:8200"
}

auto_auth {
  method "approle" {
    config = {
      role_id_file_path   = "/etc/vault/role_id"
      secret_id_file_path = "/etc/vault/secret_id"
      remove_secret_id_file_after_reading = true
    }
  }
  sink "file" {
    config = { path = "/run/vault/token" }
  }
}

template {
  source      = "/etc/agent/db.ctmpl"
  destination = "/app/config/db.properties"
  perms       = "0640"
  command     = "systemctl reload myapp"
}

Run it with vault agent -config=agent.hcl.

Template syntax

Templates use Go template syntax with Vault functions:

TEXT
{{ with secret "secret/data/myapp/db" }}
db.host={{ .Data.data.host }}
db.user={{ .Data.data.user }}
db.password={{ .Data.data.password }}
{{ end }}

For a dynamic secret, {{ with secret "database/creds/readonly" }} renders a fresh username and password; the agent renews the lease and re-renders the file before it expires, then runs the command so your app reloads. See leases and database credentials.

JS Stand-in: render a template

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Exec mode and environment variables

If your program only knows environment variables, use env_template plus exec:

HCL
env_template "DB_PASSWORD" {
  contents = "{{ with secret \"secret/data/myapp/db\" }}{{ .Data.data.password }}{{ end }}"
}
exec {
  command                   = ["/app/server"]
  restart_on_secret_changes = "always"
}

Agent then runs /app/server with DB_PASSWORD set, and restarts it when the secret changes.

Caching and security notes

  • Enable the cache and listener blocks so your app talks to http://127.0.0.1:8100; Agent attaches the token.
  • Keep the Unix socket or loopback listener private; anything that can reach it acts with the agent's token.
  • Files with secrets should be on a memory-backed volume (tmpfs, or an emptyDir with medium: Memory in Kubernetes) and have tight permissions.
  • Use remove_secret_id_file_after_reading with AppRole so the secret ID is single use.
  • On Kubernetes, the Agent Injector adds the sidecar for you through annotations (integration).
JS Stand-in: when to re-render

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does auto-auth do?

    Auto-auth manages the token lifecycle for the client.
  2. 2. How does an app get a secret with the template feature?

    Agent renders templates into files and re-renders when values change.
  3. 3. What does the template command setting do?

    The command hook lets the app pick up new values.
  4. 4. Where should rendered secret files preferably live?

    tmpfs avoids writing secrets to persistent disk.

Summary

  • Agent handles authentication, renewal, caching and secret delivery.
  • Templates render secrets into files and re-render on change.
  • Exec mode injects environment variables and restarts the app.
  • Protect the agent listener and keep rendered files in memory-backed storage.

Next: CI/CD and Kubernetes Integration