Sub topic 15
When to Move to a Secret Manager
This chapter assumes you can use a shell and Git, read short Python snippets, and already load your LLM API key from an environment variable or a git-ignored .env file. It explains when that setup stops being enough and what a dedicated secret manager adds.
What a secret manager is
A secret manager is a service that stores secrets in encrypted form. It hands them out only to callers that prove who they are, and it records each access. Common examples are AWS Secrets Manager, Google Secret Manager, Azure Key Vault, HashiCorp Vault, and the secret features built into hosting platforms.
Your application does not carry the key. At startup, or when it needs the key, it asks the manager for it. The manager checks the caller's identity, such as a cloud role attached to the server, and returns the value.
Signs that environment variables are no longer enough
Environment variables and .env files work well for one developer and one small service. Consider a secret manager when you see any of these:
- Several people or services need the same key, and you are pasting it into chat or tickets to share it.
- You must rotate keys on a schedule, and updating every server by hand is slow or error-prone.
- You need to know who or what read a key, and when.
- You deploy to many environments, such as staging and production, and keep losing track of which key lives where.
- A compliance rule or customer contract requires encrypted storage and access logs.
If none of these apply, the setup you already have is reasonable. A secret manager adds cost and moving parts, so adopt one for a reason.
What you gain and what you take on
| Feature | Environment variables or `.env` | Secret manager |
|---|---|---|
| Encrypted at rest | No, plain text in the process or file | Yes |
| Access control per secret | No | Yes, by role or identity |
| Audit log of reads | No | Yes |
| Rotation support | Manual | Often built in |
| Setup effort | Minimal | Higher: accounts, roles, client code |
| Extra cost and dependency | None | Yes, and your app fails if the manager is unreachable |
A secret manager does not remove the need for the earlier habits. Once the application fetches the key, it still holds it in memory. Do not log it, and keep it on the server.
How the flow works
Example: reading the key from AWS Secrets Manager
This example assumes the server has an AWS role allowed to read a secret named llm-api-key, and that boto3 is installed (pip install boto3). The role supplies the credentials, so no second secret sits in your code.
import boto3
def get_llm_api_key() -> str:
client = boto3.client("secretsmanager")
result = client.get_secret_value(SecretId="llm-api-key")
return result["SecretString"]
api_key = get_llm_api_key()
print(f"Fetched a key of {len(api_key)} characters")The code prints only the length, as in the earlier verification chapter.
Common mistakes
- Fetching the secret on every request. This adds latency and cost. Fetch once at startup, or cache the value for a short time, and refresh it after a rotation.
- Giving the app's role access to every secret. Allow it to read only the secrets it needs.
- Putting the manager's own credentials in a
.envfile. Use the platform's identity, such as an attached role, so there is no long-lived credential to leak.
Exercise
Your team of six shares one LLM API key through a pinned chat message, and your security lead asks who used it last week. Why would a secret manager help, and what should you do in code to keep the move small?
Show answer
A secret manager gives each person or service its own access, so it can log who read the key and when, and it lets you rotate the key without telling everyone by chat. Keep key loading in a single function, so you change only that function to fetch from the manager.