Lambda layer for Python packages not working

0

I have the following Bash script to create and deploy a Lambda layer called 'pythonPackages' that is meant to provide Python libraries to my Lambda functions that are not already installed in the runtime environment.

#!/bin/bash

if [ -d "packages" ]; then
    echo "ERROR: Directory 'packages' already exists. Remove it before continuing. Exiting without making chagnes."
    exit 1
fi

rm layer_python_packages.zip -f

mkdir packages/python -p
cd packages/python

# pip3 install \
#     --platform manylinux2014_aarch64 \
#     --target=./lib/python3.12/site-packages \
#     --implementation cp \
#     --python-version 3.12 \
#     --only-binary=:all: --upgrade \
#     pandas

pip3 install \
    --platform manylinux2014_aarch64 \
    --target . \
    --python-version 3.12 \
    --only-binary=:all: --upgrade \
    pandas

rm -rf *.dist-info

cd ../..

zip -r9 layer_python_packages.zip packages

aws lambda publish-layer-version --layer-name pythonPackages --zip-file fileb://layer_python_packages.zip \
    --compatible-runtimes python3.12 --compatible-architectures arm64

rm packages -rf
rm layer_python_packages.zip

Then, I have a Lambda function with the line import pandas. That function prints the following to the CloudWatch logs:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'pandas'
Traceback (most recent call last):

I do have the latest version of the layer linked to my function: Enter image description here

What am I doing wrong in my script that is making it so that my function can't find the libraries.

I do know that AWS provides a layer that includes Pandas. I will add that temporarily, but need to get this figured out for future use.

Thank you!

gefragt vor einem Monat187 Aufrufe
1 Antwort
1
Akzeptierte Antwort

I determined the issue thanks to the post at https://repost.aws/questions/QURjW4CanTQjyJAHuWeeVCpA/lambda-layer-issue-python-module-not-found#ANJ5tm7dLLTaCVSYT_hdALrw.

The

cd ../..
zip -r9 layer_python_packages.zip packages

lines need to instead be

cd ../
zip -r9 lambda_layer_python_packages.zip python

to have the correct file structure when deployed in the Lambda runtime environment.

beantwortet vor einem Monat
profile picture
EXPERTE
überprüft vor einem Monat
profile picture
EXPERTE
Steve_M
überprüft vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen