back to blog

Creating an AWS Lambda Layer for Python Requests Module

Read Time 4 mins | Written by: Kenneth Hough

Are you looking to make HTTP calls in your AWS Lambda functions, but hit a roadblock because the Python requests module is not natively available? Fear not, as we have a solution for you! By creating a custom AWS Lambda layer for the requests module, you can easily add the functionality you need to your Lambda functions. In this blog post, we'll walk you through the steps to create and implement this layer so that you can seamlessly make HTTP requests within your Lambda functions. Let's get started!

Saving the Requests Module

The first step is to save the requests module to a directory so we can zip it and upload to AWS. In this tutorial, we will be using Python 3.9.7 and pip version 23.1.2 on macOS.

Create a new directory for your python module (the directory name must be python):

mkdir python

Once a new directory is created, well save the requests module to the folder by specifying the directory with the -t option, like below:

pip install -t python requests

As you may have noticed, you can specify any number of modules or dependencies you need for your project using pip. Once pip downloads the module to your target directory, we will zip the folder before we can upload to AWS.

zip -r9 python_requests_layer.zip python

The zip command used above specifies the r and 9 options. The r option is to recurse into directories so that subfolders are included in our zip file. The 9 option is the compression strength, which is on a scale from 0-9.

Creating the Layer in AWS

Once we have our zip file, we are ready to navigate to the AWS Lambda service page and select Layers.

AWS Lambda layers

From the Layers page, click on the orange "Create layer" button to open the creation wizard.

Creating a layer in AWS

Specify a name for your new layer and a description so you know what this layer is. Select the "Upload a .zip file" option as we will be uploading the zip file we just created.  Next, click the upload button and select the zip file for the python requests layer. In this particular example, I have chosen arm64 as the architecture as there is better cost-performance compared to its x86 counterpart, however, make sure you select the architecture most appropriate for your requirements. Lastly, since the layer was created for Python 3.9, I will select this version of Python along with Python 3.10 from the list of compatible runtimes.

Click, "Create" and you are all set! Now we have a new layer with the requests module.

Using the Layer in a Lambda Function

Let's see how we can include this new layer in our Lambda function. If you have an existing Lambda function in python, navigate to the function. If you do not have an existing, feel free to keep reading or create a new example Lambda function so you can follow along.

Lambda function overview

From the Function overview section, click on the "Layers" button underneath your function name. Alternatively, you can just scroll to the bottom of the page where you will see a Layers section. Click on "Add a layer".

Lambda function layers

In the Add layer wizard, select Custom layers and then the layer you just added from the drop down. Since we only have 1 version, we will select 1. Click add.

Adding a layer to AWS Lambda function

Now you have added the requests module to your python Lambda script! In conclusion, creating an AWS Lambda layer for the Python requests module is a simple yet powerful solution for those who want to make HTTP calls in their AWS Lambda functions. With this approach, developers can bypass the hassle of installing and managing dependencies, while keeping their code clean and concise. By following the steps outlined in this blog, you can easily create your own layer and start leveraging the requests module in your AWS Lambda functions today. Don't let missing dependencies hold you back - take advantage of this powerful solution and unlock the full potential of your AWS Lambda functions.

How We Can Help

At KeyQ, we understand the importance of leveraging the latest technologies to help businesses stay ahead of the competition. Our team of experts can assist with all aspects of AWS Lambda and serverless technology, including creating custom Lambda layers, optimizing your Lambda functions, and providing guidance on how to best utilize these powerful tools. With our affordable and high-quality software development services, as well as our cloud-enabled framework for rapid development, prototyping, and deployment, KeyQ is the perfect partner for any business looking to take full advantage of AWS Lambda and serverless technology. Contact us today to see how we can help elevate your business to the next level.

A Framework Built to Accelerate App Development for Startups

Kenneth Hough

Background

I founded KeyQ in March of 2020 with the vision of helping businesses achieve the next level of success through delivering innovative and meaningful cloud solutions. Since its inception, I have worked with several businesses, non-profit organizations, and universities to design and build cloud applications that have helped streamline their business processes and reduce costs.

Prior to KeyQ, I was a medical researcher at the University of Alabama at Birmingham (UAB) in the Division of Pulmonary, Allergy, and Critical Care Medicine. UAB is also where I worked on my doctoral thesis under the mentorship of Dr. Jessy Deshane and Dr. Victor Thannickal. During my doctoral work at UAB I was exposed to the “omics” and big data, which has influenced my career choice to develop data-driven analytics platforms in the cloud.

I also have to give a big shoutout to my undergraduate education at Worcester Polytechnic Institute (WPI), where I majored in biochemistry. WPI’s motto is “Lehr und Kunst,” which roughly translates to “Theory and Practice” or “Learning and Skilled Art.” WPI truly cherishes and upholds this pedagogy, which can be seen by the teaching styles and class sizes. The learning experience I had at WPI is unique and has shaped me to be who I am, being able to learn, practice and apply.

Personal Interests

I love to learn innovative technologies and try new things. I have a broad area of interests that include serverless architectures, machine learning, artificial intelligence, bioinformatics, medical informatics, and financial technology. I am also working towards my CFA level 1 exam for 2021. Other interests and hobbies include traveling, rock climbing, rappelling, caving, camping and gardening!