AWS Builder Center
Build a Serverless REST API Using AWS Lambda and API Gateway

Build a Serverless REST API Using AWS Lambda and API Gateway

Learn how to build and deploy a serverless REST API using Amazon API Gateway and AWS Lambda. Explore request handling, Lambda execution, IAM permissions, testing, and how serverless applications scale without managing servers.

Introduction

When developing backend applications, one of the first challenges is deciding where and how to host your application.
Traditionally, developers deploy backend applications on virtual machines such as Amazon EC2. This requires managing servers, configuring environments, handling scaling, and monitoring infrastructure.
But what if you could run backend code without managing a server?
This is where AWS Lambda and Amazon API Gateway come in.
In this article, we will understand how these services work together and build a simple serverless REST API.

1. What Is Serverless Computing?

Serverless computing allows developers to run application code without managing the underlying servers.
It does not mean servers do not exist. AWS manages the infrastructure for you.
With AWS Lambda:
  • You write and upload your code.
  • AWS executes it when triggered.
  • You pay based on usage and execution.
  • AWS manages the underlying compute infrastructure.
  • Your application can scale automatically.
This makes serverless architecture useful for APIs, automation, event processing, and lightweight backend services.

2. Understanding the Architecture

Before writing code, let's understand how the services communicate.
Architecture flow:
Client → Amazon API Gateway → AWS Lambda → Response → Client
Amazon API Gateway acts as the entry point for HTTP requests.
AWS Lambda executes the backend logic.
For example, when a user visits  /hello , API Gateway forwards the request to Lambda. Lambda processes the request and returns a response.

3. Create an AWS Lambda Function

First, open the AWS Management Console.
  1. Search for AWS Lambda.
  1. Select Create function.
  1. Choose Author from scratch.
  1. Enter a function name:  hello-api .
  1. Select Python as the runtime.
  1. Create the function.
For this example, we will use a simple function that returns a JSON response.

Lambda Function Code

import json

def lambda_handler(event, context):
response = {
"message": "Hello from AWS Lambda!",
"status": "success"
}

return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps(response)
}
Click Deploy to save the function.
You can test it using the Lambda console's test feature.

4. Create an API Using Amazon API Gateway

Now we need an HTTP endpoint through which users can access our Lambda function.
  1. Open Amazon API Gateway.
  1. Choose Create API.
  1. Select HTTP API.
  1. Add an integration.
  1. Choose Lambda.
  1. Select the  hello-api  function.
  1. Create a route such as  GET /hello .
  1. Create and deploy the API.
API Gateway will provide an HTTP endpoint.
When a client sends a GET request to that endpoint, API Gateway invokes the connected Lambda function.

5. Test the API

Copy the API endpoint provided by API Gateway and append the  /hello  route.
For example:
https://your-api-id.execute-api.region.amazonaws.com/hello
You can test it using a browser, Postman, or curl:
curl https://your-api-id.execute-api.region.amazonaws.com/hello
Expected response:
{
"message": "Hello from AWS Lambda!",
"status": "success"
}
Congratulations! You have created your first serverless REST API.

6. How Does AWS Lambda Scale?

One of the biggest advantages of Lambda is that AWS manages the compute infrastructure.
If multiple requests arrive, Lambda can create additional execution environments to handle incoming workloads, subject to concurrency limits and account quotas.
You do not need to manually launch additional EC2 instances for every increase in traffic.
However, scaling is not unlimited. Developers should understand concurrency limits, execution timeouts, memory configuration, and downstream service capacity.

7. Understanding IAM Permissions

Security is an important part of every AWS application.
AWS Lambda uses an execution role to determine which AWS services and resources the function can access.
For example, if your Lambda function needs to read data from an S3 bucket, its execution role must have the required S3 permissions.
Always follow the principle of least privilege.
Give your Lambda function only the permissions it actually needs.

8. Lambda vs EC2: What's the Difference?

Feature
AWS Lambda
Amazon EC2
Infrastructure management
AWS managed
User managed
Server configuration
Minimal
Required
Scaling
Managed automatically
Configure scaling
Billing
Based on requests and execution duration
Based on provisioned resources
Best suited for
Event-driven workloads and APIs
Long-running applications and custom workloads
Neither service is universally better.
The right choice depends on your application's workload, execution requirements, architecture, and cost model.

9. Where Can We Use This Architecture?

This architecture can be useful for:
  • REST APIs for mobile and web applications.
  • Backend operations for React applications.
  • Form submissions and data processing.
  • Event-driven automation.
  • Lightweight microservices.
  • Webhooks and integrations.
For a larger application, Lambda can also communicate with services such as Amazon DynamoDB, Amazon S3, and other AWS services.

10. Important Things to Remember

Before deploying a serverless application in production, consider:
  • Configure appropriate IAM permissions.
  • Set suitable Lambda memory and timeout values.
  • Monitor execution using Amazon CloudWatch.
  • Handle errors and unexpected requests.
  • Protect APIs using suitable authorization and throttling.
  • Monitor costs and service quotas.
  • Avoid storing secrets directly in source code.
Also, remember that Lambda has execution limits and is not designed for every type of long-running workload.

Conclusion

AWS Lambda and Amazon API Gateway make it possible to build and deploy backend APIs without managing traditional servers.
By combining these two services, developers can create scalable, event-driven applications while focusing more on application logic and less on infrastructure management.
If you are learning AWS, building a serverless REST API is an excellent hands-on project to understand cloud-native application development.
Keep building. Keep learning. Keep exploring AWS!
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