AWS Builder Center

Building a Serverless REST API with AWS Lambda and API Gateway

Serverless architecture lets developers build and run applications without managing servers. In this article, we’ll explore how to create a simple REST API using AWS Lambda and Amazon API Gateway.

In a traditional application, you need to provision and maintain servers, configure operating systems, manage scaling, and monitor infrastructure.
With serverless services, AWS manages much of the underlying infrastructure for you. You focus primarily on application code and configuration.
Two important services are:
  • AWS Lambda — Runs code in response to events without requiring you to manage servers.
  • Amazon API Gateway — Provides HTTP endpoints that can invoke Lambda functions.
Together, they provide a straightforward way to build APIs that can scale automatically.
Architecture
A basic request flow looks like this:
Client
|
v
API Gateway
|
v
AWS Lambda
|
v
Application Logic
When a client sends an HTTP request, API Gateway receives it and invokes the Lambda function. Lambda executes the application logic and returns a response through API Gateway.
Creating the Lambda Function
Start by creating a Lambda function from the AWS Management Console.
For example, using Python:
import json
def lambda_handler(event, context):
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from AWS Lambda!"
})
}
This function returns an HTTP 200 response containing a JSON message.
Connecting API Gateway
Next, create an HTTP API in API Gateway and configure a route such as:
GET /hello
Configure the route to use your Lambda function as its integration.
Once the API is deployed, API Gateway provides an endpoint that clients can call.
For example:
curl https://your-api-id.execute-api.region.amazonaws.com/hello
The response should look similar to:
{
"message": "Hello from AWS Lambda!"
}
Why Use This Architecture?
This combination has several useful characteristics:
Automatic Scaling
Lambda can execute multiple instances of a function when request volume increases, subject to AWS service limits and your configuration.
Less Infrastructure Management
You don't need to maintain EC2 instances or manage operating system patches for the Lambda execution environment.
Pay-for-Use Model
Lambda pricing is based primarily on requests and execution duration, while API Gateway pricing depends on the API type and usage.
Easy Integration
API Gateway and Lambda can be integrated with other AWS services such as Amazon DynamoDB, Amazon S3, Amazon EventBridge, and Amazon CloudWatch.
Adding a Database
A common next step is connecting the API to DynamoDB.
For example:
Client
|
v
API Gateway
|
v
Lambda
|
v
DynamoDB
Lambda can read and write application data in DynamoDB using the AWS SDK.
This architecture is useful for applications such as:
Web APIs
Mobile backends
Event-driven applications
Lightweight microservices
Data-processing endpoints
Monitoring
For production workloads, monitoring is important.
AWS Lambda integrates with Amazon CloudWatch, which can provide logs and metrics for your functions.
Useful things to monitor include:
Invocation count
Errors
Duration
Throttling
API request metrics
CloudWatch Logs can also help diagnose application errors.
Security Considerations
Avoid placing credentials directly inside Lambda source code.
Instead, use AWS Identity and Access Management (IAM) roles to provide the permissions required by your function.
For example, if a Lambda function only needs to read from a DynamoDB table, its IAM permissions should be limited to the required resources and actions.
Following the principle of least privilege helps reduce unnecessary access.
Conclusion
AWS Lambda and API Gateway provide a simple foundation for building serverless APIs. By combining managed services, developers can focus on application functionality while AWS handles much of the underlying infrastructure.
A useful next step is to add DynamoDB, authentication, structured error handling, automated deployment, and monitoring to turn the basic API into a production-ready application.
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