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!

質問済み 1ヶ月前185ビュー
1回答
1
承認された回答

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.

回答済み 1ヶ月前
profile picture
エキスパート
レビュー済み 1ヶ月前
profile picture
エキスパート
Steve_M
レビュー済み 1ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ