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/li
If you are using serverless framework
Specify the plugin in serverless.yml
plugins:
- serverless-python-requirements
At the directory root create file requirements.txt
requirements.txt
requests==2.22.0
This will install the requests and packages mentioned.
Check out the instructions here: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies
All you need to do is download the requests module locally, then include it in your Lambda function deployment package (ZIP archive).
Example (if all your Lambda function consisted of was a single Python module + requests module):
$ pip install --target ./package requests
$ cd package
$ zip -r9 ${OLDPWD}/function.zip .
$ cd $OLDPWD
$ zip -g function.zip lambda_function.py
$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip
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.
requests is NOT part of core python. See https://docs.aws.amazon.com/en_pv/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html about packaging a Lambda having external dependencies (in your case the requests library)
pip install requests
and then
import requests
to use.
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)