Environment variables explained: how apps get their configuration in the cloud
Environment variables are named values that a program can read from its environment when it runs, like DATABASE_URL or PORT. They're a standard way to configure apps without changing code.
Why they're used so widely:
- The same code can run in development, testing and production with different settings.
- Secrets stay out of your code repository, like database passwords and API keys.
- Nearly every hosting platform, container system and CI tool supports them.
How it usually works:
- Locally, many projects use a
.envfile loaded by a library during development. Add it to.gitignoreso it's never committed, and share a.env.examplewith placeholder values instead. - In the cloud, you set variables in the hosting platform's dashboard or configuration, or through a secrets manager.
Beginner mistakes:
- Committing the
.envfile to a public repository. If that happens, rotate the exposed secrets immediately; deleting the file doesn't erase history. - Putting secrets in frontend code. Build tools often bake certain variables into browser JavaScript, where anyone can read them.
- Typos in names, leading to confusing "undefined" errors. Validate required variables at startup.
- Forgetting to restart or redeploy after changing a variable.
For more sensitive setups, dedicated secret managers add access control, auditing and rotation.
How do you manage environment variables across environments? Have you ever had to rotate a key after an accidental commit?
Comments · 3
Never commit .env files to git. A rule of thumb: if a leaked repo would expose it, it belongs in the environment, not the code.
Keep a .env.example in the repo with fake values. New teammates get set up in minutes instead of hunting for what's required.
How do hosting platforms keep these values safe? Are they encrypted at rest?