Skip to content

Instantly share code, notes, and snippets.

@PopeFelix
Created August 7, 2019 17:52
Show Gist options
  • Select an option

  • Save PopeFelix/f3f7bac5622ab3e7256f48b6798c0841 to your computer and use it in GitHub Desktop.

Select an option

Save PopeFelix/f3f7bac5622ab3e7256f48b6798c0841 to your computer and use it in GitHub Desktop.
Shell script to deploy a Lambda function
#!/usr/bin/env bash
deploy-lambda $1
# A little shell script I worked up to deploy Lambda functions from my local dev box
function deploy-lambda {
basedir=$HOME/work
dir=$1
role=${LAMBDA_ROLE:-arn:aws:iam::123456789012:role/default-role-goes-here}
runtime=${LAMBDA_RUNTIME:-provided}
if [ ! $dir ] ; then
if [[ ! $PWD =~ $HOME/work/ ]] ; then
echo "You must call package_lambda from a directory under $HOME/work or with an argument"
return 1;
fi
dir=$PWD;
else
dir="$basedir/$dir"
fi
if [ ! -d $dir ] ; then
echo "No such directory \"$dir\""
return 1;
fi
function_name=${FUNCTION_NAME:-`basename $dir`}
function_exists=`aws lambda get-function --function-name $function_name 2> /dev/null`
if [ ! "$function_exists" ] ; then
echo "Creating Lambda function $function_name"
# I'm using this to do Lambda functions in Perl, which require a custom runtime. Take out the two lines below
# if you don't need custom layers
layer_name=my_custom_runtime
layer=${LAMBDA_LAYER:-`aws lambda list-layer-versions --layer-name $layer_name --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'`}
awscmd="aws lambda create-function --function-name $function_name --handler handler.handle \
--role $LAMBDA_ROLE --runtime provided --layers $layer --zip-file fileb://$basedir/$function_name.zip"
else
echo "Updating Lambda function $function_name"
awscmd="aws lambda update-function-code --function-name $function_name --zip-file fileb://$basedir/$function_name.zip"
fi
# OK, this is still pretty perl specific. I'll fix that later maybe
zipcmd="zip -r $basedir/$function_name.zip handler.pl local"
cd $dir
echo "$zipcmd && $awscmd"
if sh -c "$zipcmd && $awscmd" ; then
rv=0
else
rv=-1
fi
cd $OLDPWD
return $rv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment