Error while running python code in aws lambda function:

0

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

Error:

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 https://numpy.org/devdocs/user/troubleshooting-importerror.html\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
1

Create an Amazon Linux2 environment using the Dockerfile below.

FROM --platform=linux/x86_64 amazonlinux:2

ARG PYTHON_VERSION=3.12.1

RUN yum update -y && yum install -y tar gzip make gcc epel-release openssl11 openssl11-devel bzip2-devel libffi-devel \
  && curl https://www.python.org/ftp/python/${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}

ADD entrypoint.sh /

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

ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh looks like this:

#!/bin/bash -eu

SRC=/python
DIST=/dist/layer.zip

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 layer.zip 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
EXPERT
answered 4 months ago
profile picture
EXPERT
reviewed 4 months ago
  • I did something similar which 1/2 manual steps but Im going to borrow this @Riku :-). Thanks

0

Hello.

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
EXPERT
answered 4 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