AWS Builder Center

Build Your First Serverless API with AWS Lambda + API Gateway

Learn how to build a simple serverless REST API using Python, AWS Lambda, and Amazon API Gateway—without managing servers.

Full Stack Developer | Python | Django | PHP | React | MySQL | REST Api | AWS

Introduction

When building a backend application, we traditionally need to run a server continuously and manage its operating system, software, scaling, and availability.
AWS provides a different approach through serverless computing.
With AWS Lambda, we can run code without managing servers. Amazon API Gateway can provide an HTTP endpoint that invokes our Lambda function.
A simple architecture looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
Client / Browser
|
| HTTP Request
↓
Amazon API Gateway
|
| Invoke
↓
AWS Lambda
|
↓
Python Application Logic
This combination is a great starting point for developers who want to understand how modern cloud-based APIs work.

What is AWS Lambda?

AWS Lambda is a serverless compute service that runs your code in response to events.
Instead of maintaining a server yourself, you upload your function to Lambda and AWS handles the underlying infrastructure.
For example, a Lambda function written in Python can look like this:
1
def lambda_handler(event, context): return { "statusCode": 200, "body": "Hello from AWS Lambda!" }
The function executes when it is invoked and returns a response.

Why use Lambda?

  • No server management
  • Automatic scaling
  • Supports multiple programming languages
  • Integrates with many AWS services
  • Pay based on usage
  • Suitable for APIs, automation, event processing, and scheduled tasks

What is Amazon API Gateway?

API Gateway allows applications to communicate with backend services through HTTP APIs.
For our project, API Gateway acts as the entry point.
For example:
1
GET /hello
The request reaches API Gateway, which invokes our Lambda function.
1
2
3
4
5
6
7
GET /hello
↓
API Gateway
↓
Lambda
↓
JSON Response
This allows a frontend application, mobile application, or another service to communicate with our backend.

Building the API

Let's create a simple Python-based serverless API.

Step 1: Create a Lambda Function

Open the AWS Management Console and navigate to AWS Lambda.
Create a new function.
Choose:
  • Create function: Author from scratch
  • Runtime: Python
  • Architecture: Default architecture
  • Execution role: Create a new role with basic Lambda permissions
Give the function a name such as:
1
hello-serverless-api
After creating the function, AWS provides a code editor where we can write our Python code.

Step 2: Write the Python Function

Replace the default code with:
1
import jsondef lambda_handler(event, context): response = { "message": "Hello from my serverless API!", "status": "success" } return { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": json.dumps(response) }
Deploy the function.
The Lambda handler receives two important parameters:
1
eventcontext

event

Contains information about the event that triggered the function.
For an API request, it can contain information such as:
  • HTTP method
  • URL path
  • Query parameters
  • Request body
  • Headers

context

Provides information about the Lambda execution environment.

Step 3: Create an API Gateway API

Now we need a way for users or applications to call our Lambda function.
Open Amazon API Gateway and create an API.
For a simple project, an HTTP API is a good starting point.
Configure the API to integrate with our Lambda function:
1
2
3
4
API Gateway
|
↓
hello-serverless-api
API Gateway will provide an HTTP endpoint similar to:
1
https://example.execute-api.region.amazonaws.com
Your actual URL will be different.

Step 4: Create a Route

A route determines how an HTTP request is handled.
For example:
1
GET /hello
This means:
  • HTTP method → GET
  • Path → /hello
The request is then connected to our Lambda function.
The complete flow becomes:
1
2
3
4
5
6
7
8
9
GET /hello
↓
API Gateway
↓
Lambda
↓
Python Code
↓
JSON Response

Step 5: Test the API

After deploying the API, open its endpoint in a browser or use a tool such as Postman.
You can also use:
1
curl https://your-api-url/hello
The response could look like:
1
2
3
4
{
"message": "Hello from my serverless API!",
"status": "success"
}
🎉 You now have a basic serverless API running on AWS.

Understanding the Architecture

The architecture is simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Internet
|
↓
┌─────────────────┐
│ API Gateway │
└────────┬────────┘
|
↓
┌─────────────────┐
│ AWS Lambda │
│ Python │
└────────┬────────┘
|
↓
Application Logic
The important thing to understand is that we don't need to create or maintain an EC2 server for this API.
AWS manages the infrastructure required to execute the Lambda function.

Adding Request Data

Serverless APIs become more useful when they can process input from clients.
For example, a request could contain:
1
2
3
{
"name": "Sriram"
}
The Lambda function can read the request body:
1
import jsondef lambda_handler(event, context): body = json.loads(event.get("body", "{}")) name = body.get("name", "User") return { "statusCode": 200, "body": json.dumps({ "message": f"Hello, {name}!" }) }
This demonstrates an important backend concept:
1
2
3
4
5
6
7
8
9
Client Request
↓
API Gateway
↓
Lambda
↓
Process Data
↓
JSON Response

Lambda vs Traditional Server

A traditional backend might look like:
1
2
3
4
5
6
7
Client
↓
Web Server
↓
Application Server
↓
Database
With a serverless approach:
1
2
3
4
5
6
7
Client
↓
API Gateway
↓
Lambda
↓
Database
The major difference is that AWS manages the compute infrastructure for Lambda.

Lambda vs EC2

Both Lambda and EC2 can run backend applications, but they solve different problems.
FeatureLambdaEC2
Server managementAWS managedYou manage it
ScalingAutomaticConfigure/manage
Server running continuouslyNot requiredUsually yes
Best forFunctions, APIs, eventsFull applications/servers
Infrastructure controlLowerHigher
Operating system accessNoYes
For small APIs and event-driven applications, Lambda can be a convenient option.
For applications requiring full operating-system control or long-running workloads, EC2 may be more appropriate.

Security Considerations

Security is an important part of serverless development.

1. Use IAM permissions

Lambda functions use an IAM execution role to access AWS resources.
Only give the function the permissions it actually needs.
For example, if a Lambda function only needs to read from a specific S3 bucket, avoid giving it broad access to every S3 resource.

2. Protect sensitive information

Don't hard-code:
1
2
3
4
API keys
passwords
database credentials
secret tokens
For sensitive credentials, AWS recommends using services such as AWS Secrets Manager.

3. Secure API access

Depending on the application, an API may need:
  • Authentication
  • Authorization
  • Request validation
  • Rate limiting
  • Monitoring
Never assume that an API is secure simply because it uses Lambda.

Where Can You Go Next?

Once you understand Lambda + API Gateway, you can expand the project.
A useful learning path is:
1
2
3
4
5
6
7
8
9
10
11
12
13
Lambda
↓
API Gateway
↓
DynamoDB
↓
IAM
↓
CloudWatch
↓
Authentication
↓
CI/CD
For example, you could build a serverless task management API:
1
2
3
4
5
6
7
8
9
10
Frontend
|
↓
API Gateway
|
↓
Lambda
|
↓
DynamoDB
The API could provide:
1
2
3
4
5
POST /tasks
GET /tasks
GET /tasks/{id}
PUT /tasks/{id}
DELETE /tasks/{id}
This would give you practical experience with REST APIs, serverless computing, databases, IAM, and cloud deployment.

Key Takeaways

From this project, you can understand several important cloud concepts:
  • AWS Lambda runs application code without managing servers.
  • API Gateway provides HTTP endpoints for applications.
  • Python can be used to build Lambda functions.
  • IAM controls what AWS resources a Lambda function can access.
  • Serverless applications can scale automatically.
  • Lambda and EC2 are suitable for different types of workloads.
  • Security and permissions are important even in serverless applications.

Conclusion

AWS Lambda and API Gateway provide a simple way to understand how modern serverless applications are built.
For developers coming from Python and backend development, this is a useful next step after learning traditional web APIs.
Instead of thinking only in terms of "Where is my server?", serverless encourages us to think about "What should happen when an API request arrives?"
That shift in thinking is one of the most useful concepts to learn when starting with cloud computing.
Next project: Build a CRUD API using Lambda + API Gateway + DynamoDB and connect it to a React frontend.
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