Error while running python code in aws lambda function:


While running aws lambda function for a lex document based chatbot code i am getting error(i have imported a layer for langchain module).


Response { "errorMessage": "Unable to import module 'lambda_function': \n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy C-extensions failed. This error can happen for\nmany reasons, often due to issues with your setup or how NumPy was\ninstalled.\n\nWe have compiled some common reasons and troubleshooting tips at:\n\n\n\nPlease note and check the following:\n\n * The Python version is: Python3.12 from "/var/lang/bin/python3.12"\n * The NumPy version is: "1.24.4"\n\nand make sure that they are the versions you expect.\nPlease carefully study the documentation linked above for further help.\n\nOriginal error was: No module named 'numpy.core._multiarray_umath'\n", "errorType": "Runtime.ImportModuleError", "requestId": "9dcde579-1c40-4020-9634-500fb4f173de", "stackTrace": [] }

2 Answers

Create an Amazon Linux2 environment using the Dockerfile below.

FROM --platform=linux/x86_64 amazonlinux:2


RUN yum update -y && yum install -y tar gzip make gcc epel-release openssl11 openssl11-devel bzip2-devel libffi-devel \
  && curl${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz | tar xz \
  && export CFLAGS=$(pkg-config --cflags openssl11) && export LDFLAGS=$(pkg-config --libs openssl11) \
  && cd Python-${PYTHON_VERSION} && ./configure && make && make install \
  && cd - && rm -rf Python-${PYTHON_VERSION}


RUN yum install -y zip \
  && mkdir /python \
  && chmod +x /

ENTRYPOINT ["/"] looks like this:

#!/bin/bash -eu


pip3 install -t ${SRC} $@
rm -f ${DIST}
zip -q -r ${DIST} ${SRC}

Please build with the following command.

docker build . -t bundle-pip-modules-for-aws-lambda-layers:python3.12

Finally, create a layer using the following command.
Please write your required modules in requirements.txt.
Once is completed, try registering it as a Lambda layer.

docker run --rm \
    -v $(pwd)/requirements.txt:/requirements.txt \
    -v $(pwd):/dist \
    bundle-pip-modules-for-aws-lambda-layers:python3.12 \
    -r requirements.txt
profile picture
answered 2 months ago
profile picture
reviewed 2 months ago
  • I did something similar which 1/2 manual steps but Im going to borrow this @Riku :-). Thanks



Does the Python version of the environment where you created the langchain module layer match the Python version used by Lambda?
If they do not match, try creating a layer by matching the Python version of the layer creation environment and Lambda.

profile picture
answered 2 months ago
  • Both python version are same 3.12

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.

Guidelines for Answering Questions