Skip to content

Instantly share code, notes, and snippets.

@sirkuttin
Last active May 17, 2023 16:28
Show Gist options
  • Save sirkuttin/190ddf867d12eed3b1ed35ea7a758b86 to your computer and use it in GitHub Desktop.
Save sirkuttin/190ddf867d12eed3b1ed35ea7a758b86 to your computer and use it in GitHub Desktop.
lambda builder
#!/bin/bash
set -e
# Build Golang code in a given directory for the linux x86_64 architecture
build_golang() {
GOOS=linux GOARCH=amd64 go build -o handler
zip $build_dir/$artifact_name handler
rm handler
}
# Python code in a given directory will be zipped up in an archive and also run pipenv to convert the pipfile into a requirements.txt and pip install the requirements.txt to get a directory of dependencies and then zip up the dependencies
build_python() {
cd src
zip -r $build_dir/$artifact_name .
cd -
# if there is a pipfile, build the layer zip
if [ -f Pipfile ]; then
pipenv requirements > requirements.txt
pip install -r requirements.txt -t python
zip -q -r $build_dir/$artifact_layer_name python/
rm requirements.txt
# if there is requirement.txt, build the layer zip
elif [ -f requirements.txt ]; then
pip install -r requirements.txt -t python
zip -q -r $build_dir/$artifact_layer_name python/
fi
rm -Rf python
}
# Node.js code in a given directory will be zipped up in an archive and also running npm install for prod only and package up the node modules into a separate zip for the lambda layer
build_nodejs() {
cd src
zip -r $build_dir/$artifact_name .
cd -
# if there is a package.json file, build the layer zip
if [ -f package.json ]; then
npm install --omit=dev
# if there is a node_modules directory, zip it up
if [ -d node_modules ]; then
mkdir -p nodejs
mv node_modules nodejs/node_modules
zip -q -r $build_dir/$artifact_layer_name nodejs/
fi
fi
rm -Rf nodejs node_modules
}
# Typescript code in a given directory will be transpilled using npm build command and then the javascript files in the build subdirectory will be zipped up and then running npm install for prod only and package up the node modules into a separate zip for the lambda layer
build_typescript() {
npm install --production=false
npm run build
cd dist && zip -r $build_dir/$artifact_name .
cd -
if [ -f package.json ]; then
npm install --omit=dev
if [ -d node_modules ]; then
mkdir -p nodejs
mv node_modules nodejs/node_modules
zip -q -r $build_dir/$artifact_layer_name nodejs/
fi
fi
rm -Rf nodejs node_modules dist
}
upload_to_s3() {
if [ -f $build_dir/$artifact_name ]; then
aws s3 cp $build_dir/$artifact_name s3://cirrus-terraform-lambda-artifacts/$repo_name/$commit_hash/
rm $build_dir/$artifact_name
fi
if [ -f $build_dir/$artifact_layer_name ]; then
aws s3 cp $build_dir/$artifact_layer_name s3://cirrus-terraform-lambda-artifacts/$repo_name/$commit_hash/
rm $build_dir/$artifact_layer_name
fi
}
# get command line flags
while getopts "l:d:" opt; do
case $opt in
l) language="$OPTARG"
;;
d) dir_path="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
# init some variables
commit_hash=$(git log -1 --format=format:%H)
repo_name=$(basename `git rev-parse --show-toplevel`)
repo_dir=$(git rev-parse --show-toplevel)
# directory for builds
build_dir=$repo_dir/build/
# create artifact zip names
artifact_base_name=$(basename $dir_path)
artifact_name="${artifact_base_name}.zip"
artifact_layer_name="${artifact_base_name}_layer.zip"
source_code_path=$repo_dir/$dir_path
cd $source_code_path
# build for specific language
case "$language" in
golang)
build_golang
;;
nodejs)
build_nodejs
;;
python)
build_python
;;
typescript)
build_typescript
;;
*)
echo "Usage: $0 {golang|nodejs|python|typescript} prefix"
esac
# upload new build zips to S3
upload_to_s3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment