AWS Builder Center
Build a Serverless REST API with AWS Lambda and API Gateway

Build a Serverless REST API with AWS Lambda and API Gateway

Learn how to build and deploy a simple serverless REST API using Amazon API Gateway and AWS Lambda. This beginner-friendly guide explains the architecture, Lambda function, API endpoint, testing process, and common troubleshooting steps.

18 | SDE Intern @ Augenblick | ML Intern @ Edunet | Green Skills Intern @ Shell x AICTE
Amazon API Gateway receives the HTTP request and invokes the Lambda function. Lambda processes the request and returns a response through API Gateway.

Prerequisites

Before starting, you should have:
  • An AWS account
  • Basic knowledge of HTTP and REST APIs
  • Basic Python knowledge
  • Access to the AWS Management Console
No EC2 server is required for this project.

Step 1: Create the Lambda Function

Sign in to the AWS Management Console and open AWS Lambda.
Choose Create function.
Select:
  • Author from scratch
  • Function name:  hello-api 
  • Runtime: Python
Create the function.
Once the function is created, open the code editor and use the following code:
import json

def lambda_handler(event, context):
response = {
"message": "Hello from AWS Lambda!",
"status": "success"
}

return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response)
}
This function creates a JSON response and returns HTTP status code  200 .

Step 2: Test the Lambda Function

Before connecting Lambda to API Gateway, test the function.
Choose Test in the Lambda console and create a test event.
You can use a simple JSON event such as:
{
"test": "hello"
}
Run the test.
If the function executes successfully, you should see a response similar to:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\": \"Hello from AWS Lambda!\", \"status\": \"success\"}"
}
This confirms that the Lambda function is working.

Step 3: Create an API Gateway API

Open Amazon API Gateway from the AWS Management Console.
Choose Create API and select HTTP API.
HTTP APIs provide a simple way to expose Lambda functions through HTTP endpoints.
Create a new API and add your Lambda function as the integration.
For example:
API Gateway
|
+---- GET /hello
|
v
hello-api Lambda
The  /hello  route will be used to access the Lambda function.

Step 4: Configure the Route

Create a route such as:
GET /hello
Connect this route to the  hello-api  Lambda function.
The HTTP method tells API Gateway what type of request is being handled.
For example:
GET /hello
POST /users
DELETE /users/1
For this project, we only need the  GET /hello  route.

Step 5: Deploy the API

After configuring the route and Lambda integration, deploy the API.
API Gateway will provide an Invoke URL.
It will look similar to:
https://example.execute-api.region.amazonaws.com
Your endpoint will then be:
https://example.execute-api.region.amazonaws.com/hello
Open the endpoint in a browser or API testing tool.
You should receive:
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
Congratulations! You have created a serverless REST API.

Step 6: Understand the Request Flow

When a user accesses the API, the request follows this process:
1. Client sends GET request
|
v
2. API Gateway receives request
|
v
3. API Gateway invokes Lambda
|
v
4. Lambda executes Python code
|
v
5. Lambda creates JSON response
|
v
6. API Gateway returns response
|
v
7. Client receives JSON
The important part is that you do not need to provision or manage an EC2 server.

Step 7: Add Query Parameters

The API can also accept query parameters.
For example:
GET /hello?name=John
The Lambda function can read the query parameter from the event.
Example:
import json

def lambda_handler(event, context):

params = event.get("queryStringParameters") or {}
name = params.get("name", "World")

response = {
"message": f"Hello, {name}!"
}

return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response)
}
Now:
/hello
returns:
{
"message": "Hello, World!"
}
While:
/hello?name=John
returns:
{
"message": "Hello, John!"
}

Common Problems

1. API Returns a 500 Error

Check the Lambda function logs in Amazon CloudWatch.
Look for Python exceptions or incorrect event handling.

2. API Returns 403

Check the API Gateway route, deployment configuration, and Lambda integration permissions.

3. Lambda Works but API Does Not

Test the Lambda function directly first.
If Lambda works independently, check the API Gateway integration and route configuration.

4. Incorrect JSON Response

Make sure the Lambda function returns the expected structure:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response)
}

Security Considerations

A public API should not be left completely open for production workloads.
Depending on your application, consider:
  • Authentication
  • Authorization
  • IAM
  • API keys where appropriate
  • Request validation
  • Rate limiting
  • Logging and monitoring
Never place passwords, API keys, or other secrets directly inside your source code.

Monitoring

AWS Lambda automatically integrates with Amazon CloudWatch for logs and monitoring.
CloudWatch can help you investigate:
  • Function errors
  • Execution logs
  • Invocation activity
  • Application problems
When troubleshooting a serverless API, checking Lambda logs should be one of the first steps.

Why Use Lambda and API Gateway?

This architecture is useful because you can build an API without managing traditional servers.
Some advantages include:
  • No server management
  • Automatic scaling
  • Pay-for-use pricing model
  • Easy integration with other AWS services
  • Built-in monitoring integrations
  • Suitable for many event-driven applications
However, serverless is not automatically the best choice for every workload. Architecture decisions should consider performance, cost, execution duration, traffic patterns, and application requirements.

Conclusion

In this tutorial, we created a simple serverless REST API using AWS Lambda and Amazon API Gateway.
The final architecture is:
Client
|
v
Amazon API Gateway
|
v
AWS Lambda
|
v
JSON Response
This simple project provides a foundation for building more advanced serverless applications.
From here, you can extend the API by adding DynamoDB, authentication, additional routes, input validation, and CloudWatch monitoring.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article