Compute and Serverless
API Gateway
Amazon API Gateway is a fully managed service that allows developers to create, publish, maintain, monitor, and secure APIs at any scale. It acts as a traffic controller between your backend services and external clients, abstracting the complexity of infrastructure management. You reach for it whenever you need to expose backend logic via HTTP or WebSocket protocols while offloading cross-cutting concerns like authentication and throttling.
The Core Concept of a REST API
At its simplest, API Gateway acts as a reverse proxy for your backend resources, such as Lambda functions or EC2 instances. When a client sends an HTTP request, the gateway intercepts it before it ever reaches your application code. This is fundamental because it decouples the client's interface from your backend implementation. By defining Resources and Methods, you create a mapping layer. Why does this matter? If you move from a monolithic backend to a microservices architecture, you can update the API Gateway routing rules without forcing the client to change their endpoint URL. The gateway handles the heavy lifting of mapping URL parameters and query strings to the format your backend expects, ensuring your application logic remains clean and focused solely on processing the business request rather than parsing headers or managing connection pooling at the edge.
# Example of a CLI command to create an API REST resource structure
aws apigateway create-resource --rest-api-id <api-id> --parent-id <parent-id> --path-part "orders" # Creates the /orders endpointIntegration Types and Request Mapping
API Gateway offers two primary integration types: Proxy Integration and Non-Proxy Integration. In Proxy Integration, the entire HTTP request—including headers, query parameters, and the body—is passed directly to your backend, like a Lambda function, in a structured format. This is the preferred method for most modern serverless architectures because it places the control inside your application code. In contrast, Non-Proxy Integration requires you to define a Mapping Template using a template language to transform the incoming request body into a specific format expected by your backend. You would choose Non-Proxy Integration when you need to enforce strict schema validation at the gateway level or when you want to transform a legacy backend response into a standardized JSON format before returning it to the user. This transformation capability allows you to maintain consistent API contracts even if your underlying backend services are inconsistent.
# Mapping template example for a VTL request integration (non-proxy)
# Set input.json to match backend requirements
{"orderId": "$input.params('orderId')", "status": "pending"}Security and Authentication Mechanisms
Security is a core pillar of API Gateway. Because the gateway serves as the first point of contact, it is the ideal place to implement authentication and authorization. You can integrate it with Lambda Authorizers, which allow you to run custom authentication logic for every request. This is critical for scenarios like verifying JSON Web Tokens (JWT) or checking headers against an external database. Additionally, you can utilize Amazon Cognito User Pools for managed authentication, ensuring that only users with valid tokens can reach your backend. By offloading these security checks to the gateway, you prevent unauthorized requests from triggering your backend services, which effectively protects your application from compute costs and potential denial-of-service attacks. This architectural pattern ensures that your business logic remains isolated from the mechanics of identity verification, creating a robust perimeter around your microservices.
# Terraform snippet for adding an authorizer to a method
resource "aws_api_gateway_authorizer" "auth" {
name = "jwt-authorizer"
rest_api_id = aws_api_gateway_rest_api.api.id
authorizer_uri = aws_lambda_function.auth_func.invoke_arn
}Throttling and Usage Plans
To maintain system stability, you must control the rate at which requests are processed. API Gateway provides built-in throttling and usage plans that allow you to define rate limits and burst limits per client. This is essential for protecting your downstream services from unexpected traffic spikes that could lead to resource exhaustion or increased costs. A rate limit defines the steady-state number of requests per second, while a burst limit handles short-term surges. Beyond basic throttling, Usage Plans allow you to associate specific API keys with clients, providing tiered access levels. For instance, you could offer a free tier with limited requests per day and a paid tier with higher limits. This mechanism not only safeguards your infrastructure but also provides a monetization pathway for your APIs, as you can track usage precisely and ensure fair distribution of system capacity among your various consumers.
# CLI command to set a usage plan limit
aws apigateway create-usage-plan --name "GoldPlan" --throttle "{\"burstLimit\": 200, \"rateLimit\": 100}"Stages and Deployment Management
Deployments in API Gateway are managed through Stages, which represent a snapshot of your API configuration. A stage could be 'dev', 'staging', or 'prod'. By separating these environments, you can test changes in isolation without impacting live users. When you update your resources or methods, those changes are not live until you redeploy the API to a stage. This promotes a disciplined development workflow where you can perform canary releases. A canary release allows you to route a small percentage of traffic to a new version of your API, enabling you to monitor performance and error rates before rolling out the update to the entire user base. Because the gateway handles the routing of traffic between these versions, you achieve seamless updates that minimize downtime and mitigate the risk associated with introducing breaking changes in a high-traffic production environment.
# CLI command to create a deployment stage for production
aws apigateway create-deployment --rest-api-id <api-id> --stage-name "prod"Key points
- API Gateway acts as the entry point for all incoming HTTP requests to your backend infrastructure.
- Proxy integration allows you to pass the entire request object directly to your backend for flexible processing.
- Lambda Authorizers provide a way to implement custom authentication logic at the API edge.
- Throttling protects your backend services from being overwhelmed by unexpected traffic spikes.
- Usage plans enable the enforcement of access limits based on unique API keys assigned to different consumers.
- Stages act as distinct environments that allow you to deploy and test different versions of your API separately.
- Non-proxy integration enables message transformation using mapping templates to ensure backend compatibility.
- Canary deployments facilitate safe updates by shifting traffic percentages between different API stage versions.
Common mistakes
- Mistake: Configuring API Gateway endpoints as public without authorization. Why it's wrong: This exposes sensitive backend resources to the entire internet, leading to potential data breaches or DoS attacks. Fix: Always implement Lambda Authorizers or Cognito User Pools to secure endpoints.
- Mistake: Forgetting to deploy the API after making changes. Why it's wrong: Changes in the console are only saved to the API configuration but not pushed to the live stage, resulting in no observable changes for users. Fix: Create a new deployment or enable Stage Auto-Deployment.
- Mistake: Failing to define usage plans for throttling. Why it's wrong: Without usage plans, a single user can overwhelm your backend services, leading to cost spikes and service degradation. Fix: Define specific Usage Plans with throttle and quota limits.
- Mistake: Overlooking the need for CORS headers in Proxy Integrations. Why it's wrong: Browser-based clients will reject responses from different origins if the Gateway doesn't return the appropriate Access-Control-Allow-Origin headers. Fix: Enable CORS in the API Gateway console or explicitly return headers from your backend.
- Mistake: Not utilizing Caching for repetitive requests. Why it's wrong: It causes unnecessary latency and increased invocation costs for data that doesn't change frequently. Fix: Enable API Caching on the stage and set an appropriate TTL.
Interview questions
What is an AWS API Gateway and why would I use it in my architecture?
AWS API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. I use it as a 'front door' for applications to access data, business logic, or functionality from backend services. It is essential because it handles traffic management, authorization and access control, monitoring, and API version management, allowing developers to focus on the business logic instead of infrastructure.
How does API Gateway handle security and authentication for incoming requests?
API Gateway offers several security mechanisms to control access. You can use IAM roles and policies to restrict access based on AWS credentials. You can also implement Lambda authorizers to run custom logic for token validation, such as verifying a JSON Web Token. Additionally, you can integrate with Amazon Cognito User Pools for managed authentication, or use API Keys and usage plans to throttle and limit access for specific clients or API consumers.
What are the key differences between a REST API and an HTTP API in AWS API Gateway?
REST APIs are feature-rich and support advanced functionality like API keys, usage plans, mutual TLS, and WAF integration. They are designed for legacy support and complex proxying requirements. Conversely, HTTP APIs are optimized for performance and cost, offering a lower latency and cheaper pricing model. HTTP APIs lack some of the advanced features of REST APIs but are the preferred choice for simple proxying to AWS Lambda or private HTTP endpoints.
Could you explain the difference between a Proxy Integration and a Non-Proxy Integration in API Gateway?
In a Lambda Proxy Integration, API Gateway passes the entire request—headers, query parameters, body, and context—directly to the Lambda function, and expects a specific JSON response format from the function. In a Non-Proxy Integration, you must manually map the incoming request to the backend requirements using API Gateway Mapping Templates. Proxy integrations are much faster to set up, whereas non-proxy integrations provide granular control over the data transformation process.
How do you implement throttling in API Gateway to protect your backend services?
Throttling is managed at two levels: the account level and the API level. You can set a default request rate limit and burst limit for the entire account. For more specific control, you create Usage Plans associated with API Keys. For example, if you have a premium customer, you can assign them a specific Usage Plan with higher throttling limits than a basic user, ensuring your backend services are protected from sudden spikes.
Explain how you would implement a Canary Deployment for an API Gateway stage.
To implement a Canary Deployment, you configure your API stage to use a Canary setting. You define a percentage of traffic—for instance, 10%—to be routed to a new deployment version, while 90% stays on the current stable version. This allows you to monitor the health of the new API release in production. If errors occur, you can automatically roll back. This mitigates risk by ensuring new deployments do not negatively impact your entire user base.
Check yourself
1. An application requires specific request validation before reaching the backend integration. Which feature should be used?
- A.Request Models and Validators
- B.Stage Variables
- C.Binary Media Types
- D.API Gateway Caching
Show answer
A. Request Models and Validators
Request Models and Validators enforce strict JSON schemas. Stage variables allow environment-specific configs, Binary Media Types handle non-text data, and Caching optimizes performance; none of the others validate incoming request structures.
2. A developer needs to expose a legacy backend service via an API, but the service requires dynamic path routing. What is the best approach?
- A.Use a Proxy Resource with a greedy path parameter
- B.Hardcode every individual route in the console
- C.Enable CORS for every request
- D.Use Lambda Authorizers to route the traffic
Show answer
A. Use a Proxy Resource with a greedy path parameter
A proxy resource with a greedy path variable ({proxy+}) captures all incoming requests under a base path and forwards them to the backend. Hardcoding is inefficient; CORS handles security headers, and Authorizers handle identity, not routing.
3. How can you securely pass credentials to a backend service that API Gateway does not manage directly?
- A.Store the secret in an API Key
- B.Use a Lambda Authorizer to inject headers
- C.Use an API Gateway Private Integration with Secrets Manager
- D.Include the credentials in the URL query string
Show answer
C. Use an API Gateway Private Integration with Secrets Manager
Private Integrations allow secure communication via VPC Links. Using Secrets Manager via a Lambda or direct integration is secure. API Keys identify clients, not backend secrets; URL query strings are insecure, and Authorizers are for client validation, not backend secret storage.
4. Your API is receiving a 429 Too Many Requests error. What is the most likely cause?
- A.The Lambda integration has timed out
- B.The request has exceeded the configured Throttling limits
- C.The endpoint does not have CORS enabled
- D.The Authorization token has expired
Show answer
B. The request has exceeded the configured Throttling limits
HTTP 429 indicates that the request rate exceeds the defined throttling limits. Timeouts result in 504 errors, CORS issues result in 403 or 405 errors, and expired tokens result in 401 Unauthorized responses.
5. Why would you choose a REST API over an HTTP API in API Gateway?
- A.REST APIs are cheaper and faster than HTTP APIs
- B.HTTP APIs do not support Lambda integration
- C.REST APIs offer advanced features like Request Validation, WAF support, and private endpoints
- D.REST APIs are the default for serverless applications
Show answer
C. REST APIs offer advanced features like Request Validation, WAF support, and private endpoints
REST APIs offer deep integration features like request validation, usage plans, and WAF integration. HTTP APIs are faster and cheaper but lack those advanced controls. Both support Lambda, and HTTP API is actually the recommended default for most modern serverless needs.