From 16aa427d5c0e9ea4c606f58fd9c14980dc2435e1 Mon Sep 17 00:00:00 2001 From: PoeppingT Date: Mon, 13 Feb 2023 15:09:51 -0800 Subject: [PATCH] Update scripts must not fail if Lambda function does not already exist. (#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 --- functions/authorizer/update.sh | 3 +- functions/core-stack-listener/update.sh | 3 +- functions/ecs-service-update/update.sh | 3 +- functions/ecs-shutdown-services/update.sh | 3 +- functions/ecs-startup-services/update.sh | 3 +- .../onboarding-app-stack-listener/update.sh | 3 +- functions/onboarding-stack-listener/update.sh | 3 +- functions/system-rest-api-client/update.sh | 3 +- functions/workload-deploy/update.sh | 1 + .../app-services-macro/update.sh | 8 ++- .../custom-resources/cidr-dynamodb/update.sh | 7 ++- .../custom-resources/clear-ecr-repo/update.sh | 9 ++- .../clear-s3-bucket/update.sh | 8 ++- .../cognito-app-client-details/update.sh | 58 +++++++++++++++++++ .../custom-resources/rds-options/update.sh | 7 ++- .../set-instance-protection/update.sh | 7 ++- .../start-codebuild/update.sh | 7 ++- 17 files changed, 110 insertions(+), 26 deletions(-) create mode 100755 resources/custom-resources/cognito-app-client-details/update.sh diff --git a/functions/authorizer/update.sh b/functions/authorizer/update.sh index 5ef9df77..944624ac 100755 --- a/functions/authorizer/update.sh +++ b/functions/authorizer/update.sh @@ -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 diff --git a/functions/core-stack-listener/update.sh b/functions/core-stack-listener/update.sh index e7609435..82c2ae43 100755 --- a/functions/core-stack-listener/update.sh +++ b/functions/core-stack-listener/update.sh @@ -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 diff --git a/functions/ecs-service-update/update.sh b/functions/ecs-service-update/update.sh index b89550b6..498675a4 100755 --- a/functions/ecs-service-update/update.sh +++ b/functions/ecs-service-update/update.sh @@ -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 diff --git a/functions/ecs-shutdown-services/update.sh b/functions/ecs-shutdown-services/update.sh index 57f4344b..cb3651ec 100755 --- a/functions/ecs-shutdown-services/update.sh +++ b/functions/ecs-shutdown-services/update.sh @@ -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 diff --git a/functions/ecs-startup-services/update.sh b/functions/ecs-startup-services/update.sh index 0cee42aa..b4cc945b 100755 --- a/functions/ecs-startup-services/update.sh +++ b/functions/ecs-startup-services/update.sh @@ -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 diff --git a/functions/onboarding-app-stack-listener/update.sh b/functions/onboarding-app-stack-listener/update.sh index 3f412ab6..bbb8f76c 100755 --- a/functions/onboarding-app-stack-listener/update.sh +++ b/functions/onboarding-app-stack-listener/update.sh @@ -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 diff --git a/functions/onboarding-stack-listener/update.sh b/functions/onboarding-stack-listener/update.sh index bbb05d1c..982852e1 100755 --- a/functions/onboarding-stack-listener/update.sh +++ b/functions/onboarding-stack-listener/update.sh @@ -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 diff --git a/functions/system-rest-api-client/update.sh b/functions/system-rest-api-client/update.sh index f06ebec8..8b0c0bdb 100755 --- a/functions/system-rest-api-client/update.sh +++ b/functions/system-rest-api-client/update.sh @@ -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 diff --git a/functions/workload-deploy/update.sh b/functions/workload-deploy/update.sh index 87215f0f..7c407ed2 100755 --- a/functions/workload-deploy/update.sh +++ b/functions/workload-deploy/update.sh @@ -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 diff --git a/resources/custom-resources/app-services-macro/update.sh b/resources/custom-resources/app-services-macro/update.sh index 5f305d64..52c20e3f 100755 --- a/resources/custom-resources/app-services-macro/update.sh +++ b/resources/custom-resources/app-services-macro/update.sh @@ -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 \ No newline at end of file diff --git a/resources/custom-resources/cidr-dynamodb/update.sh b/resources/custom-resources/cidr-dynamodb/update.sh index 2bc2d025..64a66a68 100755 --- a/resources/custom-resources/cidr-dynamodb/update.sh +++ b/resources/custom-resources/cidr-dynamodb/update.sh @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/resources/custom-resources/clear-ecr-repo/update.sh b/resources/custom-resources/clear-ecr-repo/update.sh index 34355082..0f714ec8 100755 --- a/resources/custom-resources/clear-ecr-repo/update.sh +++ b/resources/custom-resources/clear-ecr-repo/update.sh @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/resources/custom-resources/clear-s3-bucket/update.sh b/resources/custom-resources/clear-s3-bucket/update.sh index 2b199df4..188b4c74 100755 --- a/resources/custom-resources/clear-s3-bucket/update.sh +++ b/resources/custom-resources/clear-s3-bucket/update.sh @@ -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 \ No newline at end of file + +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 \ No newline at end of file diff --git a/resources/custom-resources/cognito-app-client-details/update.sh b/resources/custom-resources/cognito-app-client-details/update.sh new file mode 100755 index 00000000..33d165b0 --- /dev/null +++ b/resources/custom-resources/cognito-app-client-details/update.sh @@ -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 [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 \ No newline at end of file diff --git a/resources/custom-resources/rds-options/update.sh b/resources/custom-resources/rds-options/update.sh index e141d194..406e4488 100755 --- a/resources/custom-resources/rds-options/update.sh +++ b/resources/custom-resources/rds-options/update.sh @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/resources/custom-resources/set-instance-protection/update.sh b/resources/custom-resources/set-instance-protection/update.sh index b949d6c1..72416310 100755 --- a/resources/custom-resources/set-instance-protection/update.sh +++ b/resources/custom-resources/set-instance-protection/update.sh @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/resources/custom-resources/start-codebuild/update.sh b/resources/custom-resources/start-codebuild/update.sh index 10e881bc..829bd854 100755 --- a/resources/custom-resources/start-codebuild/update.sh +++ b/resources/custom-resources/start-codebuild/update.sh @@ -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 \ No newline at end of file