Skip to content

Instantly share code, notes, and snippets.

@giehlman
Last active April 29, 2018 14:08
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 giehlman/e1d627e87c8ad5b6fac99a1158607d82 to your computer and use it in GitHub Desktop.
Save giehlman/e1d627e87c8ad5b6fac99a1158607d82 to your computer and use it in GitHub Desktop.
For gradle projects! Convenience script to build, pack and upload code to an AWS Lambda function, using the AWS CLI. For personal and experimental use only!
#!/usr/bin/env bash
#title : lambda-publish.sh
#description : For gradle or mvn projects! Convenience script to build, pack and upload code to an AWS Lambda function, using the AWS CLI. For personal and experimental use only!
#author : Christian-André Giehl <christian@emailbrief.de>
#date : 20170410
#version : 1.1
#usage : sh lambda-publish.sh
#==============================================================================
# Exits in case the supplied state is != 0. State is typically supplied via $?
exitOnError() {
state=$1
msg=$2
if [ $state -ne 0 ]; then
echo "!!! ${msg}"
echo "### Exiting."
exit $state
fi
}
# Check is AWS CLI is there
command -v aws
exitOnError $? "AWS CLI not found or not accessible!"
#
# AWS cfg
#
ARN=arn:aws:lambda:eu-central-1:someId:function:someFunction
PROFILE=internal-user # make sure profile exists in ~/.aws/config
REGION=eu-central-1
useMaven=true # or e.g. [[ -e "pom.xml" ]]
if [ useMaven ]
then
echo "### Starting mvn..."
mvn clean package
JAR=$(find ./target -iname *-jar-with-dependencies.jar -print -quit)
else
echo "### Starting gradle..."
./gradlew clean test fatJar
JAR=$(find ./build -iname *.jar -print -quit)
fi
exitOnError $? "Unable to build!"
if [ -z "${JAR}" ]; then
echo "JAR-file could not be found (yields: '${JAR}'. Stopping..."
exit 1
fi
echo "### Using file '${JAR}' to deploy to lambda..."
read -p "-----> DO YOU REALLY WANT TO DEPLOY? [Yy]es " -n 1 -r
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
echo "!!! ABORTED !!!"
exit 1
fi
#
# Deploy!
#
aws --profile ${PROFILE} \
--region ${REGION} \
lambda update-function-code \
--function-name ${ARN} \
--zip-file fileb://${JAR} \
--publish
exitOnError $? "Deployment failed!"
echo "### Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment