AWS Builder Center

Building a Serverless REST API Using AWS Lambda and Amazon API Gateway ⚡

A Lightweight, Scalable, and Fully Managed Approach to Modern API Development



📘Introduction

This document provides a detailed, step-by-step guide for creating a serverless REST API using AWS Lambda and Amazon API Gateway. The process includes writing and testing a Lambda function, integrating it with an external REST API (JSONPlaceholder), configuring API Gateway, deploying the API, and validating the responses through Postman and browser-based testing. This documentation is designed for individuals seeking a structured and methodical approach to building serverless applications on AWS.

🏗️ Architecture Overview

The architecture follows a fully serverless design, eliminating the need for infrastructure provisioning or server management. Client requests are routed through Amazon API Gateway, which acts as the front door for the REST API. API Gateway invokes an AWS Lambda function, which contains the application logic. The Lambda function communicates with an external third-party REST API (JSONPlaceholder), processes the response, and returns the data back through API Gateway to the client. This loosely coupled architecture ensures scalability, fault tolerance, and cost efficiency while adhering to modern cloud-native best practices.

🧩 Architecture Components

  • Amazon API Gateway: Acts as the REST API endpoint exposed to clients. It handles request routing, method configuration, CORS settings, deployments, and stages.
  • AWS Lambda: Executes backend logic without requiring server management. It processes incoming requests from API Gateway, makes outbound calls to the external API, and formats the response.
  • External REST API (JSONPlaceholder): Serves as a third-party data source. The Lambda function retrieves sample JSON data from this API to demonstrate external integration.
  • IAM (Implicit Role Usage): Provides AWS Lambda with the necessary permissions to execute and interact with API Gateway securely.
  • Clients (Browser / Postman): Used to test and validate the API responses by sending HTTP requests to the deployed API endpoint.

🎯 Why This Project?

This project demonstrates how serverless technologies can be used to build scalable, cost-efficient, and maintenance-free APIs. By integrating AWS Lambda with API Gateway, developers can deploy backend logic without provisioning servers. Additionally, the use of JSONPlaceholder as an external data source introduces practical experience in handling third-party API calls within a Lambda function.

✨ Key Features

  • Serverless compute using AWS Lambda
  • REST API creation and deployment using Amazon API Gateway
  • Integration with an external API (JSONPlaceholder)
  • End-to-end testing using Lambda test events, API Gateway test console, and Postman
  • Configuration of CORS, methods, deployments, and triggers
  • A complete flow from writing code to exposing a public API endpoint

🛠️Execution Flow

I. Creating and Testing the AWS Lambda Function

  • Open the AWS Management Console.

  • Search for AWS Lambda and Click Create a function.

  • Enter a name for the Lambda function (example: my-new-lambda-function) and set the runtime to Python 3.9.

  • Keep all other settings as default. and click on Create function.

  • Review the auto-generated code in  lambda_function.py .

  • Prepare to test the function, Click Test in the left-hand panel.

  • Select Create new test event.

  • Assign a name to the event (example: NewEvent).

  • Delete the default JSON in the event input.

  • Replace it with the following:
{
"test": "test data"
}

  • Save and test the event.

  • View the response output in the console.


II. Adding Configuration for External API Integration

  • Create a new file in the Lambda function named config.py.
  • Add the following line of code:
base_url = “jsonplaceholder”

  • Open a new browser tab and search for JSONPlaceholder

  • You will see a demo code and after running the script you will see the output for that code

  • Copy the base URL:
https://jsonplaceholder.typicode.com/
  • Paste this URL into  config.py  in place of the previous value.

  • Modify the  lambda_function.py  file to incorporate the external API integration.

  • Deploy and test the Lambda function.
  • Confirm that the output matches the expected JSONPlaceholder response.

💻 Code Reference: Here's the code snippet you can refer to and adapt for your projects —  view code 

III. Creating and Configuring the REST API Using API Gateway

  • Search for API Gateway in AWS and open it.
  • Click Create API.

  • Select REST API and click Build.

  • Provide an API name (example: get-user-data).
  • Keep the endpoint type as Edge-optimized.

  • Under Security Policy, select SecurityPolicy_TLS12_2018-EDGE.
  • Click Create API.

  • In the Resources section, click Enable CORS and save with default settings.


  • Back in the Resources section, click Create Method.

  • Configure the method:
  • Method Type: GET
  • Integration Type: Lambda
  • Lambda Function: select the previously created Lambda function

  • Scroll down and click Create Method.

  • Once created, return to the Resources section.
  • Open the Test tab.
  • Scroll and click Test to execute the method.

  • Confirm that the response matches the JSONPlaceholder output.

  • Scroll up and choose Deploy API.

  • Select New Stage, name it dev, and deploy.


IV. Testing the Deployed API

  • In the Stage Details section, copy the generated Invoke URL.

  • Open Postman.

  • Paste the URL and click Send.
  • Confirm that the response is identical to the previously observed outputs from JSONPlaceholder, Lambda tests, and API Gateway tests.


V. Adding API Gateway as a Trigger for Lambda

  • Return to the Lambda function page.
  • Click Add Trigger.

  • In the Trigger Configuration:
  • Select Source: API Gateway
  • Intent: Use existing API
  • Existing API: select the previously created API
  • Deployment Stage: dev
  • Security: Open

  • Add the trigger.

  • Copy the displayed API endpoint.

  • Paste it into a browser tab.
  • Verify that the output matches the JSONPlaceholder data, confirming successful integration.


VI. Cleanup

  • Delete the Lambda function.

  • Delete the REST API from API Gateway.


✅Conclusion

This project demonstrates a complete workflow for building a serverless REST API using AWS Lambda and Amazon API Gateway. Through integration with JSONPlaceholder, the Lambda function showcases the ability to communicate with external APIs, process responses, and return data through an API endpoint. The steps covered in this documentation provide a clear, structured foundation for creating scalable, low-maintenance backend services on AWS. This project serves as an essential learning experience for understanding serverless architecture and API management within the AWS ecosystem.
📂 Full Repository: Explore the complete project with setup guides and architecture details on  GitHub 
🤝 Let’s Connect: I’d love to connect on  LinkedIn  to discuss cloud, Kubernetes, and DevOps practices
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