Skip to content

Lambda with Docker (from ECR) is Failing to Install All Depedencies

0

I've created a Python script and requirements.txt in a Docker container. I've copied the requirements and installed them inside the docker image, then pushed the image to ECR and set up a Lambda using it.

However, the Lambda throws this error: "[ERROR] ModuleNotFoundError: No module named 'numpy._core' Traceback (most recent call last): File "/var/task/api.py", line 109, in lambda_handler model = load_model() File "/var/task/api.py", line 24, in load_model loaded_model = pickle.load(io.BytesIO(model))"

For reference, I am trying to use xgboost which is why I'm deploying via Docker as the size is large. This is what the requirements file looks like (I had to remove some versions because those were all causing issues within the Lambda previously, for some reason):

boto3==1.35.24 botocore==1.35.24 jmespath==1.0.1 numpy python-dateutil==2.9.0.post0 s3transfer==0.10.2 scipy six==1.16.0 urllib3 xgboost==2.1.1 scikit-learn

dockerfile: FROM public.ecr.aws/lambda/python:3.8

COPY requirements.txt ${LAMBDA_TASK_ROOT}

RUN pip install --no-cache-dir -r requirements.txt

COPY api.py ${LAMBDA_TASK_ROOT}

Command to run the Lambda function

CMD ["api.lambda_handler"]

asked 2 years ago884 views

3 Answers
1

You may want to look at using at least Python >=3.11

3.8 is very old now and you may find your issues go away with 3.11/3.12

You could also use Native Python in Lambda and just create a Layer to support your required libraries than building a container

EXPERT

answered 2 years ago

0

Hi

Double-check Dependencies

  • Ensure your requirements.txt includes not only numpy but also libraries XGBoost relies on, such as scipy and scikit-learn.

Reinstall NumPy

  • Sometimes, the installation might be incomplete. Add a line after installing requirements in your Dockerfile:

RUN pip install --no-cache-dir numpy

Lambda Configuration

  • Memory and CPU: Ensure your Lambda function has enough resources allocated. XGBoost can be resource-intensive.
  • Model Size: Large pickled models take up space. Consider compressing them or using a model server like SageMaker Neo for deployment.
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Hello.

  • The error you're encountering with AWS Lambda using Docker from ECR is primarily due to missing dependencies like numpy or xgboost that require compiled binaries to run properly in the Lambda environment.

  • To resolve this, ensure that your Dockerfile is built using Lambda-compatible libraries.

  • Since AWS Lambda has limitations when it comes to certain libraries, especially those that rely on compiled C extensions, using precompiled versions of these libraries within the Docker image can resolve the issue.

  • Additionally, testing the Docker container locally before pushing it to ECR will help ensure that all dependencies are properly installed.

  • It’s also essential to ensure that Lambda has enough memory and time to process large libraries or models like xgboost.

  • Alternatively, you can consider using Lambda Layers to separate large dependencies like numpy, allowing for a smaller Docker image and faster execution.

  • By following these approaches, you can ensure that your Lambda function runs smoothly with all dependencies correctly installed.

https://dev.to/linoespinoza/how-to-deploy-your-aws-lambda-based-in-containers-with-ecs-15ep

https://dev.to/aws-builders/how-to-package-and-deploy-a-lambda-function-as-a-container-image-3d1a

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.