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.
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'"}


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