Using the requests library in AWS Lambda (with screenshots)
Python’s built-in library for making http requests,
urllib, is a nightmare to deal with. Even the library’s own documentation recommends using the third-party
requests library. This is all fine and well — you
pip install requests and get on with your life. But things are a little more complicated when you need to write an AWS Lambda function that uses the
By default, a Lambda function has access to the Python Standard Library, as well as the
boto3 library which provides lambda-specific services. But no other third party libraries are provided — not even
Boto used to provide a handy alias for the requests library. You could add the line
from botocore.vendored import requests directly into your lambda and easily use the requests library that way. But if you try that today, you’ll find yourself with this warning:
/var/runtime/botocore/vendored/requests/api.py:72: DeprecationWarning: You are using the get() function from 'botocore.vendored.requests'. This dependency was removed from Botocore and will be removed from Lambda after 2021/01/30. https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/. Install the requests package, 'import requests' directly, and use the requests.get() function instead.
You see, Amazon, in all of their finite wisdom, decided that this feature was too convenient, and have deprecated it. You are given the cryptic advice to ‘Install the requests package’, but a newcomer to Lambdas may not know how to do that.
The documentation provides instructions. But it doesn’t have any pictures, which is why I am writing this guide. Naturally, you can do this to install any external dependencies and use them in a Lambda.
Step 0) If you have previously just been working on your Lambda in the AWS console’s in-browser editor, you’ll need to create a deployment package. To begin doing that, simply create a new folder with a file called
lambda_function.py. This file MUST have that name and it must sit in the top-level of your folder. Copy-paste your lambda code into this file.
Step 1) Now that you have a deployment package, we must install
requests into it. Navigate to the folder in your command line. You should be at the same level as your
lambda_function.py. Now run
pip install requests -t ./. The ‘t’ or ‘target’ flag refers to where the package gets installed. In this case, we want to install it directly into your current working directory, where our
Step 2) Zip the contents of the folder. It is important that we do not want to zip the folder itself: rather we want to select everything inside the folder and zip it. You can use the
zip command line tool, or a GUI. On mac, this is as simple as selecting all contents of the folder and clicking “Compress X items”
Step 3) Upload the zip file. On your Lambda, in the configuration tab, there should be an ‘Actions’ button with an ‘Upload a .zip file’ option. Click that, and upload the zip file you created.
Step 4) Check your function code. It should look something like this. All of those folders that make up the requests library should be located directly within your top-level lambda folder, along with your lambda_function.py. They should not be nested within another folder.