How to make an HTTP Request in AWS Lambda using Python
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
|
|
Run this command on your local machine, then zip your working directory and upload to AWS.
Make the HTTP request
Option 2 – Use urllib3
import
If you don’t want to create a custom lambda layer, then you can import the urllib3
library directly.
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
|
|
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.