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!

posta un mese fa185 visualizzazioni
1 Risposta
1
Risposta accettata

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.

con risposta un mese fa
profile picture
ESPERTO
verificato un mese fa
profile picture
ESPERTO
Steve_M
verificato un mese fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande