-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?

10
  • 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 Commented Apr 15, 2022 at 17:26
  • if you are using the console to upload your .zip file, you can refer to docs.aws.amazon.com/lambda/latest/dg/python-package.html Commented Apr 15, 2022 at 17:29
  • @rcshon I'm using serverless Commented Apr 15, 2022 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. Commented Apr 15, 2022 at 17:34
  • you can use the serverless-python-requirements plugin to help you. (You need docker though) serverless.com/plugins/serverless-python-requirements Commented Apr 15, 2022 at 17:39

1 Answer 1

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.

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

3 Comments

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"
Well, the import now succeeded and this is a different issue. I would suggest a new question with your updated code.
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.

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.