Skip to content

Single API with Stage Variables vs. Separate APIs per Environment

0

Hi AWS Community,

I am isolating Dev and Prod environments for multiple microservices in lambda(20+ total Lambda functions).

I am evaluating two HTTP API Gateway setups:

  • Option 1: 1 API Gateway per service using Stage Variables (${stageVariables.functionName}) for dev/prod stages.
  • Option 2: Dedicated API Gateways for Dev and Prod environments.

Could you provide guidance on:

  1. Operational Risk: Which approach better reduces deployment risk and blast radius?
  2. Operational Feasibility: Does Option 1 require running manual aws lambda add-permission CLI commands for all 20+ functions? Is it maintainable for multiple microservices ?
  3. Cost: Is there any price difference between the two approaches?

Which approach does AWS recommend for long-term maintainability, operational stability, and reducing blast radius during deployments?

Thank you!

2 Answers
1

Both approaches are valid, but they offer different trade-offs for operational risk, feasibility, and cost. Here's guidance on each of your concerns:

1. Operational Risk and Blast Radius:

Using separate API Gateways for Dev and Prod (Option 2) provides stronger isolation and significantly reduces blast radius. With dedicated APIs per environment, a misconfiguration or deployment issue in Dev cannot impact your Prod environment. This approach better aligns with the principle of environment isolation you're already implementing for your Lambda functions.

With a single API using stage variables (Option 1), while stages do provide separation, both Dev and Prod configurations exist within the same API resource. A deployment or configuration change could potentially affect both stages if not carefully managed. However, API Gateway does create snapshots of your API with each stage deployment, allowing you to revert if necessary.

2. Operational Feasibility:

Yes, when using stage variables to reference Lambda function names (Option 1), you must manually configure permissions for each Lambda function. The permissions cannot be automatically set up by API Gateway in this scenario. For 20+ functions across multiple microservices, you would need to run the aws lambda add-permission command for each function-stage combination, which adds operational overhead during initial setup and when adding new functions.

With separate API Gateways (Option 2), permission management is more straightforward since you're directly integrating with specific Lambda functions rather than using variables. This approach is generally more maintainable at scale.

3. Cost:

For HTTP APIs, you pay per million requests received plus data transfer costs. The pricing model doesn't differentiate based on the number of APIs you create. Whether you use one API with multiple stages or separate APIs per environment, your costs will be essentially the same—you're paying for the actual API calls made. There's no additional charge for creating multiple APIs or stages.

Recommendation:

For long-term maintainability, operational stability, and reduced blast radius with 20+ Lambda functions across multiple microservices, separate API Gateways per environment (Option 2) is the stronger approach. This provides:

  • Complete environment isolation
  • Simpler permission management
  • Reduced risk of cross-environment impact
  • Clearer separation of concerns
  • Easier troubleshooting and monitoring per environment

Stage variables are valuable for scenarios like pointing to different backend endpoints within the same environment or passing environment-specific metadata, but for true Dev/Prod isolation at scale, dedicated APIs offer better operational characteristics without cost penalties.
Sources
Use stage variables for HTTP APIs in API Gateway - Amazon API Gateway
Use stage variables for a REST API in API Gateway - Amazon API Gateway
Iterate rapidly, stay agile - AWS Serverless Multi-Tier Architectures with Amazon API Gateway and AWS Lambda

answered 2 months ago

EXPERT

reviewed 2 months ago

1

If it were me (and I'm doing exactly this right now) I'd be running a separate API Gateway for your environments. Further: I would be running them in separate accounts. That's a little more operational overhead for you but it greatly reduces risk and blast radius.

Your question about adding permissions is interesting. Why are you doing that? At the very least you should be automating your deployments using AWS SAM - using that you create a template and when you have the Lambda specification you tell it how you want API Gateway to trigger that Lambda function. SAM then builds the permissions (for Lambda execution and trigger) automatically. Far less verbose for you and far easier to define/deploy/update/maintain. This also means that you can (and should!) be able to deploy the same template for dev and prod as long as you haven't statically named anything.

(Admittedly, I'm not using stage variables in my world but that is in line with my recommendation here.)

The will be a cost difference is if you go with the multi-account approach and you have things like AWS Config or other infrastructure that you run per-account. In a single account environment (again, I would not recommend this for dev/prod - for uat/dev, sure) the cost difference will come down to what you configure in API Gateway - a "vanilla" API Gateway is free of charge except for usage; but if you enable (say) WAF or caching then your cost will go up accordingly.

Again: I'd recommend putting prod in its own account. It is best practice to do so. It also makes billing for things easier.

EXPERT

answered 2 months ago

EXPERT

reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.