lambda/deploy
#!/bin/bash
## Usage (from project root):
#
# ./lambda/deploy [scanner] [--create]
#
# The --create flag will create a new function. Otherwise,
# the function is assumed to exist and will be updated in place.
#
# Examples:
# ./lambda/deploy noop --create
# ./lambda/deploy sslyze
SCANNER_NAME=$1
FUNCTION_NAME="task_$SCANNER_NAME"
IS_CREATE=$2
if [ -z "$1" ]; then
echo "ERROR: A scanner name is required."
fi
echo "Building $FUNCTION_NAME from $SCANNER_NAME..."
# Go into the lambda dir
cd lambda
# From the lambda dir - use the build/ dir to assemble a zip
# and "publish" it back up to the lambda dir.
rm -r build
mkdir -p build
cd build
# Copy the lambda handler, the scanner itself, and utils.
cp ../lambda_handler.py .
mkdir -p scanners
mkdir -p utils
cp ../../scanners/$SCANNER_NAME.py scanners/.
cp ../../utils/*.py utils/.
# Copy a (possibly remotely built) locally versioned virtualenv.
#
# Copying a remotely build virtualenv to the local environment
# is not captured in this script. Run the scp (or whatever)
# command directly before running this script.
if [ -f ../envs/$SCANNER_NAME.zip ]; then
echo "Incorporating custom-built env for $SCANNER_NAME..."
cp ../envs/$SCANNER_NAME.zip .
unzip -q $SCANNER_NAME.zip
rm $SCANNER_NAME.zip
else
echo "Incorporating catch-all domain-scan env..."
cp ../envs/domain-scan.zip .
unzip -q domain-scan.zip
rm domain-scan.zip
fi
echo "Building zip package for $FUNCTION_NAME..."
zip -rq9 $FUNCTION_NAME.zip .
cd ..
# Create the function using the zipped code.
if [ "$IS_CREATE" == "--create" ]; then
echo "Creating Lambda function $FUNCTION_NAME..."
aws lambda create-function \
--function-name $FUNCTION_NAME \
--zip-file fileb://./build/$FUNCTION_NAME.zip \
--role $AWS_LAMBDA_ROLE \
--handler lambda_handler.handler \
--runtime python3.6 \
--timeout 900 \
--memory-size 128
# Or, update the function's code with the latest zipped code.
else
echo "Updating Lambda code file for $FUNCTION_NAME..."
aws lambda update-function-code \
--function-name $FUNCTION_NAME \
--zip-file fileb://./build/$FUNCTION_NAME.zip
fi
# back up to project root
cd ..
# Kept here for reference, not used in the script:
#
# echo "Updating Lambda handler function for $FUNCTION_NAME..."
# aws lambda update-function-configuration \
# --function-name $FUNCTION_NAME \
# --handler lambda_handler.handler