I need to do a rest-call within a python script, that runs once per day. I can\'t pack the \"requests\" package into my python-package using the AWS Lambdas. I get the error
EDIT: On Oct-21-2019 Botocore removed the vendored version of requests: https://github.com/boto/botocore/pull/1829.
EDIT 2: (March 10, 2020): The deprecation date for the Lambda service to bundle the requests module in the AWS SDK is now January 30, 2021. https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/
To use requests module, you can simply import requests
from botocore.vendored
. For example:
from botocore.vendored import requests
def lambda_handler(event, context):
response = requests.get("https://httpbin.org/get", timeout=10)
print(response.json())
you can see this gist to know more modules that can be imported directly in AWS lambda.
With this command download the folder package
pip install requests -t .
Run this command on your local machine, then zip your working directory, then upload to aws.
If you're working with Python on AWS Lambda, and need to use requests, you better use urllib3, it is currently supported on AWS Lambda and you can import it directly, check the example on urllib3 site.
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://httpbin.org/robots.txt')
r.data
# b'User-agent: *\nDisallow: /deny\n'
r.status
# 200
I finally solved the problem: The structure in my zip file was broken. It is important that the python script and the packed dependencies (as folders) are in the root of the zip file. This solved my problem.
It's a bit depressing if you find such easy errors after hours of try and failure.
I believe you have lambda_function.py
on the Lambda console. You need to first create the Lambda function deployment package, and then use the console to upload the package.
project-dir
on your system (locally)lambda_function.py
in project-dir
, copy the content of lambda_function.py
from lambda console and paste it in project-dir/lambda_function.py
pip install requests -t /path/to/project-dir
project-dir
directory, which is your deployment package (Zip the directory content, not the directory)Go to the Lambda console, select upload zip file in code entry type and upload your deployment package. Import requests should work without any error.