Skip to content

Commit

Permalink
Update scripts must not fail if Lambda function does not already exis…
Browse files Browse the repository at this point in the history
…t. (#480)

* Update scripts must not fail if Lambda function does not already exist.

When updating from older versions of SaaS Boost to newer ones, we rely
on each Lambda function's update.sh script to build the code and update
any relevant Lambda functions in the AWS account. If any of these
update.sh scripts fail, an entire SaaS Boost update will fail, since it
assumes something has gone wrong with building or uploading that code.

However, in the case we add a new Lambda function, the existing Lambda
function may not already exist until the CloudFormation stack is
updated, which is something the Installer executes after running each
update.sh script. This change makes these update scripts not fail in
that case.

* Switch to using lambda list

---------

Co-authored-by: PoeppingT <[email protected]>
  • Loading branch information
PoeppingT and PoeppingT authored Feb 13, 2023
1 parent 7f4b9a1 commit 16aa427
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 26 deletions.
3 changes: 1 addition & 2 deletions functions/authorizer/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi

aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-authorizer"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-authorizer\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/core-stack-listener/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi
# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-core-stack-listener"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-core-stack-listener\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/ecs-service-update/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ fi

aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-update-ecs"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-update-ecs\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/ecs-shutdown-services/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi
# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-ecs-shutdown-services"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-ecs-shutdown-services\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/ecs-startup-services/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi
# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-ecs-startup-services"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-ecs-startup-services\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/onboarding-app-stack-listener/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi

aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-onboarding-app-listener"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-onboarding-app-listener\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/onboarding-stack-listener/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ fi

aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-onboarding-listener"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-onboarding-listener\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
3 changes: 1 addition & 2 deletions functions/system-rest-api-client/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ fi

aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-private-api-client"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-private-api-client\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
1 change: 1 addition & 0 deletions functions/workload-deploy/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTIONS=("sb-${ENVIRONMENT}-workload-deploy"
)
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-workload-deploy\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
Expand Down
8 changes: 6 additions & 2 deletions resources/custom-resources/app-services-macro/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ fi
# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for saas-boost-app-services-macro\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "saas-boost-app-services-macro" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`saas-boost-app-services-macro\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
7 changes: 6 additions & 1 deletion resources/custom-resources/cidr-dynamodb/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ fi
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-populate-ddb\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "sb-${ENVIRONMENT}-populate-ddb" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-populate-ddb\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
9 changes: 6 additions & 3 deletions resources/custom-resources/clear-ecr-repo/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ fi
# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

FUNCTION="sb-${ENVIRONMENT}-clear-ecr-repo"
printf "Updating function code for ${FUNCTION}\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "${FUNCTION}" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-clear-ecr-repo\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
8 changes: 7 additions & 1 deletion resources/custom-resources/clear-s3-bucket/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ fi
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-clear-bucket\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "sb-${ENVIRONMENT}-clear-bucket" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE

eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-clear-bucket\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
58 changes: 58 additions & 0 deletions resources/custom-resources/cognito-app-client-details/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -z $1 ]; then
echo "Usage: $0 <Environment> [Lambda Folder]"
exit 2
fi

MY_AWS_REGION=$(aws configure list | grep region | awk '{print $2}')
echo "AWS Region = $MY_AWS_REGION"

ENVIRONMENT=$1
LAMBDA_STAGE_FOLDER=$2
if [ -z $LAMBDA_STAGE_FOLDER ]; then
LAMBDA_STAGE_FOLDER="lambdas"
fi
LAMBDA_CODE=CognitoAppClientDetails-lambda.zip

#set this for V2 AWS CLI to disable paging
export AWS_PAGER=""

SAAS_BOOST_BUCKET=$(aws --region $MY_AWS_REGION ssm get-parameter --name "/saas-boost/${ENVIRONMENT}/SAAS_BOOST_BUCKET" --query 'Parameter.Value' --output text)
echo "SaaS Boost Bucket = $SAAS_BOOST_BUCKET"
if [ -z $SAAS_BOOST_BUCKET ]; then
echo "Can't find SAAS_BOOST_BUCKET in Parameter Store"
exit 1
fi

# Do a fresh build of the project
mvn
if [ $? -ne 0 ]; then
echo "Error building project"
exit 1
fi

# And copy it up to S3
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-cognito-client-details\n"

eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-cognito-client-details\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
7 changes: 6 additions & 1 deletion resources/custom-resources/rds-options/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ fi
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-rds-options\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "sb-${ENVIRONMENT}-rds-options" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-rds-options\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
7 changes: 6 additions & 1 deletion resources/custom-resources/set-instance-protection/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ fi
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-set-instance-protection\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "sb-${ENVIRONMENT}-set-instance-protection" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-set-instance-protection\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done
7 changes: 6 additions & 1 deletion resources/custom-resources/start-codebuild/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ fi
aws s3 cp target/$LAMBDA_CODE s3://$SAAS_BOOST_BUCKET/$LAMBDA_STAGE_FOLDER/

printf "Updating function code for sb-${ENVIRONMENT}-start-build\n"
aws lambda --region "$MY_AWS_REGION" update-function-code --function-name "sb-${ENVIRONMENT}-start-build" --s3-bucket "$SAAS_BOOST_BUCKET" --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
eval FUNCTIONS=\$\("aws --region $MY_AWS_REGION lambda list-functions --query 'Functions[?starts_with(FunctionName, \`sb-${ENVIRONMENT}-start-build\`)] | [].FunctionName' --output text"\)

for FUNCTION in ${FUNCTIONS[@]}; do
#echo $FUNCTION
aws lambda --region $MY_AWS_REGION update-function-code --function-name $FUNCTION --s3-bucket $SAAS_BOOST_BUCKET --s3-key $LAMBDA_STAGE_FOLDER/$LAMBDA_CODE
done

0 comments on commit 16aa427

Please sign in to comment.