49

I am fairly new to AWS and I am having some issues. Here is my code:

from __future__ import print_function
from urllib2 import Request, urlopen, URLError
import requests
import boto3
import json

def lambda_handler(event, context):
    url = "https://globalcurrencies.xignite.com/xGlobalCurrencies.json/GetHistoricalRatesRange?Symbol=BTCUSD&PriceType=Mid&StartDate=01/01/2017&EndDate=10/27/2017&PeriodType=Daily&FixingTime=22:00&_token=some_token_xyz"
    response = requests.get(url).json()
    # print json.dumps(response, indent=4) # gives a syntax error
    return response

Name of the file is lambda_function.py; I have checked similar problems on stackoverflow and some mentioned that I have to change the file naming. But it didn't help. Here is how python method was named:
enter image description here Here is the error I am getting: START RequestId: cf24e9be-bbef-11e7-97b4-d9b586307f3e Version: $LATEST Unable to import module 'lambda_function': No module named requests And when try to print it gives me a syntax error. Sorry for the formatting. Any suggestions?

2
  • 4
    requests isn't in the standard library. You'll need to look into how to provide dependencies on that platform; it's common to use a requirements.txt file to specify them, for example. Have a look at e.g. docs.aws.amazon.com/lambda/latest/dg/… Commented Oct 28, 2017 at 15:05
  • @jonrsharpe I have virtual environment setup on my machine. Should I just upload everything on do pip install -r requirements.txt ? Commented Oct 28, 2017 at 15:11

10 Answers 10

51

requests is not a standard library in AWS lambda.

So two ways to solve this:

1- Import it from the Botocore libraries stack as: (Option to be deprecated after the answer was given)

from botocore.vendored import requests

Here there is a list of all the available libraries to import in lambda

2- Create a deployment package with virtualenv.

Sign up to request clarification or add additional context in comments.

3 Comments

Option number 1 shouts this message: /var/runtime/botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the get() 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.get() function instead.
I just added the heads up. Thanks @ilvidel for the heads up!
According to an updated version of the warning: "This dependency was removed from Botocore and will be removed from Lambda after 2020/03/31." Stick with option 2.
35

N.B requests is already included in lambda functions with runtime Python 3.7:

In fact:

from pkg_resources import working_set

def lambda_handler(event, context):
    for p in list(working_set):
        print(p.project_name, p.version)

urllib3 1.26.6 six 1.16.0 s3transfer 0.6.0 requests 2.26.0 python-dateutil 2.8.2 jmespath 1.0.1 idna 2.10 charset-normalizer 2.0.12 chardet 4.0.0 certifi 2020.11.8 botocore 1.29.90 boto3 1.26.90 setuptools 47.1.0 pip 22.0.4

For all other runtimes, see below.


If you want to include libraries which are not part of Python's standard library, such as requests, you can use lambda's layers.

1. Create the zip

This is a zip which contains all the libraries you want the lambda function to use. First, create a folder called python:

$ mkdir python
$ cd python

then, install the Python libraries you need in there. You can do this either with a single library:

$ pip install --target . requests

or with a list of libraries (requirements.txt)

$ pip install --target . -r requirements.txt

finally, zip up everything.

... in Bash:

$ zip -r dependencies.zip ../python

... in Powershell:

$ Compress-Archive -Path . -DestinationPath dependencies.zip

2. Create a layer

You can do this either in the AWS console or in the CLI. Follow these instructions.


3. Add the layer to the lambda function

You can do this either with the Add a layer option in the lambda page, or in the CLI. Follow these instructions.

3 Comments

Great answer! In my case, step number 2 threw the error ERROR: Can not combine '--user' and '--target'. As it turns out, this happens if Python is installed as an app from the store and in some other cases. The issue is easily solved by adding a --no-user at the end of the command: pip install --target . requests --no-user. Source
If you're using the AWS SAM Workflow, create the directory with dependencies described in part 1 of answer, then build the layer by including the Metadata property in the Layer resource in the SAM Template: docs.aws.amazon.com/serverless-application-model/latest/… . Then attach the layer to the function's Layers property: docs.aws.amazon.com/serverless-application-model/latest/… . There's no need to zip and upload using CLI.
Tested for lambda runtime python 3.9, pip install --target . requests==2.29.0 would work
14

This is because it is missing the requests library when running in the lambda - its likely that its installed globally on your local machine. If you run:
pip install requests -t .
in the same directory as your source code it will install the requests package in that directory then you can upload it to lambda along with your lambda_function.py. You may need to do the same for boto3 and json:
pip install boto3 -t .
pip install json -t .

2 Comments

I am using virtual environment in my machine. How can I upload and retrieve the dependancy from requirements.txt?
boto3 and json are both already included in AWS lambda, see gist.github.com/gene1wood/4a052f39490fae00e0c3
8

You want to use Layers. You download a python module e.g pip install requests -t . then simply put all the module folders into a folder called python and zip it and upload it in the lambda layers section of the aws console. Add it to your lambda function and it will work. The dir structure is very important so for requests when you unzip the folder it should be RequestLayer/python/requests/"requests and all the other folders that are part of that package". This guy gives a good step by step.

2 Comments

What should be the folder structure? What do you mean by "RequestLayer"? I tries your version and a bunch of others (with or without "RequesLayer", with or without "requests") but no luck.
@LoMaPh I tried without RequestLayer and it works. So after installing the package using pip, place all content within a folder called python, zip that and add that as a Layer.
7

Use urllib3 instead

While yes, requests is not a standard library in AWS lambda, there are alternatives within the standard library that will allow you to avoid Lambda layers.

If you check the list of libraries that Nilo_DS linked (https://gist.github.com/gene1wood/4a052f39490fae00e0c3), you'll find that urllib3 is available.

import requests

requests.post(<url>, data=json.dumps(<message>))

can be rewritten as

import urllib3

http = urllib3.PoolManager()
http.request('POST', '<url>',
             headers={'Content-Type': 'application/json'},
             body=json.dumps(<message>))

and similar changes for a get request.

Comments

3

For Python 3.12 lambda runtime use the following import for requests module

from pip._vendor import requests

Comments

2

For future readers using Python: If you created your Lambda app directly in Cloud9, you would notice that there is a virtual environment for your app. To install requests (or any other package for that matter), do the following:

  1. Right-click on the folder for your app
  2. Select "Open terminal here"
  3. In the terminal type source venv/bin/activate to activate your virtual environment
  4. Type pip3 install requests to install requests

That is it. You can now use requests with import requests as per normal.

1 Comment

I don't see any such option "Open terminal here"
1

'requests' module is not in your 'zip' file that your are trying to install. you have to put all modules into the zip file by 'pip install module_name(such as requests) -t .'

Comments

0

POST: 20Feb24

You can use the http.client like below.

enter image description here

This solves the purpose and you don't have to install anything else for http.client.

For scheduler cronjob use https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html

and https://stackoverflow.com/a/68146073/11207658

PS: cronjob uses the UTC timezone. so apply it accordingly

Comments

-9

you have to name your lambda this way in python code:

def lambda_function(event, context):

and in lambda console handler:

main.lambda_function

for your request error, must have the folder of that module on your .zip before you upload to lambda.

2 Comments

No, there's another error: Unable to import module 'main': No module named requests
do you have the .zip folder with the request library inside? before upload to lambda? thats the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.