Skip to content

Instantly share code, notes, and snippets.

@Hritik14
Last active October 13, 2020 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hritik14/e504fc4d02a7d8d8032f026c93edafe9 to your computer and use it in GitHub Desktop.
Save Hritik14/e504fc4d02a7d8d8032f026c93edafe9 to your computer and use it in GitHub Desktop.
Deploy go project to AWS lambda.
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
#CHANGE THIS
AWS_FUNCTION_NAME=routine
_green(){
echo -e "\033[32m$1\033[0m"
}
_red(){
echo -e "\033[31m$1\033[0m"
}
cleanup(){
rm -f "$ZIP"
}
trap cleanup SIGINT EXIT
_green "[+] aws function name: $AWS_FUNCTION_NAME"
MODULE=$(go mod graph | head -n1 | cut -f 1 -d " ")
_green "[+] Detected module name: $MODULE"
_green "[+] Building"
go clean
CGO_ENABLED=0 go build
_green "[+] Zipping"
ZIP=$(mktemp "lambda-$AWS_FUNCTION_NAME-XXX.zip")
zip --display-globaldots - "$MODULE" > "$ZIP"
_green "[+] Deploying"
reply=$(aws lambda update-function-code \
--function-name "$AWS_FUNCTION_NAME" \
--zip-file "fileb://$ZIP" )
echo "$reply" | jq '{Handler,State,LastModified,CodeSize,LastUpdateStatus}' | tr -d "{},\""
if [[ $(echo "$reply" | jq '.LastUpdateStatus') == "\"Successful\"" ]]; then
_green "[+] Done"
else
_red "[-] Failed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment