Skip to content
This repository was archived by the owner on Oct 21, 2019. It is now read-only.

Commit d69e827

Browse files
author
Darren Weber
committed
Use a packaging script for the lambda layer
1 parent 2a22ee2 commit d69e827

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

Makefile

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ container-clean:
3737
# lambda layer build and package using /opt
3838

3939
LAYER_BUILD = ${BUILD}-layer
40-
LAYER_PACKAGE := amazonlinux-${TAG}-layer.zip
40+
LAYER_PACKAGE := amazonlinux-${TAG}-layer
4141

4242
lambda-layer-build:
4343
docker build -f Dockerfile -t ${LAYER_BUILD} --build-arg prefix=/opt .
@@ -46,14 +46,15 @@ lambda-layer-shell: lambda-layer-build container-clean
4646
docker run --name amazonlinux --volume $(shell pwd)/:/data --rm -it ${LAYER_BUILD} /bin/bash
4747

4848
lambda-layer-package: lambda-layer-build container-clean
49-
docker run --name amazonlinux -itd ${LAYER_BUILD} /bin/bash
50-
docker exec -it amazonlinux bash -c 'mkdir -p $${PREFIX}/python/lib/python${PY_VERSION}/site-packages'
51-
docker exec -it amazonlinux bash -c 'rsync -a /var/lang/lib/python${PY_VERSION}/site-packages/ $${PREFIX}/python/lib/python${PY_VERSION}/site-packages/'
52-
docker exec -it amazonlinux bash -c 'cd $${PREFIX} && zip -r9 --symlinks /tmp/package.zip python'
53-
docker exec -it amazonlinux bash -c 'cd $${PREFIX} && zip -r9 --symlinks /tmp/package.zip lib/*.so*'
54-
docker exec -it amazonlinux bash -c 'cd $${PREFIX} && zip -r9 --symlinks /tmp/package.zip lib64/*.so*'
55-
docker exec -it amazonlinux bash -c 'cd $${PREFIX} && zip -r9 --symlinks /tmp/package.zip bin'
56-
docker exec -it amazonlinux bash -c 'cd $${PREFIX} && zip -r9 /tmp/package.zip share'
57-
docker cp amazonlinux:/tmp/package.zip ${LAYER_PACKAGE}
49+
docker run --name amazonlinux \
50+
-e PREFIX=${PREFIX} \
51+
-e PY_VERSION=${PY_VERSION} \
52+
-e LAYER_PACKAGE=${LAYER_PACKAGE} \
53+
-itd ${LAYER_BUILD} /bin/bash
54+
docker cp package_lambda_layer.sh amazonlinux:/tmp/package_lambda_layer.sh
55+
docker exec -it amazonlinux bash -c '/tmp/package_lambda_layer.sh'
56+
mkdir -p ./packages
57+
docker cp amazonlinux:/tmp/${LAYER_PACKAGE}_libs.zip ./packages/
58+
docker cp amazonlinux:/tmp/${LAYER_PACKAGE}_python.zip ./packages/
5859
docker stop amazonlinux && docker rm amazonlinux
5960

package_lambda_layer.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
prefix="${PREFIX:-/opt}"
4+
5+
py_version="${PY_VERSION:-3.6}"
6+
7+
layer_package="${LAYER_PACKAGE:-layer_package}"
8+
9+
10+
# Note: excluding many paths in the python libs crashed the layer runtime
11+
# somehow, so the excludes are disabled for now.
12+
src=/var/lang/lib/python${py_version}/site-packages
13+
dst=${prefix}/python/lib/python${py_version}/site-packages
14+
find $src -type d -name '__pycache__' -exec rm -rf {} +
15+
mkdir -p ${dst}
16+
rsync -a $src/ ${dst}/
17+
#rsync -a \
18+
# --exclude 'boto3' \
19+
# --exclude 'botocore' \
20+
# --exclude 'aws_lambda_builders' \
21+
# --exclude 'awscli' \
22+
# --exclude 's3transfer' \
23+
# --exclude 'samtranslator' \
24+
# --exclude 'docker' \
25+
# --exclude 'samcli' \
26+
# --exclude 'wheel' \
27+
# --exclude 'share/doc/*' \
28+
# --exclude 'share/man/man1/*' \
29+
# --exclude 'share/man/man3/*' \
30+
# --exclude 'pipenv' \
31+
# $src/ ${dst}/
32+
33+
# package the binary 'libs' and the python packages into two layers,
34+
# to reduce the size of each package and allow flexibility in using
35+
# just the binaries or the binaries plus the python bindings.
36+
cd ${prefix}
37+
zip -r9 --symlinks /tmp/${layer_package}_python.zip python
38+
zip -r9 --symlinks /tmp/${layer_package}_libs.zip bin lib/*.so* lib64/*.so* share
39+

0 commit comments

Comments
 (0)