Skip to content

Instantly share code, notes, and snippets.

@ezr
Last active January 12, 2021 04:06
Show Gist options
  • Save ezr/621827ba5fe5fa5ddf858a8c7d63033d to your computer and use it in GitHub Desktop.
Save ezr/621827ba5fe5fa5ddf858a8c7d63033d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ -z ${1+x} ]; then
echo "Usage: $0 <source.go>"
echo
echo "Or in order to also publish:"
echo " $0 <source.go> <function name>"
exit 2
fi
SRC=$1
echo "Building..."
grep "Arch Linux" /etc/os-release > /dev/null
# On Arch we need to specify CGO_ENABLED=0
# would it hurt to always do that? (when not using CGO)
if [ $? -eq 0 ]; then
CGO_ENABLED=0 go build $SRC
else
go build $SRC
fi
if [ $? -ne 0 ]; then
echo "build failed. exiting..."
exit 1
fi
echo "Starting the chmod step..."
EXE="${SRC%.*}"
chmod 711 "$EXE"
if [ $? -ne 0 ]; then
echo "chmod failed. exiting..."
exit 1
fi
echo "Creating the ZIP file..."
zip "$EXE.zip" "$EXE"
if [ $? -ne 0 ]; then
echo "zip creation failed. exiting..."
exit 1
fi
if [ -z ${2+x} ]; then
echo "Done."
exit 0
fi
echo "Running aws lambda update-function-code ..."
FUNCTION=$2
PWD=`pwd`
aws lambda update-function-code --function-name "$FUNCTION" --zip-file "fileb://$PWD/$EXE.zip" --publish
echo "Done."
@ezr
Copy link
Author

ezr commented Sep 7, 2019

Quick script that builds go binary, creates a zip file from it, and publishes it to AWS lambda. Only works with a single source code file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment