AWS API Gateway: Stages, Variables, and Custom Domains
Describe API Gateway stages and custom domains
AWS API Gateway: Stages, Variables, and Custom Domains
Learning Objectives
After studying this guide, you should be able to:
- Explain the relationship between Deployments and Stages.
- Configure and use Stage Variables to parameterize backend integrations.
- Describe the purpose and setup of Custom Domain Names in API Gateway.
- Differentiate between Dev, Test, and Prod environments within a single API.
- Understand how to implement Throttling and Canary Deployments at the stage level.
Key Terms & Glossary
- Stage: A logical reference to a lifecycle state of your API (e.g.,
prod,beta). It points to a specific deployment snapshot. - Deployment: A point-in-time snapshot of your API resources and methods. You must "deploy" to make changes live.
- Stage Variable: A name-value pair that acts like an environment variable for your API stage.
- Invoke URL: The default HTTPS endpoint generated by AWS (e.g.,
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}). - Custom Domain Name: A user-friendly URL (e.g.,
api.example.com) that maps to your API Gateway stages. - Base Path Mapping: A configuration that links a custom domain name and optional path to a specific API and stage.
The "Big Idea"
In modern cloud development, you shouldn't hardcode endpoints. API Gateway Stages allow you to maintain multiple versions of your API (Development, QA, Production) simultaneously without changing your code structure. By using Stage Variables, your API becomes a dynamic template: the same API definition can point to a "Dev" Lambda function in one stage and a "Prod" Lambda function in another, simply by swapping a variable value.
Formula / Concept Box
| Concept | Syntax / Rule |
|---|---|
| Stage Variable Access | ${stageVariables.variableName} |
| Default URL Pattern | https://{api-id}.execute-api.{region}.amazonaws.com/{stage-name}/ |
| Lambda Integration | arn:aws:lambda:region:account-id:function:MyFunc:${stageVariables.lambdaAlias} |
| Throttling Error | 429 Too Many Requests (occurs when RPS or Burst limits are exceeded) |
Hierarchical Outline
- I. API Deployments and Stages
- Snapshot Logic: Changes made in the console are not live until a Deployment is created.
- Stage Association: A stage is a "pointer" to a specific deployment ID.
- Lifecycle Management: Facilitates gradual rollouts and A/B testing via Canary Deployments.
- II. Stage Variables
- Purpose: Externalize configuration (URLs, ARNs, feature flags).
- Supported Integrations: HTTP Endpoints, Lambda Function ARNs, and Mapping Templates.
- Benefits: Environment isolation without duplicating API resources.
- III. Custom Domain Names
- Prerequisites: Requires an SSL/TLS certificate from AWS Certificate Manager (ACM).
- Types:
- Edge-optimized: Routed via CloudFront (global).
- Regional: Deployed in the specific AWS region.
- Mapping: Use Base Path Mappings to route
api.com/v1to Stage A andapi.com/v2to Stage B.
Visual Anchors
The Deployment Workflow
Custom Domain Mapping
\begin{tikzpicture}[node distance=2cm, every node/.style={draw, rectangle, align=center, rounded corners}] \node (domain) [fill=blue!10] {Custom Domain$api.example.com)}; \node (mapping1) [right=of domain, yshift=1cm] {Base Path: /v1\Map to API-A, Stage: Prod}; \node (mapping2) [right=of domain, yshift=-1cm] {Base Path: /dev\Map to API-A, Stage: Dev}; \draw[->, thick] (domain.east) -- (mapping1.west); \draw[->, thick] (domain.east) -- (mapping2.west); \end{tikzpicture}
Definition-Example Pairs
- Stage Variable: A key-value pair configured at the stage level.
- Example: Setting a variable
envtoprod. In your HTTP integration, you usehttp://${stageVariables.env}.example.com/datato dynamically route requests.
- Example: Setting a variable
- Canary Deployment: A feature where a small percentage of traffic is shifted to a new "canary" deployment for testing.
- Example: Routing 10% of users to a new API version to monitor for errors before a full 100% rollout.
- Throttling: Limits on the rate of requests.
- Example: Setting a
prodstage to 10,000 requests per second (RPS) while limiting thedevstage to 100 RPS to save backend costs.
- Example: Setting a
Worked Examples
Example 1: Dynamic Lambda Routing
Scenario: You have two Lambda aliases, PROD and DEV. You want your API to call the correct alias based on the stage.
- Define Stage Variables:
- In the
prodstage, create a variablefunctionAlias=PROD. - In the
devstage, create a variablefunctionAlias=DEV.
- In the
- Configure Integration:
- In the Lambda integration field, enter:
arn:aws:lambda:us-east-1:123456789012:function:MyProcessor:${stageVariables.functionAlias}.
- In the Lambda integration field, enter:
- Permission:
- Ensure you grant API Gateway permission to invoke the Lambda function using a wildcard for the alias in the resource-based policy.
Example 2: Setting up a Custom Domain
Scenario: Making your API accessible at api.mycompany.com.
- Certificate: Request a certificate for
api.mycompany.comin ACM (must be inus-east-1for edge-optimized). - Create Custom Domain: In API Gateway console, go to "Custom Domain Names" and create one using the ACM certificate.
- Base Path Mapping: Map the empty path
(none)to your APIMySystemand stageprod. - DNS: In Route 53, create an A-Alias record pointing
api.mycompany.comto the API Gateway domain name provided in the console.
Checkpoint Questions
- What is the difference between a Deployment and a Stage in API Gateway?
- Which HTTP status code is returned when a client exceeds the throttling limits configured on a stage?
- How do you access a stage variable named
dbEndpointin a mapping template? - Why must you use AWS Certificate Manager (ACM) when setting up a Custom Domain Name?
- Can you have different throttling settings for different stages of the same API? Explain.
[!TIP] Remember: When you change an API's resources or methods, the changes are not automatically reflected in the Stage. You must create a new Deployment and update the Stage to point to it!
[!WARNING] If you use stage variables in a Lambda integration, the AWS Console cannot automatically add the
lambda:InvokeFunctionpermission for you. You must add this permission manually via the AWS CLI or CloudFormation.