1
votes

I have recently started using AWS SAM to build API and AWS Lambda. The code is working fine in AWS but I'm having some difficulty setting the local testing and debugging (which is one of the main reasons why I wanted to use SAM in the first place).

The lambda_handler function is very straightforward. It looks similar to below. I am simply running my function like some_function_here which is using 'requests' module in one of the dependent functions. In my requirements.txt in the SAM project, I have 'requests' alongside some other dependencies. This seems to do it's job since I can see the request being installed in AWS Lambda (screenshot below).

The trouble surprisingly is when I'm running SAM locally (on VsCode, I hit F5). When I run sam local invoke (since I don't need an event), I get an error sayng 'no module named requests' when it should've downloaded the requets based on the 'requirements.txt' as it's doing on the AWS Cloud. Any suggestion is appreciated.

AWS Lambda Folder Structure

Lambda Handler Code

def lambda_handler(event, context):
    try:
        some_function_here()
        return {
            "statusCode": 200,
            "body": json.dumps({
                "message": "Job ran successfully."
            }),
        }
    except Exception as e:
        return {
            "statusCode": 500,
            "body": json.dumps({
                "message": "Something went wrong!"
            }),
        }

Error

(base) anojshrestha% sam local invoke
Invoking app.lambda_handler (python3.8)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-python3.8:rapid-1.7.0.

Mounting /... as /var/task:ro,delegated inside runtime container
START RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'requests'
END RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d
REPORT RequestId: 17ce7573-87ed-1ce0-5584-31a7f3f0823d  Init Duration: 188.03 ms       Duration: 3.89 ms        Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 24 MB

{"errorType":"Runtime.ImportModuleError","errorMessage":"Unable to import module 'app': No module named 'requests'"}
1
Is the module "requests" installed in the Python environment you use in VSCode? Please enter "pip show requests" in the terminal to check its installation. - Jill Cheng
Hi @JillCheng, Yes I do have the library installed. Also should the library not be installed as part of starting SAM (like it does on AWS)? xx% pip show requests Name: requests Version: 2.22.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: [email protected] License: Apache 2.0 Location: /opt/anaconda3/lib/python3.7/site-packages Requires: urllib3, chardet, certifi, idna Required-by: Sphinx, pymsteams, conda, conda-build, anaconda-project, anaconda-client - Anoj Shrestha
Sorry for delayed response @JillCheng. It turns out that the issue is not because of the python version. And simply doing a sam build (hitting F5 on VSCode) prompts for the underlying docker to reinstall requirements. When you just do 'sam local invoke', the requirements dont seem to be taken into account. Not sure why it didn't work before but forcing sam to build now fixed the issue. - Anoj Shrestha
Hi @JillCheng, The modules install works slightly different when it comes AWS SAM. AWS SAM builds a docker container in the background, installs requirements into the docker container and runs your function using the same/similar AWS lambda runtime. My issue was that the requirments install in that container was not working. - Anoj Shrestha

1 Answers

0
votes

I noticed that the Python interpreter you are using is "python3.8", but the module "requests" is installed in "anaconda3/lib/python3.7". It is recommended that you switch the Python interpreter in VSCode to "Python3.7(conda)". (Please click the Python interpreter in the lower left corner of VSCode to switch it.)

We can use the command "python --version" to check the Python interpreter version currently used by the VSCode terminal:

enter image description here

enter image description here

Reference: Environment in VSCode.