๐Ÿ”ง DevOps

Environment Variable

Environment Variable

Stores configuration values that differ by execution environment. Used to manage passwords and API keys.

Detailed Explanation

An environment variable is a key-value pair passed to a process by the operating system or the execution environment. It separates an application's configuration from its code, so the same code can behave differently in different environments such as development, staging, and production.

The 12-Factor App methodology recommends managing configuration through environment variables. If you manage database URLs, API keys, port numbers, and the like as environment variables, you can deploy by changing only the environment, without changing any code. This is especially essential in container environments.

Security-sensitive information (passwords, API keys, tokens, etc.) should be managed as environment variables, but never committed to a code repository. When using a .env file, be sure to add it to .gitignore, and in production, use a secrets manager (AWS Secrets Manager, HashiCorp Vault, etc.).

It is conventional to name environment variables using uppercase letters and underscores. Examples: DATABASE_URL, API_SECRET_KEY. Adding an application-specific prefix prevents conflicts (e.g., MYAPP_DATABASE_URL).

Code Example

# .env ํŒŒ์ผ (๊ฐœ๋ฐœ ํ™˜๊ฒฝ์šฉ - .gitignore์— ์ถ”๊ฐ€!)
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
REDIS_URL=redis://localhost:6379
API_SECRET_KEY=dev-secret-key-never-use-in-prod
NODE_ENV=development
PORT=3000
LOG_LEVEL=debug

# Node.js์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์‚ฌ์šฉ
# config.js
import dotenv from 'dotenv';
dotenv.config();

export const config = {
  database: {
    url: process.env.DATABASE_URL,
    poolSize: parseInt(process.env.DB_POOL_SIZE || '10'),
  },
  api: {
    secretKey: process.env.API_SECRET_KEY,
    port: parseInt(process.env.PORT || '3000'),
  },
  // ํ•„์ˆ˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ฒ€์ฆ
  validate() {
    const required = ['DATABASE_URL', 'API_SECRET_KEY'];
    const missing = required.filter(key => !process.env[key]);
    if (missing.length > 0) {
      throw new Error(`Missing required env vars: ${missing.join(', ')}`);
    }
  }
};

# Kubernetes์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ฃผ์ž…
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: app
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: db-credentials
              key: url
        - name: NODE_ENV
          value: "production"

How It's Said in Practice

Senior: "We're integrating a new external API, so let's store the API key in Vault and inject it as an environment variable."

Junior: "What do I do for local development?"

Senior: "Create an .env.example file to document the list of required variables, and each person can create their own .env file and use it."

Interviewer: "How do you manage sensitive information in a production environment?"

Candidate: "I use AWS Secrets Manager or HashiCorp Vault. In Kubernetes, I automatically sync secrets with External Secrets Operator and inject them into Pods as environment variables. I never hardcode them in code or images."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "This API key is hardcoded in the code. Please move it out into an environment variable. And rotate the key right now."

Developer: "I'm sorry. I'll move it to a .env file and also check .gitignore."

Cautions

Learn More