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:
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_layer 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 python_requests_layer.zip
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.
From the Layers page, click on the orange "Create layer" button to open the creation wizard.
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.
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".
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.
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.