AWS Builder Center

Build a Serverless REST API with Amazon API Gateway and AWS Lambda

Building an API usually means managing servers, scaling infrastructure, and monitoring application performance. AWS lets us simplify this process with a serverless architecture using Amazon API Gateway and AWS Lambda.

Build a Simple Serverless API with AWS Lambda and API Gateway

Serverless architecture makes it easy to build APIs without managing servers. In this tutorial, we'll create a simple API using AWS Lambda and Amazon API Gateway.

Architecture

Client
|
v
API Gateway
|
v
AWS Lambda
|
v
JSON Response


Step 1: Create a Lambda Function

Create a new Lambda function using Python and add this code:
import json

def lambda_handler(event, context):
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from AWS Lambda!",
"success": True
})
}

Deploy the function and test it from the Lambda console.

Step 2: Create an API Gateway API

Open Amazon API Gateway and create an HTTP API.
Connect the API to your Lambda function and create the route:
GET /hello

AWS will provide an API endpoint for your application.

Step 3: Test the API

You can test the endpoint using a browser or  curl :
curl https://your-api-endpoint/hello

You should receive:
{
"message": "Hello from AWS Lambda!",
"success": true
}


Why Serverless?

Using API Gateway and Lambda means you don't need to manage traditional servers. AWS handles the underlying infrastructure while you focus on your application code.
This architecture can later be extended with services such as Amazon DynamoDB, Amazon S3, and Amazon SQS to build more complete applications.

Conclusion

AWS Lambda and API Gateway provide a simple way to create scalable APIs with minimal infrastructure management. Starting with a small endpoint like  /hello  is a great way to learn the fundamentals of serverless development.
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