Building a Serverless REST API with AWS API Gateway and Lambda

Published by Rhein-Ruhr-Informatik on

Introduction

In modern software development, speed and scalability are crucial. Companies want to deliver features quickly without worrying about maintaining infrastructure. This is where serverless computing shines.

With AWS Lambda and Amazon API Gateway, developers can build a fully functional REST API without managing servers. AWS automatically takes care of scaling, availability, and billing — you only pay for the requests that are actually processed.

In this article, we will explore how to set up a simple REST API using AWS API Gateway and AWS Lambda, with examples in Python.

What is Serverless?

The term serverless doesn’t mean “no servers” — servers are still involved, but they are fully managed by the cloud provider. Developers focus only on writing code, while AWS handles:

– Provisioning and scaling (from zero to millions of requests)
– High availability
– Security and patching
– Billing based on actual usage

For REST APIs, the combination of API Gateway and Lambda has become a common pattern. Let’s see how they work together.

Architecture Overview

The flow looks like this:
1. A client (browser, mobile app, Postman) sends a request to an API Gateway endpoint.
2. API Gateway routes the request to a configured Lambda function.
3. The Lambda function runs your custom code (Python in our case).
4. Lambda returns a response, which API Gateway forwards back to the client.

This simple architecture is powerful because you can connect multiple Lambda functions to different API routes and methods (e.g., GET /users, POST /orders).

Step-by-Step Setup

1. Create a Lambda Function

1. Log in to the AWS Management Console.
2. Go to AWS Lambda → Create function.
3. Choose Author from scratch, give it a name (e.g., hello-world), and select Python 3.x as runtime.
4. In the code editor, paste the following function:

import json

def lambda_handler(event, context):
    name = event.get("queryStringParameters", {}).get("name", "World")
    return {
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"message": f"Hello, {name}!"})
    }

2. Create an API Gateway

1. Go to Amazon API Gateway → Create API.
2. Choose HTTP API (simpler) or REST API (more configurable). For this tutorial, select HTTP API.
3. Create a new API and attach it to your Lambda function (hello-world).
4. Deploy the API.

3. Test the API

Once deployed, you will get an endpoint URL such as:

https://abc123.execute-api.us-east-1.amazonaws.com/default/hello-world

Now test it:

curl "https://abc123.execute-api.us-east-1.amazonaws.com/default/hello-world?name=Alice"

Response:
{ "message": "Hello, Alice!" }

Congratulations! You have just built a serverless REST API with AWS.

Why Use API Gateway + Lambda?

– Scalability: Handles traffic spikes automatically.
– Cost efficiency: Pay only when functions are executed.
– Simplicity: No server management, no patching, no scaling headaches.
– Flexibility: Connect Lambda to databases (DynamoDB, RDS), queues (SQS), or even event streams (Kinesis).

Next Steps

What we built is a minimal example. In production, you might:
– Add authentication & authorization (AWS Cognito, IAM policies, custom JWT).
– Use Infrastructure as Code (AWS SAM, CDK, or Terraform).
– Enable logging & monitoring (CloudWatch, X-Ray).
– Connect to other AWS services for full applications.

Conclusion

AWS API Gateway and AWS Lambda together make it incredibly easy to create serverless REST APIs. With just a few clicks and a small Python script, you can deploy an endpoint that scales automatically and costs only when used.

If you are building modern applications, experimenting with microservices, or simply want to avoid server management, this serverless approach is a great starting point.

en_USEnglish