-2

I need to call external API and print the output as JSON. I'm using AWS Lambda function to execute this code sample in serverless environment. Following is the code and its return error as

Error message "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'", "errorType": "Runtime.ImportModuleError", "requestId": "399d0ea2-6713-4714-90ee-1b8e9924af13", "stackTrace": []

Code Sample

import json
import boto3
import requests
from botocore.vendored import requests


BASE_URL = 'https://fakestoreapi.com'

query_params = {
    "limit": 1
}

response = requests.get(f"{BASE_URL}/products", params=query_params)
print(response.json())

Some of the post suggested solution as use Python 3.7. I'm using 3.9. Is this 'requests' module not available in Python 3.9? How to use this in Python 3.9? Some of the post suggested to "pip3 install -t python requests" . I'm using Lambda function inside AWS. How to execute this command if require?

DasGuin
  • 11
  • 1
  • 1
  • 8
  • how are you deploying your lambda function onto AWS? using the `serverless` framework? `aws-cdk`? It'd be better if you tagged your question to the framework you are using – rcshon Apr 15 '22 at 17:26
  • if you are using the console to upload your .zip file, you can refer to https://docs.aws.amazon.com/lambda/latest/dg/python-package.html – rcshon Apr 15 '22 at 17:29
  • @rcshon I'm using serverless – DasGuin Apr 15 '22 at 17:30
  • @rcshon I'm in the initial stage of AWS Lambda function execution. I just tried to call a api and print those value. Next is to do something use those Json output. Any other good way to move forward please let me know. – DasGuin Apr 15 '22 at 17:34
  • you can use the serverless-python-requirements plugin to help you. (You need docker though) https://www.serverless.com/plugins/serverless-python-requirements – rcshon Apr 15 '22 at 17:39
  • I recall doing this before but its not fresh in my mind anymore. You should probably tag the serverless community for better help – rcshon Apr 15 '22 at 17:40
  • Does this answer your question? [AWS python lambda function:No module named requests](https://stackoverflow.com/questions/46991117/aws-python-lambda-functionno-module-named-requests) – Paolo Apr 15 '22 at 18:12
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Apr 15 '22 at 19:57
  • @Paolo 1st I Import the urllib3 and change the code as resp = urllib3.request("GET", "httpbin.org/ip") print(resp.json()) but still return error as "errorMessage": "'module' object is not callable" 2nd way, I installed the pip install requests -t , manually Zip the content , add as layer and tried to import requests. Still getting previous error as "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'", – DasGuin Apr 16 '22 at 17:11
  • @dasitha did you read my answer in the link I gave you? – Paolo Apr 16 '22 at 21:08

1 Answers1

2

There are several options that you can check at https://aws.amazon.com/blogs/compute/upcoming-changes-to-the-python-sdk-in-aws-lambda/.

Using a Lambda layer would be your best choice.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I Import the urllib3 and change the code as resp = urllib3.request("GET", "https://httpbin.org/ip") print(resp.json()) but still return error as "errorMessage": "'module' object is not callable" – DasGuin Apr 16 '22 at 16:27
  • Well, the import now succeeded and this is a different issue. I would suggest a new question with your updated code. – Diego Torres Milano Apr 16 '22 at 21:31
  • Issue fixed. Install the request module and kept the file "\python\lib\python3.9\site-packages" path. Then ZIP the whole content. Then import as layer. I don't have idea reason behind keep the install package "\python\lib\python3.9\site-packages" file path as it is. Please let me know any idea if you have regarding this. – DasGuin Apr 17 '22 at 03:54