I've got a Python script for an AWS Lambda function that does HTTP POST requests to another endpoint. Since Python's urllib2.request
, https://docs.python.org/2/library/urllib2.html, can only handle data in the standard application/x-www-form-urlencoded
format and I want to post JSON data, I used the Requests library, https://pypi.org/project/requests/2.7.0/.
That Requests library wasn't available at AWS Lambda in the Python runtime environment, so had to be imported via from botocore.vendored import requests
. So far, so good.
Today, I get a deprecation warning on that:
DeprecationWarning: You are using the post() function from 'botocore.vendored.requests'.
This is not a public API in botocore and will be removed in the future.
Additionally, this version of requests is out of date. We recommend you install the
requests package, 'import requests' directly, and use the requests.post() function instead.
This was mentioned in this blog post from AWS too: https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/.
Unfortunately, changing from botocore.vendored import requests
into import requests
results in the following error:
No module named 'requests'
Why is requests
not available for the Python runtime at AWS Lambda? And how can I use / import it?
Because the boto3 module is already available in the AWS Lambda Python runtimes, don't bother including boto3 and its dependency botocore in your Lambda deployment zip file.
Update (April 26, 2022): The version of the AWS SDK included in the AWS Lambda runtimes for Python 2.7, Python 3.6 and Python 3.7 will continue to include the 'requests' module in Botocore.
Overview. As we enter our third year of Python 2.7 reaching end-of-life, Requests has decided it's time to start deprecating our support. While we have yet to confirm a date, we want to provide early notice that this is coming at some point in 2022.
I succeeded sending HTTP POST requests using the urllib3
library, which is available at AWS Lambda without the requirements for additional installation instructions.
import urllib3
http = urllib3.PoolManager()
response = http.request('POST',
url,
body = json.dumps(some_data_structure),
headers = {'Content-Type': 'application/json'},
retries = False)
Answer 2020-06-18
I found a nice and easy way to use requests
inside AWS Lambda functions!
Open this link and find the region that your function is using:
https://github.com/keithrozario/Klayers/tree/master/deployments/python3.8/arns
Open the .csv
related to your region and search for the requests
row.
This is the ARN
related to requests
library:
arn:aws:lambda:us-east-1:770693421928:layer:Klayers-python38-requests:6
So now in your lambda function, add a layer using the ARN found.
Obs.: make sure your Python lambda function runtime is python3.8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With