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!

asked 24 days ago162 views
1 Answer
1
Accepted Answer

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.

answered 24 days ago
profile picture
EXPERT
reviewed 23 days ago
profile picture
EXPERT
Steve_M
reviewed 23 days 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.

Guidelines for Answering Questions