AWS API Gateway
# CHAPTER 16
AWS API Gateway
1. Introduction
In Chapter 15, we wrote a powerful Lambda function. However, a Lambda function is isolated deep within your AWS account. A user on a mobile app cannot simply "click" a button and trigger your Lambda code. You need a public-facing door. Amazon API Gateway is that door. It is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure REST and WebSocket APIs at any scale.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the role of an API Gateway in a Serverless architecture.
- Differentiate between REST APIs and HTTP APIs.
- Create an API Gateway endpoint.
- Integrate API Gateway with an AWS Lambda function.
-
Deploy an API to a specific stage (e.g.,
prodordev).
3. Beginner-Friendly Explanation
Imagine an exclusive restaurant (Your AWS Backend).- The Chef: The Lambda function. It knows exactly how to cook the food (process the data).
- The Customer: A mobile app trying to place an order over the internet.
- The Waiter (API Gateway): The customer cannot walk into the kitchen and talk to the chef. The Waiter stands at the front door, takes the customer's order (the HTTP Request), walks to the kitchen, hands the order to the Chef (triggers Lambda), waits for the food (the JSON Response), and delivers it back to the customer.
API Gateway intercepts public internet traffic and securely routes it to your backend AWS services.
4. API Gateway Capabilities
API Gateway does far more than just route traffic:- Throttling/Rate Limiting: If a hacker tries to hit your API 10,000 times a second, API Gateway intercepts it and blocks them, preventing your Lambda functions from executing and costing you money.
- Authentication: It can verify JWT tokens (via Amazon Cognito or custom Lambda Authorizers) before allowing traffic through.
- Caching: It can cache API responses, so if 100 users ask for the same data, API Gateway serves it instantly without bothering the Lambda function.
5. HTTP APIs vs. REST APIs
When creating an API in AWS, you are presented with two main options:- 1. REST APIs: The original, feature-rich service. Supports complex data mapping, API keys, and advanced monitoring. (More expensive).
- 2. HTTP APIs: The newer, modernized version. They are designed exclusively for serverless workloads, offer 71% lower latency, and cost 60% less than REST APIs. Unless you need highly specific legacy features, HTTP APIs are the standard choice for modern Lambda integrations.
6. Mini Project: Build a Serverless API
Let's connect the public internet to the Python Lambda function we wrote in Chapter 15!Step-by-Step Tutorial:
- 1. Open the AWS Console and search for API Gateway.
- 2. Click Create API.
- 3. Under HTTP API, click Build.
-
4.
Integrations: Click "Add integration". Select Lambda. In the dropdown, select the
HelloCloudFunctionyou created in Chapter 15.
-
5.
API name: Enter
MyFirstAPI. Click Next.
- 6. Configure routes:
- Method: GET
-
Resource path:
/hello
-
Integration target:
HelloCloudFunction
- Click Next.
-
7.
Define stages: Leave the default stage name
$defaultand ensure "Auto-deploy" is checked. Click Next.
- 8. Click Create.
-
9.
AWS will generate an "Invoke URL" (e.g.,
https://abc123xyz.execute-api.us-east-1.amazonaws.com).
-
10.
Copy that URL, paste it into your web browser, and add
/helloto the end of it. (e.g.,...amazonaws.com/hello).
Result: Your browser will display: "Hello, Cloud Explorer! Your Serverless code is running!"
*You have just built a production-grade, infinitely scalable API without provisioning a single server!*
7. The Serverless Architecture Pattern
This is the modern standard for cloud startups:- 1. User clicks a button on a React site hosted on S3 + CloudFront.
- 2. The browser makes an HTTP request to API Gateway.
- 3. API Gateway securely triggers a Lambda Function.
- 4. Lambda queries a NoSQL DynamoDB Table.
- 5. The JSON data flows all the way back to the user in 100 milliseconds.
8. Best Practices
-
Enable CORS: If your API Gateway is at
api.comand your frontend is atapp.com, modern browsers will block the request. You must explicitly configure CORS (Cross-Origin Resource Sharing) settings inside API Gateway to allow your frontend domain to communicate with it.
9. Common Mistakes
- Forgetting to Deploy: If you use a REST API (instead of an HTTP API with auto-deploy), any changes you make in the console will NOT take effect on the live internet until you explicitly click Actions -> Deploy API. Many beginners spend hours wondering why their new route is returning a 404 error because they forgot to hit deploy.
10. Exercises
- 1. Explain the "Waiter" analogy. Why shouldn't a mobile application trigger a Lambda function directly?
- 2. Compare AWS HTTP APIs with AWS REST APIs. Which is preferred for a simple, low-cost serverless microservice?
11. MCQs with Answers
You have written a Lambda function in Node.js. What AWS service must you deploy in front of this function to expose it as a public RESTful HTTP endpoint?
Which feature of Amazon API Gateway helps protect backend Lambda functions from DDoS attacks or massive traffic spikes by limiting the number of requests a client can make per second?
12. Interview Questions
- Q: Walk me through the request flow of a fully serverless web application, detailing the interaction between S3, API Gateway, Lambda, and DynamoDB.
- Q: Explain how API Gateway handles authorization. How would you architect a system to ensure only authenticated users (holding a valid JWT) can access an API Gateway route?