Skip to main content
Skip to main content
Still in beta — questions, comments or suggestions? aramb@aramb.dev

Separate Dev and Prod Configuration with Environment Variables and Stages

Learn how to decouple your code from your configuration using Lambda environment variables and API Gateway stages.

25 min
Introductory
Has Paid ComponentsPAID

Some services in this lesson have no free tier and will incur charges.

AWS Services Used

Lambda Env VarsAlways freeAPI Gateway StagesAlways freeSecrets Manager$0.40/secret/month — no free tier

You can complete most of this lesson using only Lambda environment variables (free). Secrets Manager is the recommended upgrade for sensitive values but incurs charges.

Learning Outcomes

By the end of this lesson, you will be able to:

  1. Explain why dev and prod should not share the exact same configuration.
  2. Use Lambda environment variables to change behavior without changing code.
  3. Explain what an HTTP API stage is and why dev and prod are useful.
  4. Use stage variables for non-sensitive configuration.
  5. Explain why secrets should not be stored in plain environment variables.

Why this lesson matters

Right now, your project probably has one set of values hard-coded or copied around: one API URL, one bucket name, one table name. That works while learning, but it's risky for real apps.

A dev environment should let you test changes safely, while prod stays stable. Use this golden rule:

Code stays the same; Configuration changes by environment.


Part 1: Lambda Environment Variables

Lambda environment variables are key-value pairs stored in your function configuration. They let you use the same code to talk to different resources.

Example Setup

Instead of hard-coding my-bucket-name in your Python code, use os.environ["UPLOAD_BUCKET"].

VariableDev ValueProd Value
TABLE_NAMEmetadata_devmetadata_prod
UPLOAD_BUCKETmy-app-dev-uploadsmy-app-prod-uploads
URL_EXPIRES300 (5 mins)3600 (1 hour)

Part 2: Don't Store Secrets in Env Vars

AWS recommends Secrets Manager instead of plain environment variables for sensitive values like API keys, database passwords, or private tokens.

  • Normal Config: Bucket names, table names, stage names (Use Env Vars).
  • Sensitive Secrets: Credentials, private keys (Use Secrets Manager).

Part 3: HTTP API Stages

In API Gateway, a stage is a named reference to a deployment of your API (e.g., dev, prod, v2). The stage name appears in the URL:

https://abc123.execute-api.us-east-1.amazonaws.com/dev
https://abc123.execute-api.us-east-1.amazonaws.com/prod
Environment Isolation via Stages

Part 4: Stage Variables

Stage variables are key-value pairs attached to an API stage. They act like environment variables for your API definition. They are useful for passing different metadata to your Lambda integrations without changing the API routes themselves.

Warning: Like Lambda env vars, stage variables are not intended for sensitive data.


Part 5: Frontend Environment Separation

If you host your frontend in Amplify, you can use branch-specific overrides.

  • The dev branch of your code can be built with the dev API URL.
  • The main branch can be built with the prod API URL.

This ensures your test site never accidentally writes data to your production bucket.


Lab Checklist

StepSuccess Condition
Decouple CodeCode uses os.environ instead of hard-coded strings
Add Env VarsVariables are configured in the Lambda console
Create StagesAPI has at least two stages (e.g., dev and prod)
DeployChanges are deployed to specific stages
VerifyDev URL and Prod URL return data from different sources

Micro-activity 1: Configuration Map

Environment Configuration Map
0 / 6 filled

Fill out your project's configuration for each environment. Your answers are saved in your browser.


Micro-activity 2: Reflection

Think about it

Why are Lambda environment variables better than hard-coding table names? Why should stage variables not be used for database passwords?


Summary

In this lesson, you moved from a single "toy" setup to a professional environment-aware architecture. Decoupling code from configuration using Lambda environment variables and API stages is a fundamental requirement for scaling applications safely in the cloud.


Quiz

Knowledge Check
1 / 5

What is an HTTP API stage in API Gateway?