I am trying to add the numpy package to my lambda function, but I can't import it.
I have followed several tutorials, but they all have the same problem. On my last attempt, I executed the following step by step:
- I created a lambda function in AWS and tested it
- Installed the numpy package on my local machine and zipped it
- Created lambda layer and loaded the numpy package
- Fixed the layer to lambda and tested
When I run my lambda function without importing numpy, it works perfectly, however when I import it I get this error:
{
"errorMessage": "Unable to import module 'lambda_function': No module named 'numpy'",
"errorType": "Runtime.ImportModuleError",
"requestId": "0153834a-6b28-44d1-889f-3e2e3ead9c4a",
"stackTrace": []
}
A very common error on the forum, however everything is fine with lambda_function
, because as I said it works fine if I'm not importing any module.
- lambda_function.py
import json
import numpy
def lambda_handler(event, context):
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
I would like to learn how to use Layers inside Lambda.
CodePudding user response:
There are at least two ways to add NumPy to your AWS Lambda as layer:
- Add an AWS (Managed) Layer:
From your Lambda console, chooseAWS Layers
andAWSSDKPandas-Python39
(Choose a layer accordingly for your Python version). Since Pandas is built on top of NumPy, you should be able to use NumPy once you add this Pandas layer. - Add a custom layer:
Create a virtual environment on your local machine, install NumPy, create a zip file, and upload to AWS as a layer. Ensure the version of Python in your local Virtual env is the same as the Lambda. Step by step instruction here. This is a general approach which you can use to add any external Python libraries as Lambda layers.