You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to add numpy to any of my SAM applications but I keep running across a strange error. At first, I thought it was my projects dependencies but I pinned it down where it seems like it's entirely SAM.
The output I always get is this with sam local start-api:
Invalid lambda response received: Invalid API Gateway Response Keys: {'errorType', 'errorMessage', 'stackTrace', 'requestId'} in {'errorMessage':
"Unable to import module 'app': Error importing numpy: you should not try to import numpy from\n its source directory; please exit the numpy
source tree, and relaunch\n your python interpreter from there.", 'errorType': 'Runtime.ImportModuleError', 'requestId':
'387b7f65-d7fe-48b4-8f51-9d074be50daa', 'stackTrace': []}
Steps to reproduce:
I've made a minimally reproducible example called sam-numpy-example. The steps to create this are:
sam init
python3.12 runtime
Add a layer directory with a requirements.txt including only numpy
Add layer to template.yaml and reference in the Hello World function
Create venv and install layer requirements.txt
Run sam local start-api
CURL http://localhost:3000/hello
I have separate projects where I've tried to include numpy in a layer and the error is the exact same. Even libraries dependent on numpy output this error.
Observed result:
2024-08-31 11:19:45,507 | Lambda function 'HelloWorldFunction' is already running
2024-08-31 11:19:45,509 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
2024-08-31 11:19:45,511 | Getting lock for the key localhost-7271
2024-08-31 11:19:45,512 | Waiting to retrieve the lock (localhost-7271) to start invocation
START RequestId: db560bbc-9d9c-43f8-b309-ee7f7d60d83e Version: $LATEST
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
{"timestamp": "2024-08-31T16:19:45Z", "log_level": "ERROR", "errorMessage": "Unable to import module 'app': Error importing numpy: you should not try to import numpy from\n its source directory; please exit the numpy source tree, and relaunch\n your python interpreter from there.", "errorType": "Runtime.ImportModuleError", "requestId": "8afe2a9e-1a6c-48fd-bd78-1ae1045c3dd5", "stackTrace": []}
END RequestId: 8afe2a9e-1a6c-48fd-bd78-1ae1045c3dd5
REPORT RequestId: 8afe2a9e-1a6c-48fd-bd78-1ae1045c3dd5 Init Duration: 1.11 ms Duration: 290.11 ms Billed Duration: 291 ms Memory Size: 128 MBMax Memory Used: 128 MB
2024-08-31 11:19:45,827 | Unable to find Click Context for getting session_id.
2024-08-31 11:19:45,829 | Lambda returned empty body!
2024-08-31 11:19:45,829 | Invalid lambda response received: Invalid API Gateway Response Keys: {'errorMessage', 'stackTrace', 'requestId',
'errorType'} in {'errorMessage': "Unable to import module 'app': Error importing numpy: you should not try to import numpy from\n its source
directory; please exit the numpy source tree, and relaunch\n your python interpreter from there.", 'errorType': 'Runtime.ImportModuleError',
'requestId': '8afe2a9e-1a6c-48fd-bd78-1ae1045c3dd5', 'stackTrace': []}
2024-08-31 11:19:45,830 | Lambda execution failed ()
2024-08-31 11:19:45 127.0.0.1 - - [31/Aug/2024 11:19:45] "GET /hello HTTP/1.1" 502 -
Expected result:
I'd expect to see the results of print(numpy.__file__) that I added into the hello world function however since it's an import error, we don't even get there.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
Hi, thanks for bringing up the issue, I was able to reproduce it locally. That being said, based on the artifacts generated from the .aws_sam/build folder, It seems like the SAM template was generated properly and looks most likely to be an issue with NumPy itself interacting with lambda layers. Replicating this with with different libraries works properly and seems like a NumPy error since that is the error message specifically provided from NumPy itself. I would recommend opening a ticket in the Numpy repository.
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Description:
I'm trying to add numpy to any of my SAM applications but I keep running across a strange error. At first, I thought it was my projects dependencies but I pinned it down where it seems like it's entirely SAM.
The output I always get is this with
sam local start-api
:Steps to reproduce:
I've made a minimally reproducible example called sam-numpy-example. The steps to create this are:
requirements.txt
including only numpytemplate.yaml
and reference in theHello World
functionrequirements.txt
sam local start-api
http://localhost:3000/hello
I have separate projects where I've tried to include numpy in a layer and the error is the exact same. Even libraries dependent on numpy output this error.
Observed result:
Expected result:
I'd expect to see the results of
print(numpy.__file__)
that I added into the hello world function however since it's an import error, we don't even get there.Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
The text was updated successfully, but these errors were encountered: