The requests
module for Python is very useful in helping simplify HTTP/s requests from Python, but how would you use it in an AWS Lambda script?
Option 1 – Use requests
import
The requests
library is very popular among Python enthusiasts.
You will need to create a custom lambda layer and include requests
This will allow you to use import requests
in your code.
Download the folder package
pip install requests -t .
Run this command on your local machine, then zip your working directory and upload to AWS.
Make the HTTP request
import requests
response = requests.get("https://andrewodendaal.com")
Option 2 – Use urllib3
import
If you don’t want to create a custom lambda layer, then you can import the urllib3
library directly.
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://andrewodendaal.com')
#r.data
#r.status
Option 3 – Old way with botocore.vendored
While it is not immediately possible to just do a import requests
and start using the module, it is possible to import it from the botocore.vendored
top-level package.
Python on Lambda exposes a module for common packages called botocore
that you can call in any Lambda script.
Using the requests library in Lambda
from botocore.vendored import requests
Once you have imported the requests library from botocore.vendored
, you will be able to make use of all the functionality you are familiar with.
Make the HTTP request
from botocore.vendored import requests
response = requests.get("https://andrewodendaal.com")