Build an API Gateway REST API with Lambda Integration Part I

In this tutorial, we’ll walk through setting up an REST API in AWS API Gateway with Lambda Integration, configuring various integration methods and handling method requests and responses.
Introduction
API Gateway Lambda integration allows us to connect our API Gateway resources to Lambda functions and use them as backend logic for our API. There are two types of Lambda integrations:
Lambda Proxy Integration
This approach simplifies development by offloading request parsing and response formatting to API Gateway. This object is then passed to the Lambda function as an event object. The Lambda function processes the event object and returns a JSON response object, which API Gateway further parses and sends back to the client as the API response. This is suitable for most API implementations with JSON input and output formats.
Lambda Non-Proxy Integration
This approach provides greater flexibility and control over the request and response format. The Lambda function parses the request, processes it, and generates the response. This approach provides greater flexibility and control over the request and response format but requires more development effort as the Lambda function needs to handle parsing and formatting.
Implementation
Let’s extend the tutorial by creating a simple CRUD API for managing a list of books.
Step 1: Create an API in API Gateway
In AWS API Gateway, when we create a new API, we can choose between REST API and HTTP API. REST APIs offer more advanced features and capabilities, making them suitable for complex API scenarios. HTTP APIs prioritize simplicity and clarity, making them perfect for streamlined use cases.
Set Up API Gateway
In the API Gateway console, choose Create API and select REST API. Give it a descriptive name like Book Management API. Select Regional as the desired API endpoint type then click on Create API.

Step 2: Deploy a Lambda function
Next, we need to create Lambda functions to handle the logic for each method. Having a separate Lambda function for each resource method (e.g., Create, Read, Update, Delete) is a common and recommended best practice. This approach follows the Single Responsibility Principle and helps maintain a clean and modular codebase.
Create Function (Create a new book):
import json
def create_book(event, context):
request_body = json.loads(event['body'])
# Validate required fields (e.g., title, author, etc.)
if not request_body.get('title') or not request_body.get('author'):
return {
'statusCode': 400,
'body': json.dumps({'message': 'Title and Author are required.'}),
}
new_book = {
'id': 123, # Replace with a proper ID generation method
'title': request_body['title'],
'author': request_body['author'],
# Other book details...
}
return {
'statusCode': 201,
'body': json.dumps(new_book),
}Read Function (Create a book by ID):
import json
def get_book(event, context):
book_id = event['bookId']
book = {
"bookId": 123,
"bookTitle": "Harry Potter",
"authorName": "J. K. Rowling"
}
if not book_id == "123":
return {
'statusCode': 404,
'body': json.dumps({'message': 'Book not found.'}),
}
return {
'statusCode': 200,
'body': json.dumps(book),
}Step 3: Create Resources and Methods
Next, create resources for the API endpoints, e.g., /books for listing and creating books, and /books/{id} for getting, updating, and deleting individual books. Define appropriate HTTP methods (GET, POST, PUT, DELETE) for each resource.

Step 4: Test the API
API Gateway provides a testing feature called Test for API methods, allowing us to test them before deploying to a stage.
In the API Gateway console, navigate to our API and select the resource/method we want to test. Click on the Test tab. On the test page, we can configure a test event. This is a JSON payload that simulates the input to your API method.
Let’s test the POST request by inputting the request body as below:

Click the Test button to simulate the API request using the configured test event. Review the results, including the request sent and the response received.

The log displays empty values for the method request path, query string, and headers, revealing that no specific details have been extracted from the request yet.
The log indicates that the HTTP POST request is received with a JSON payload. The original request body contains information about the book, including the title and author.
It then transforms this information, prepares an endpoint request to a specified Lambda function, and forwards the request.
Upon receiving the Lambda response, API Gateway performs transformations before sending the final response to the client. This involves mapping the Lambda response to a structured API response.
In the next article, we’ll learn how to configure the method and integration to define the structure of API requests and responses.
Happy coding! 🎉
- Leave a comment if you have any question.
- Clap if you find this tutorial useful.
- Follow me to get notified when I publish new tutorials.
- Buy me a coffee to support me.