Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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 and some mentioned that I have to change the file naming. But it didn't help. Here is how the python method was named:

imageHere 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 to try to print it gives me a syntax error. Sorry for the formatting. Any suggestions?

2 Answers

0 votes
by (44.4k points)

Requests are not a standard library in AWS lambda.

The ways to solve this issue:

1- Using the Botocore library stack, import it like this:

from botocore.vendored import requests

Libraries to import in Lambda

2- Create an AWS Lambda Deployment Package in Python with virtualenv

If you are interested to learn Python from Industry experts, you can sign up for this Python Certification Course by Intellipaat.

0 votes
by (340 points)

No module named requests error:

First create a directory for your lambda function:

mkdir lambda_function
cd lambda_function

Then install requests library into that directory

pip install requests -t

After that, add lambda_function to the directory

cp /path/to/your/lambda_function.py .

zip the contents of the directory

zip -r lambda_function.zip .


At last, Upload the zipped file to AWS Lambda

The error is unable to import ‘lambda_functions’: No module named requests tells you the requests library is not available in your lambda environment. By default, AWS Lambda has a limited set of libraries available; generally requests is not included.

Related questions

0 votes
3 answers
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...