Open In App

AWS Lambda – Create a Lambda Function in Python, Integrated with API Gateway

Last Updated : 16 Apr, 2023
Like Article

Pre-requisite: AWS 

We will begin with creating a lambda function in the AWS Lambda console and write our Python code for the same. We will test our function by creating the new event and verifying the response. Next, we will configure the lambda function with the JSON placeholder which is a sample API. We will be using the concept of layers in the AWS lambda function to achieve the same. Layers can be understood as a ZIP archive that contains runtimes, libraries, or other functional dependencies which can be used across multiple functions. Lastly, we will test our integration.

Step 1: Go to AWS Management Console and search for lambda. Click on “Lambda” and next click on “Create Function”.

Step 2: Give your function a name, here we have given it an if-test. Select the runtime as Python 3.9. Click on create function.

Creating function


Step 3: For API call we will be using JSON placeholder which is a sample API. 

Basically, we will be using a request library in Python, we will call the API.

and whatever output is received will be shown by the lambda function.

Step 4: Create a new file

add the following line of code to 

base_url = ""
Adding python code to


Step  5: Replace the code in the Lambda_function file with the following:

import json
from config import base_url
import requests

def lambda_handler(event, context):
    res = requests.get(url=base_url+'todos/1')
    return {
        'statusCode': res.status_code,
        'body': res.json
add above code to


Step 6: Once we have replaced the contents simply click on Deploy and Test. The test UI will appear, modify the changes as shown in the below image

Configure test event


Click on create Event. Once it’s done click on Test again.

Create event


This is the error we receive. This is because the function is not able to find the requests module. In order to resolve the issue we must pass the dependencies in the layer.

Function created successfully


But before we create and add a layer we must save the dependencies in the zip file and then upload it to the layer. To do so we will use VS Code on the local computer.

Step 7: Use the following commands to create a zip folder of dependencies:

mkdir pypackages
pip3 install requests -t /home/rishaw/gfg-test/pypackages
zip -r pypackages/
Zip floder


Step 8: Once the zip is created go to the AWS console and click on create a layer.

Create layer


Once the layer is created we can attach it to our Lambda function.

Step 9: Go to your Lambda Function and scroll down to layers and click on add a layer.Make changes as shown in the image.

add the layer


Once the layer is added, click on Test again. This time the execution is completed and the output is visible.

Output of the sample function


Similar Reads

How To Deploy GraphQL API Using AWS Lambda And AWS API Gateway ?
GraphQL is known for its flexibility and efficiency. AWS Lambda, on the other hand, provides a serverless computing service that allows developers to run code without managing servers, and we pay for what we use. We can combine the power of GraphQL with the scalability and ease of AWS Lambda to serve our GraphQL application easily with minimal step
6 min read
Building a Serverless Blog with AWS Lambda and API Gateway
AWS Lambda is a serverless computing service offered by AWS. It enables you to run code in response to events without the need to manage servers. This event-driven service charges you based on actual compute time, making it cost-effective. Lambda supports various programming languages, scales automatically, and seamlessly integrates with other AWS
9 min read
Build an API Gateway REST API with Lambda Integration
Pre-requisite: AWS Amazon Web Services is a leading cloud provider which provides us with plenty of Paas, and Iaas, and services that we can use to build and deploy our applications. we going to build and Deploy a REST API with API Gateway which is integrated with AWS Lambda and expose GET and POST methods, here we going to use an open-source frame
4 min read
Complete Guide To Create AWS API Gateway With Terraform
AWS Application Programming Interface (API) Gateway is a completely managed service presented by Amazon Web Services (AWS) that empowers developers to create, monitor, deploy, and secure APIs at any scale. It goes about as a gateway for managing and routing HTTP and WebSocket traffic to backend service, including AWS Lambda function, Amazon EC2 ins
7 min read
Create a REST API as an Amazon Kinesis Proxy in API Gateway
Pre-Requisite: AWS Amazon Kinesis is a managed, scalable, cloud-based service that enables real-time processing of enormous amounts of data per second in real-time. It is intended for real-time applications and lets developers collect an unlimited amount of data from many sources, scaling up and down that can be run on EC2 instances. It is used to
4 min read
Create a REST API as an Amazon S3 Proxy in API Gateway Automation
Amazon Web Services is a leading cloud provider which provides us with plenty of Paas, and Iaas, and services that we can use to build and deploy our applications. we gonna build and Deploy a REST API with API Gateway which acts as a proxy to S3 and can be used to perform Read/Write on S3 without any intermediate service, here we gonna use an open-
5 min read
AWS DynamoDB - Insert Data Using AWS Lambda
In this article, we will look into the process of inserting data into a DynamoDB table using AWS Lambda. Amazon DynamoDB is a completely owned NoSQL proprietary provider that helps key-value and textual statistics systems and is supplied via way of means of as a part of Amazon Web Services. AWS Lambda is an event-driven, serverless compu
3 min read
AWS Lambda Functions With AWS CLI
Amazon Web Services (AWS) is a comprehensive cloud computing platform offering many services, including storage, computing, databases, and more. AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). we can create functions and self-contained applications. With AWS Lambda functions, we can perform any kind of computing
6 min read
AWS Lambda Deployments with AWS CloudFormation
AWS CloudFormation allows to create AWS resources with help of templates. It provisions and deploy all the resources required for the entire application described as a Stack. Templates can be updated and replicated as required to reduce the overhead of implementing and provisioning infrastructure. let's learn how we can provision lambda deployments
6 min read
Difference Between Internet Gateway and NAT Gateway
Pre-requisite: AWS In the world of networking, the terms "Internet Gateway" and "NAT Gateway" are commonly used, but they can often be confusing. In this article, we will explain the difference between an Internet Gateway and NAT Gateway. We will define each term and explain how they work, their use cases, and the main differences between them. Int
4 min read