Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Numpy In Lambda Layer #7429

Closed
Jdldeveloper opened this issue Aug 31, 2024 · 3 comments
Closed

Bug: Numpy In Lambda Layer #7429

Jdldeveloper opened this issue Aug 31, 2024 · 3 comments
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days

Comments

@Jdldeveloper
Copy link

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:

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:

  1. sam init
  2. python3.12 runtime
  3. Add a layer directory with a requirements.txt including only numpy
  4. Add layer to template.yaml and reference in the Hello World function
  5. Create venv and install layer requirements.txt
  6. Run sam local start-api
  7. 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)

{
  "version": "1.123.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-14.3-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "25.0.3",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}
@Jdldeveloper Jdldeveloper added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Aug 31, 2024
@jysheng123
Copy link
Contributor

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.

@jysheng123 jysheng123 added blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Sep 3, 2024
@Jdldeveloper
Copy link
Author

Thanks @jysheng123, I'll follow up with them then. I appreciate your insight!

Copy link
Contributor

github-actions bot commented Sep 4, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days
Projects
None yet
Development

No branches or pull requests

3 participants
@jysheng123 @Jdldeveloper and others