What is the role of environment variables in serverless functions?
Answer
Environment variables in serverless functions serve the same purpose as in traditional applications — providing runtime configuration without hardcoding values. In AWS Lambda, environment variables are set at the function level via the console, CLI (--environment-variables KEY=VALUE), or IaC templates. They are injected into the function's runtime environment and accessible via process.env.KEY (Node.js), os.environ['KEY'] (Python), etc. Common uses: database connection strings, API keys, feature flags, stage identifiers (dev/staging/prod). For sensitive values, store secrets in AWS Secrets Manager or Parameter Store and retrieve them at function startup (using SDK calls in the initialization code outside the handler, not inside — to avoid per-invocation API calls). Environment variables are limited to 4KB total per function. Never commit secrets in environment variable files.
Previous
What is an API Gateway?
Next
What is event-driven architecture and how does it relate to serverless?