44

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?

Mark B
  • 183,023
  • 24
  • 297
  • 295
Zafar
  • 1,111
  • 2
  • 11
  • 23
  • 3
    `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. http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html – jonrsharpe Oct 28 '17 at 15:05
  • @jonrsharpe I have virtual environment setup on my machine. Should I just upload everything on do `pip install -r requirements.txt` ? – Zafar Oct 28 '17 at 15:11

8 Answers8

48

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.

Nilo_DS
  • 889
  • 7
  • 14
  • 4
    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. – ilvidel Oct 28 '19 at 10:09
  • I just added the heads up. Thanks @ilvidel for the heads up! – Nilo_DS Nov 19 '19 at 22:11
  • 8
    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. – McGlothlin Mar 20 '20 at 17:46
27

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.

Paolo
  • 21,270
  • 6
  • 38
  • 69
  • 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](https://stackoverflow.com/a/67259534/13018268) – Riccardo Nov 22 '21 at 16:21
  • 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: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-layers.html . Then attach the layer to the function's `Layers` property: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-layers . There's no need to zip and upload using CLI. – Philosopher-Programmer Dec 04 '22 at 05:04
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 .

Mark Kell
  • 165
  • 3
  • 1
    I am using virtual environment in my machine. How can I upload and retrieve the dependancy from requirements.txt? – Zafar Nov 15 '17 at 16:56
  • 1
    boto3 and json are both already included in AWS lambda, see https://gist.github.com/gene1wood/4a052f39490fae00e0c3 – nnsense May 22 '20 at 00:37
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.

OmegaOdie
  • 373
  • 2
  • 8
  • 1
    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 Mar 08 '20 at 00:07
  • 1
    @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. – ninsonTan Oct 01 '21 at 21:00
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

'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 .'

vedat
  • 1,193
  • 9
  • 10
0

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.

dwp7kp
  • 1
-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.