Use a secrets manager for any secret you must keep confidential (API keys, database passwords, tokens) and environment variables for non-secret configuration (ports, feature flags, log levels). This article explains the trade-offs, when each is appropriate, and how to combine them safely.
Background: Why This Question Matters
Every application needs configuration, and some of that configuration is sensitive. For years, the simplest approach was to put everything in environment variables, including secrets. But as cloud-native development matured, dedicated secrets managers appeared—AWS Secrets Manager, HashiCorp Vault, Google Secret Manager, Azure Key Vault, and open-source options like SOPS. The question of which to use is really about where you draw the line between “configuration” and “secret.”
The stakes are real: leaked credentials are a leading cause of data breaches. A 2023 report from the security firm GitGuardian found that over 10 million secrets were exposed in public GitHub commits in 2022, a 50% increase from the prior year. Most of those were environment variables accidentally committed to source control.
How Environment Variables Work
Environment variables are key-value pairs set outside your application, typically in your shell, container runtime, or CI/CD pipeline. Your code reads them at runtime via a standard interface like process.env in Node.js or os.environ in Python.
They are simple, universal, and have no official standard—they’ve been a part of Unix since the 1970s. In modern deployment, you typically set them in a Dockerfile, a Kubernetes manifest, or a cloud function configuration.
The key characteristic is that they are static: once set, they remain the same until you redeploy or restart. There is no built-in rotation, versioning, or access auditing.
How Secrets Managers Work
A secrets manager is a dedicated service that stores secrets in an encrypted vault, exposes an API to retrieve them, and provides features like:
- Encryption at rest and in transit—secrets are encrypted with keys managed by the provider.
- Access control—fine-grained policies (e.g., IAM roles) determine which applications can read which secrets.
- Rotation—you can schedule automatic rotation of credentials, reducing the risk of long-lived leaks.
- Audit logging—every access is logged, so you can see who read what and when.
- Versioning—old versions remain available for rollback.
Applications retrieve secrets at startup or on-demand via the manager’s API or SDK. Some managers offer a caching layer to reduce latency and cost.
Key Differences: Security, Rotation, and Complexity
The core difference is security posture. Environment variables are visible to anyone who can access the process environment—that includes other processes on the same host, anyone with debugging access, and anyone who can read your CI logs. They also end up in shell history, Docker image layers, and Kubernetes manifests if you’re not careful.
Secrets managers add a layer of indirection: the secret is not in your code or environment at all. It exists only in the vault, and your application fetches it over an authenticated API call. This means:
- If an attacker gains read access to your code or environment, they still don’t see the secret.
- You can revoke access instantly by changing IAM policies.
- You can rotate secrets without redeploying your application.
However, secrets managers introduce complexity: you must manage another service, handle network calls, and deal with potential downtime. If the manager is unavailable, your application may fail to start unless you implement caching.
How to Choose: A Practical Framework
Ask these questions about each piece of configuration:
- Is it a secret? If the value grants access to something (a database, an API, a third-party service), it’s a secret. If it just changes behavior (port number, log level), it’s not.
- How often does it change? Secrets that rotate frequently (e.g., database passwords) are painful to manage in environment variables because you must redeploy. Secrets managers handle rotation automatically.
- Who needs to see it? If multiple teams or services need the same secret, a central manager is easier to audit and control.
- What’s your threat model? If your app runs on a shared host or you’re dealing with highly sensitive data (PII, payment info), the extra security of a manager is worth it.
A common pattern is to use environment variables for non-secret config and a secrets manager for secrets. For example, a web app might use PORT=8080 and LOG_LEVEL=info as env vars, but fetch DATABASE_URL from the secrets manager.
Common Problems and Misconceptions
Misconception: Environment variables are secure because they’re not in code. That’s false—they’re still visible in many places. A 2021 study by North Carolina State University found that 26% of Docker images on Docker Hub contained hardcoded secrets, many in environment variables.
Problem: Secrets in CI/CD logs. If you echo an env var during a build, it appears in logs that may be accessible to a wider audience. Secrets managers can inject secrets directly into the build environment without ever printing them.
Problem: Over-reliance on a secrets manager. Some teams put everything in a manager, even non-secrets, which adds latency and cost for no benefit. It’s over-engineering.
Problem: Ignoring the secrets manager entirely. For small projects or prototypes, a secrets manager may be overkill. But as soon as you have real users or real data, start using one.
Comparing Costs and Operational Overhead
Environment variables are free—they’re a feature of your OS and runtime. Secrets managers have a cost: AWS Secrets Manager charges $0.40 per secret per month plus $0.05 per 10,000 API calls (as of 2025). Vault is free if you self-host, but you pay for the infrastructure and maintenance.
Operationally, environment variables are zero-maintenance. Secrets managers require you to set up IAM roles, configure rotation, and monitor access logs. But they save time when you need to rotate a compromised credential—with env vars, you’d have to redeploy every service that uses it.
For a small startup, the cost of a secrets manager is trivial compared to the cost of a breach. For a hobby project, it’s probably unnecessary.
Best Practices and Alternatives
If you choose environment variables, follow these rules:
- Never commit .env files to source control—add them to .gitignore.
- Use a tool like
dotenvfor local development, but load real secrets from the environment in production. - Use different values per environment (dev, staging, prod) to avoid accidental cross-environment access.
If you choose a secrets manager, consider these practices:
- Cache secrets in memory to reduce API calls and avoid startup failures.
- Set up rotation for any secret that expires or is sensitive.
- Use least-privilege IAM roles—each service can only read its own secrets.
Alternatives to a full secrets manager include:
- Encrypted files (e.g., SOPS) that you decrypt at deploy time—good for GitOps workflows.
- Kubernetes Secrets—a middle ground, but they’re just base64-encoded unless you enable encryption at rest.
- Cloud-specific tools like AWS Parameter Store (a cheaper, simpler sibling to Secrets Manager).
Frequently Asked Questions
Can I use environment variables for secrets if my app is serverless? Yes, but it’s risky. Serverless functions often have environment variables visible in the console and logs. For anything sensitive, use a secrets manager—most cloud providers offer a way to inject secrets directly into the function’s environment at runtime.
What’s the difference between AWS Secrets Manager and Parameter Store? Secrets Manager is designed for secrets with rotation, auditing, and automatic credential rotation for RDS. Parameter Store is a simpler, cheaper key-value store that can be used for both secrets and config, but it lacks native rotation and advanced features.
How do I rotate a secret without downtime? Most secrets managers support rotation. For example, you can configure automatic rotation for database passwords: the manager creates a new password, updates the database, and then your application picks it up on the next fetch. With environment variables, you’d have to redeploy with the new value.
Is it okay to use environment variables for non-secret config? Absolutely. It’s the standard practice for things like ports, feature flags, and log levels. Just make sure they’re not accidentally exposed in logs or error messages.
What if my secrets manager goes down? That’s a valid concern. Implement a caching layer: fetch the secret at startup, cache it in memory, and only refresh periodically. This way, a temporary outage won’t crash your app. You can also use a fallback to environment variables for non-critical secrets, but that defeats the purpose.
Should I use a secrets manager for a personal project? If it’s a learning project with no real data, no. But if you’re practicing for production, it’s good to learn the workflow. Start with a free tier or self-hosted Vault.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- DNF vs DNF5: Which Package Manager?
- Car Trends Making Cars Worse: 7 Downsides
- Nostalgic Classic Games Revamped on G2A?
- Nvidia RTX 4000 Super GPUs: Full Specs & Buying Guide
- Photoshop vs Lightroom AI Features Compared
- Korean SUV Making BMW and Audi Owners Question Their Choices?