Skip to content

Instantly share code, notes, and snippets.

@nickovs
Created March 13, 2023 16:41
Show Gist options
  • Save nickovs/6062cd62d5094b6a4bd42659cdf586b2 to your computer and use it in GitHub Desktop.
Save nickovs/6062cd62d5094b6a4bd42659cdf586b2 to your computer and use it in GitHub Desktop.
A script to build an AWS Lambda overlay layer that installs `git` and all its dependencies.
#!/bin/bash
# Build an AWS Lambda zip file containing git and its dependencies
cat <<\EOF > tmp_build.sh
#!/bin/bash
# Comment this out if you want to see all the actions.
quiet=-q
# List of main executables from /usr/bin to copy into layer
main_execs=("git")
# List of libexec executables NOT to copy into layer
exclude_libexecs=("git-shell" "scalar")
echo "Installing packages"
# Install the git pacakge, as well file for testing the flie types and zip to package things up
yum install -y $quiet git file zip
echo "Copying executables"
# Create temporary bin and lib directories
mkdir -p /var/task/{bin,lib}
# Copy over main executables
for fname in ${main_execs[*]}; do
cp /usr/bin/$fname /var/task/bin
done
# Copy over any libexec executables that are not symlinks and not in the exclude list
for fname in $(find /usr/libexec/git-core -type f -executable); do
echo ${exclude_libexecs[*]} | grep -w -q $(basename $fname)
if [ $? -ne 0 ]; then
cp $fname /var/task/bin
fi
done
echo "Copying libraries"
# Copy over all the library dependencies of the copied binaries
# (This could be more efficient, since it will copy a bunch of files multiple times)
for fname in /var/task/bin/*; do
if [ $(file -b --mime-type $fname) == "application/x-sharedlib" ]; then
ldd $fname | awk 'NF == 4 { system("cp " $3 " /var/task/lib/") }'
fi
done
echo "Fixing symlinks"
# Create symlinks for all the (non-excluded) linked executables
for fname in $(find /usr/libexec/git-core -type l); do
source=$(basename $fname)
target=$(basename $(readlink -f $fname))
if [ ! -f bin/$source ]; then
ln -s $target bin/$source
fi
done
echo "Preparing zip file"
# Package temporary bin and lib into zip file.
zip --symlinks $quiet -r9 /build/git-layer.zip bin lib
echo "Done."
EOF
chmod +x tmp_build.sh
rm -f git-layer.zip
docker run --rm -it -v $(pwd):/build --entrypoint /build/tmp_build.sh amazon/aws-lambda-python
rm tmp_build.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment