LambdaのDockerコンテナでPIP実行してZIP化。それをAWS Lambda Layerとして発行するスクリプトです。
- 要Dokerインストール(lambci/lambdaイメージ内でPIPします)
- 事前にAWS Configureしてください。
- 以下のソースコードをファイル保存(publish.sh)し、実行権限を付与。
chmod +x publish.sh
- パラメータ指定して実行(param1=layer-name, param2=lib-name, param3=lib-version, param4=python-version)
./publish.sh requests-python37 requests 2.22.0 python3.7
- 同じLayer-nameを指定して再実行すると、新しいlayer-versionが作成されます。
作りが雑ですが、一応動きます。。。手元の環境を汚したくない時にどうぞ。
#
# Require: docker installed.
# Require: aws config for publishing lambda layer
#
# 1. in container, pip install python liblary and zip,
# 2. publish to aws layer
#
# Usage: param1=layer-name, param2=lib-name, param3=lib-version, param4=python-version
# ex ```./publish.sh requests-python37 requests 2.23.0 python3.7```
layername=$1 # requests-python37
lib=$2 # requests
ver=$3 # 2.23.0
env=$4 # python3.7
zip=$lib-$ver-$env.zip # requests-2.23.0-python3.8.zip
echo packing: $zip
# Prepare
mkdir -p build
rm -rf build/* # Clean up
# pip and zip in docker.
docker run --rm -it \
--volume `pwd`/build:/tmp/build \
--workdir /tmp/build \
lambci/lambda:build-$env \
bash -c "pip3 install $lib==$ver -t /tmp/build/python && zip -qr $zip python/ && rm -rf /tmp/build/python && chown -R `id -u`:`id -g` ./*"
echo packed : build/$zip
aws lambda publish-layer-version \
--layer-name $layername \
--zip-file fileb://`pwd`/build/$zip \
--description $zip \
--compatible-runtimes $env