AWS Builder Center

Build a Serverless REST API with AWS Lambda and API Gateway

A practical beginner-friendly guide to building a serverless REST API using AWS Lambda and Amazon API Gateway, including the Lambda code, API route configuration, testing, and production best practices.

Build a Serverless REST API with AWS Lambda and API Gateway
Introduction
Building a REST API no longer requires managing servers, operating systems, or complex infrastructure. With AWS Lambda and Amazon API Gateway, you can create a scalable API that runs your code only when requests arrive.
In this guide, we'll build a simple serverless REST API using AWS Lambda and API Gateway, with Python as the programming language.
What You'll Build
By the end of this tutorial, you'll have an API endpoint that returns a JSON response like:
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
The basic architecture looks like this:
Client
|
v
API Gateway
|
v
AWS Lambda
|
v
JSON Response
Prerequisites
Before getting started, you should have:
An AWS account
Basic knowledge of Python
Familiarity with HTTP APIs
Access to the AWS Management Console
Step 1: Create the Lambda Function
Open the AWS Lambda console and select Create function.
Choose:
Author from scratch
Function name: hello-serverless-api
Runtime: Python 3.x
Architecture: x86_64
For this basic example, you can use the default execution role created by AWS.
Click Create function.
Step 2: Add the Lambda Code
Replace the default Lambda code with:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from AWS Lambda!",
"status": "success"
})
}
Click Deploy to save the changes.
The function returns an HTTP 200 response containing a JSON object.
Step 3: Create an API Gateway API
Next, open Amazon API Gateway.
Create a new HTTP API and configure the Lambda function as the integration.
For example, create the following route:
GET /hello
Connect this route to the hello-serverless-api Lambda function.
API Gateway will provide you with an invoke URL similar to:
https://example.execute-api.region.amazonaws.com
Your endpoint will therefore look similar to:
GET https://example.execute-api.region.amazonaws.com/hello
Step 4: Test the API
You can test your API using a browser, curl, or an API testing tool.
Using curl:
curl https://example.execute-api.region.amazonaws.com/hello
You should receive:
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
Congratulations! You now have a working serverless REST API.
Why Use Lambda and API Gateway?
This architecture is useful because AWS handles much of the underlying infrastructure.
No Server Management
You don't need to provision or maintain servers. Lambda runs your function when it is invoked.
Automatic Scaling
Lambda can automatically handle additional requests without requiring you to manually scale a server fleet.
Pay for Usage
With a serverless architecture, you generally pay based on usage rather than maintaining an always-running application server.
Easy Integration
API Gateway can expose Lambda functions as HTTP endpoints, making this architecture useful for web applications, mobile applications, and backend services.
Best Practices
When building production APIs, consider the following:
Use authentication and authorization for protected endpoints.
Validate incoming request data.
Store secrets in AWS Secrets Manager or another appropriate secret-management service.
Keep Lambda functions focused on a specific responsibility.
Configure appropriate logging and monitoring.
Set sensible timeout and memory configurations.
Use infrastructure-as-code tools such as AWS CloudFormation or AWS CDK for repeatable deployments.
Conclusion
AWS Lambda and API Gateway provide a straightforward way to build scalable APIs without managing traditional servers. Starting with a simple endpoint like this is a good way to understand serverless architecture before adding databases, authentication, monitoring, and more complex application logic.
Once you're comfortable with this setup, you can extend the API with Amazon DynamoDB, authentication, multiple routes, and automated deployments.
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