Last active
November 21, 2017 14:22
-
-
Save giehlman/5ab9838c6ab515611b447f7842839081 to your computer and use it in GitHub Desktop.
For npm projects! Convenience script to build, pack and upload code to an AWS Lambda function, using the AWS CLI. For personal and experimental use only!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#title : lambda-publish-npm.sh | |
#description : For npm 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 <[email protected]> | |
#date : 20170410 | |
#version : 1.1 | |
#usage : sh lambda-publish-npm.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 | |
# | |
PROJECT_NAME=lambda-printbureau-notifier | |
ARN=arn:aws:lambda:eu-central-1:216518302536:function:lambda-printbureau-notifier | |
PROFILE=internal-user | |
REGION=eu-central-1 | |
pack() { | |
rm *.zip | |
rm *.tgz | |
rm -rf package | |
npm pack | |
FILE=$(find * -type f -iname ${PROJECT_NAME}*.tgz -print -quit) | |
tar -xf ${FILE} | |
cd package && zip -r ../${FILE}.zip ./ && cd .. | |
rm *.tgz | |
rm -rf package | |
exitOnError $? "Unable to zip!" | |
FILE=$(find * -type f -iname $PROJECT_NAME*.zip -print -quit) | |
echo "##### Adding node_modules..." | |
zip -ur ${FILE} node_modules | |
exitOnError $? "Unable to add node_modules to zip!" | |
} | |
echo "### Packaging..." | |
pack | |
exitOnError $? "Unable to package!" | |
echo "### Using file '${FILE}' to deploy to lambda..." | |
read -p "-----> DO YOU WANT TO DEPLOY? [Yy]es " -n 1 -r | |
if ! [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "!!! ABORTED !!!" | |
exit 1 | |
fi | |
# | |
# Deploy! | |
# | |
aws --region ${REGION} \ | |
--profile ${PROFILE} \ | |
lambda update-function-code \ | |
--function-name ${ARN} \ | |
--zip-file fileb://${FILE} \ | |
--publish | |
exitOnError $? "Deployment failed!" | |
echo "### Cleaning up..." | |
rm -rf package | |
echo "### Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment