11

I want to send a post request to an external API (https://example.com/api/jobs/test) every hour.

The Lambda Function that I used is as follows:

Handler: index.lambda_handler
python: 3.6

index.py

import requests
def lambda_handler(event, context):
  url="https://example.com/api/jobs/test"
  response = requests.post(url)
  print(response.text) #TEXT/HTML
  print(response.status_code, response.reason) #HTTP

Test Event:

 {
 "url": "https://example.com/api/jobs/test"
}

Error:

 START RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Version: $LATEST
 Unable to import module 'index': No module named 'requests'

 END RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d
 REPORT RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Duration: 0.65 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 21 MB  

Any help would be appreciated.

Matrix
  • 2,399
  • 5
  • 28
  • 53
  • The problem isn't with your request, but with the module you're trying to import. Make sure it's installed. I'm not sure what that "index.php" is about, you are working with python, not php. – FelisPhasma Nov 02 '17 at 14:37
  • @FelisPhasma Thanks for the comment. Yes, but where the module should be installed? and sorry I meant index.py. it was typo mistake. – Matrix Nov 02 '17 at 14:43
  • All good. You just need to install the requests module into your project directory. – FelisPhasma Nov 02 '17 at 14:45
  • I'm not sure if I'm following... Project Directory is it somewhere on AWS? (I'm new with Lambda) – Matrix Nov 02 '17 at 14:47
  • Your `index.py` file is located somewhere on your aws server, and you should be able to install into the same directory as where that file is stored. – FelisPhasma Nov 02 '17 at 14:48
  • @FelisPhasma Sorry it might be stupid question... but I just when to AWS Lambda and created a function and it's not in VPC. I didn't use any EC2 server. Am I missing some setup? – Matrix Nov 02 '17 at 14:53
  • 1
    no, not a stupid question. I think this answer can explain it better than I can: https://stackoverflow.com/questions/40741282/cannot-use-requests-module-on-aws-lambda – FelisPhasma Nov 02 '17 at 14:59
  • 3
    this command should install requests for you, just make sure the root folder is specified properly: `pip install requests -t PATH_TO_ROOT_FOLDER` – FelisPhasma Nov 02 '17 at 15:00
  • http.client is supported natively. It's not quite as friendly as requests, but it is faster and will usually get the job done. – John Heyer Nov 08 '20 at 16:42

3 Answers3

29

Vendored requests are now removed from botocore.

Consider packaging your Lambda code with requirements.txt using CloudFormation package or SAM CLI packaging functionality.

My older answer from before vendored requests deprecation: You may be able to leverage requests module from the boto library without having to install or package your function.

Consider this import: import botocore.vendored.requests as requests

adamkonrad
  • 6,794
  • 1
  • 34
  • 41
  • 1
    This is a much better option. Why were there no vote-ups? – MGhostSoft May 25 '19 at 22:38
  • It’s a brand new answer :) – adamkonrad May 26 '19 at 03:08
  • This method of importing the requests is depreciated is it not? – cvb Sep 18 '19 at 20:53
  • There is just deprecation warning, but that by itself doesn't mean much. – adamkonrad Sep 19 '19 at 19:00
  • 3
    Please note that an upcoming change to Botocore will be removing the vendored version of the requests library in Botocore: https://aws.amazon.com/de/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/ – Sma Ma Oct 29 '19 at 10:00
  • i use this 'pip install requests boto3 mock --upgrade --user' still getting error in REST end point call. Jenkins build was success, and when i invoke the REST my response in internal server error and when i see the logs, unable to import module requests. – parrotjack Nov 18 '19 at 16:45
  • @parrotjack maybe the requests vendored module is already gone from botocore :( – adamkonrad Nov 18 '19 at 17:02
  • @kixorz yeah i read the aws article, the mentioned to install requests, and installed it using the commands but still occurs. – parrotjack Nov 18 '19 at 17:04
  • 1
    https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/ New updates on deprecations – Parvez Kazi Feb 01 '21 at 09:45
  • It's better to move off botocore.vendored.requests ASAP anyway. – adamkonrad Feb 01 '21 at 14:17
  • 1
    This should be the accepted answer. I tried using botocore and spent hours figuring out why my code wasn't working – EternalObserver Jun 27 '23 at 11:57
10

You need to install requests module to your project directory and create a lambda deployment package. See this link for details.

In short, you need to create your index.py file on you development system (PC or mac), install Python & pip on that system; them follow the steps in the doc. To create lambda, choose the 'Upload zip' option instead of the 'Edit inline' one

agent420
  • 3,291
  • 20
  • 27
  • Thanks for the reply. But I'm wondering if I need to setup an EC2 server for installing the module? Because at the moment I just went to AWS Lambda and created a function. Is there something more that should be set? – Matrix Nov 02 '17 at 14:59
  • 1
    No. You need to do these things (install python & pip, install requests using pip, zip the bundle etc.) on a system. Doesn't need to be EC2; can be your Windows PC or mac – agent420 Nov 02 '17 at 15:00
  • 1
    @Matrix it would only need to be on an EC2 server (one running Amazon Linux specifically) if you needed to install binary dependencies. You don't need to do that for the `requests` module. – Mark B Nov 02 '17 at 15:03
  • Thank you, I've created a directory(project_dir) and installed "requests" using pip and the created post.py file in the dir after that zipped it via cd project_dir; zip -r ../post_dir.zip * now I got this error: This function contains external libraries. Uploading a new file will override these libraries. – Matrix Nov 02 '17 at 15:20
0

You need to install requests module. Type :

pip install requests 

into your terminal, or after activating virtual environment if you are using one.

Antoine
  • 1,393
  • 4
  • 20
  • 26