Build and Deploy a Scalable Backend Using AWS Lambda and API Gateway

Want to deploy your backend? Want a cost-effective and scalable way to deploy your APIs without worrying about infrastructure? AWS Lambda and API Gateway make it easy to go serverless and focus purely on your code.
In this blog, you’ll learn how to build and deploy a backend using AWS Lambda and API Gateway. This guide is tailored for beginners, so even if you’re new to AWS or serverless, you’ll be able to follow along and get your backend live in the cloud.
🧠 Understanding the Theory
What is AWS Lambda?
AWS Lambda is a compute service that lets you run code without provisioning or managing servers. You simply upload your code, and Lambda executes it in response to events (like API requests). It’s event-driven, auto-scaling, and you only pay for the compute time you use—making it ideal for lightweight, cost-effective workloads.
What is API Gateway?
API Gateway acts as a bridge between your users and your backend. It handles:
Incoming HTTP requests
Routing requests to AWS Lambda
Sending responses back to clients
It also offers built-in features for security, throttling, monitoring, and versioning.
🚀 Step-by-Step Guide
1. Build Your Application
Develop your backend using your preferred language (e.g., Node.js, Python).
If you're using Express.js, wrap it using the
serverless-http
package so it can run in AWS Lambda.
Example for Express:
npm i serverless-http
index.js:
const serverless = require("serverless-http"); //lambda config
const express = require("express");
const app = express();
app.use(express.json());
app.get("/users", (req, res) => {
res.json({ message: "Hello from Lambda!" });
});
module.exports = app;
module.exports.handler = serverless(app); //lambda config
- Ensure all dependencies (like
node_modules
) are included before zipping.
2. Zip the Code
- Zip your entire project folder including all dependencies.
- Name the archive, e.g.,
my-backend.zip
.
3. Set Up the Lambda Function
Go to the AWS Lambda Console.
Click Create function.
Choose Author from scratch.
Set a name and runtime (e.g., Node.js 18.x), then create the function.
In the Code section, choose Upload from .zip file and upload your archive.
Important: Set the Handler to
index.handler
if the entry point of application is index.js
4. Set Up API Gateway
Go to the API Gateway Console.
Choose REST API, then click Build.
Fill in the required details and create the API.
Create a Resource (e.g., /users
)
In the left sidebar under Resources, click Actions > Create Resource.
Enter the Resource Name as
users
and the Resource Path as/users
.
Create a Method (e.g., GET
)
Select the
/users
resource.Click Actions > Create Method.
Choose GET (or POST/PUT as needed) and click the checkmark.
Select Lambda Function and enter the name of your Lambda function.
Save and grant permissions if prompted.
Deploy the API
Click Actions > Deploy API.
Create a new stage (e.g.,
prod
).Copy the Invoke URL (e.g.,
https://xyz.execute-api.region.amazonaws.com/prod/users
).
5. Test Your API
Open Postman.
Create a new request using the invoke URL.
Send a GET request to
/users
.You should receive your Lambda function's response.
✅ Done!
Your backend is now live and accessible via a public endpoint. You’ve successfully deployed a serverless backend using AWS Lambda and API Gateway—without managing a single server.
Subscribe to my newsletter
Read articles from Tanseer Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
