Skip to content

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

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://ao.ms")
Code language: Python (python)

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://ao.ms') #r.data #r.status
Code language: Python (python)

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
Code language: Python (python)

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://ao.ms")
Code language: Python (python)

See also  How to Create a MySQL Table in Python
Tags:
Subscribe
Notify of
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Claes
Claes
2 years ago

Please don’t import vendored things in Lambda code in general, and certainly not requests from botocore .vendored, see https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/

qtp
qtp
8 months ago
Reply to  Claes

Then how do you use it? Don’t post half solutions dude, if you are going to tell us not to use Requests from vendored, then where do you get it from?

3
0
Would love your thoughts, please comment.x
()
x