AWS Lambda is a serverless compute service that automatically runs backend code in response to events (such as HTTP requests) without provisioning or managing servers.
API Gateway acts as the "front door" for your Lambda functions, allowing you to expose them securely as RESTful APIs.
API Gateway receives HTTP requests (GET, POST, PUT, DELETE), and forwards the request to the appropriate Lambda function.
In this model:
API Gateway handles the HTTP layer (Path, Method, Authentication, etc.)
Lambda handles the Business Logic (what to do when someone hits /user/create
, /user/get-details
, etc.)
Together, this combination allows us to build powerful APIs without managing servers! 🚀
Your project has two major files:
✅ lambda_function.py
(business logic)
✅ template.yaml
(serverless configuration)
def lambda_handler(event, context):
event
contains all information about the incoming HTTP request (path, query parameters, body, etc.)
context
provides metadata about the Lambda invocation (memory, timeout, etc.)
The event['path'] field is used to identify which endpoint is being called.
Based on the path (like /user/get-details
, /user/create
, etc.), the corresponding business logic is executed.
Endpoint Path | Business Logic (Return Message) |
---|---|
/user/get-details |
"user details" |
/user/create |
"Create user" |
/user/get-user |
"Single User Details" |
/user/update |
"Update user details" |
/user/delete |
"User Delete" |
Anything else | "Service not found" |
Example:
If a user hits /user/get-details
, the Lambda returns:
{"message": "user details"}
In your template.yaml
, you define which HTTP requests (Paths + Methods) should trigger your Lambda:
Events:
GetAllUser:
Type: Api
Properties:
Path: /user/get-details
Method: get
CreateUser:
Type: Api
Properties:
Path: /user/create
Method: get
Meaning:
When a user sends a GET request to /user/get-details
, API Gateway triggers your Lambda.
When a user sends a GET request to /user/create
, API Gateway triggers your Lambda.
(Client HTTP Request)
↓
(API Gateway)
↓
(lambda_handler in Lambda)
↓
(Business logic based on event['path'])
↓
(Response back to Client)
HTTP Method | API Gateway Path | Triggered Lambda Action |
---|---|---|
GET | /user/get-details | Return user details |
GET | /user/create | Return create user confirmation |
(Any other) | (Invalid path) | Return "Service not found" |
event['path']
decides the logic execution in Lambda.
API Gateway matches the path and method to route the request to your Lambda.
Lambda must return a JSON response with statusCode and body.
In real-world projects, you would connect this code to databases (like DynamoDB, RDS) instead of just static messages.
event
JSONWhen a user sends a request (example: GET /user/get-details
),
API Gateway internally creates an event
dictionary and passes it to your lambda_handler
.
A typical event will look like this:
{
"resource": "/user/get-details",
"path": "/user/get-details",
"httpMethod": "GET",
"headers": {
"Host": "your-api-id.execute-api.us-east-1.amazonaws.com",
"User-Agent": "Mozilla/5.0",
"Accept": "application/json",
...
},
"queryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"body": null,
"isBase64Encoded": false
}
Field | Meaning |
---|---|
resource |
The API Gateway Resource being accessed (in your case /user/get-details ) |
path |
The full URL path of the request |
httpMethod |
Whether it’s GET , POST , PUT , DELETE , etc. |
headers |
Request headers sent by the client (browser, Postman, etc.) |
queryStringParameters |
If any query parameters like ?id=5 , it will be captured here |
pathParameters |
If you are using dynamic path parameters (e.g., /user/{id} ) |
body |
The request body (only for POST/PUT calls where you send data) |
isBase64Encoded |
If the body is encoded, this will be true . Mostly false for text requests |
In your code:
endpoint = event['path']
You are checking the path,
then based on that path (/user/get-details
, /user/create
, etc.),
you decide which business logic to execute.
✅ Simple and clean!
Step | Action |
---|---|
1 | User sends HTTP request (GET /user/get-details ) |
2 | API Gateway receives request |
3 | API Gateway triggers Lambda and passes event |
4 | Lambda reads event['path'] |
5 | Based on path, Lambda returns appropriate response |
6 | API Gateway returns Lambda response back to user |
Instead of many if...elif
for paths,
You can use a Python dictionary router for cleaner code!
Example:
def lambda_handler(event, context):
path_to_response = {
"/user/get-details": {"message": "user details"},
"/user/create": {"message": "Create user"},
"/user/get-user": {"message": "Single User Details"},
"/user/update": {"message": "Update user details"},
"/user/delete": {"message": "User Delete"},
}
endpoint = event.get('path')
response = path_to_response.get(endpoint, {"message": "Service not found"})
return {
"statusCode": 200,
"body": json.dumps(response),
}