14

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.

10
  • 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. Commented Nov 2, 2017 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. Commented Nov 2, 2017 at 14:43
  • All good. You just need to install the requests module into your project directory. Commented Nov 2, 2017 at 14:45
  • 1
    no, not a stupid question. I think this answer can explain it better than I can: stackoverflow.com/questions/40741282/… Commented Nov 2, 2017 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 Commented Nov 2, 2017 at 15:00

5 Answers 5

31

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

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

11 Comments

This is a much better option. Why were there no vote-ups?
It’s a brand new answer :)
Please note that an upcoming change to Botocore will be removing the vendored version of the requests library in Botocore: aws.amazon.com/de/blogs/developer/…
aws.amazon.com/blogs/compute/… New updates on deprecations
This should be the accepted answer. I tried using botocore and spent hours figuring out why my code wasn't working
|
12

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

4 Comments

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?
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
@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.
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.
1

For the lazy devs like me, urllib is included natively, doesn't require layers or zips or anything like that.

import json
import urllib.requests

def send_slack_message(prefix):
    slack_message = json.dumps({
        "text": f"New file uploaded to: `{prefix}`"
    }).encode('utf-8')
    req = urllib.request.Request(
        SLACK_WEBHOOK_URL, 
        data=slack_message, 
        headers={"Content-Type": "application/json"}
    )
    with urllib.request.urlopen(req) as response:
        if response.status != 200:
            print(f"Failed to send Slack message: {response.read().decode('utf-8')}")
    return

Comments

0

pip install custom package to /tmp/ and add to path

subprocess.call('pip install requests -t /tmp/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) sys.path.insert(1, '/tmp/') import requests

2 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

You need to install requests module. Type :

pip install requests 

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

Comments

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.