1- #! /bin/bash
1+ #! /bin/bash -e
22
33export CDK_PARAM_SYSTEM_ADMIN_EMAIL=" $1 "
44
@@ -7,62 +7,26 @@ if [[ -z "$CDK_PARAM_SYSTEM_ADMIN_EMAIL" ]]; then
77 exit 1
88fi
99
10- REGION=$( aws ec2 describe-availability-zones --output text --query ' AvailabilityZones[0].[RegionName]' ) # Region setting
11- ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
12-
13- export CDK_PARAM_S3_BUCKET_NAME=" saas-reference-architecture-ecs-$ACCOUNT_ID -$REGION "
10+ export REGION=$( aws ec2 describe-availability-zones --output text --query ' AvailabilityZones[0].[RegionName]' ) # Region setting
11+ export ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
1412
1513# Create S3 Bucket for provision source.
14+ source ./update-provision-source.sh
1615
17- if aws s3api head-bucket --bucket $CDK_PARAM_S3_BUCKET_NAME 2> /dev/null; then
18- echo " Bucket $CDK_PARAM_S3_BUCKET_NAME already exists."
19- else
20- echo " Bucket $CDK_PARAM_S3_BUCKET_NAME does not exist. Creating a new bucket in $REGION region in $ACCOUNT_ID "
21-
22- if [ " $REGION " == " us-east-1" ]; then
23- aws s3api create-bucket --bucket $CDK_PARAM_S3_BUCKET_NAME
24- else
25- aws s3api create-bucket \
26- --bucket $CDK_PARAM_S3_BUCKET_NAME \
27- --region " $REGION " \
28- --create-bucket-configuration LocationConstraint=" $REGION "
29- fi
30-
31- aws s3api put-bucket-versioning \
32- --bucket $CDK_PARAM_S3_BUCKET_NAME \
33- --versioning-configuration Status=Enabled
34-
35- aws s3api put-public-access-block \
36- --bucket $CDK_PARAM_S3_BUCKET_NAME \
37- --public-access-block-configuration \
38- BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
39-
40- if [ $? -eq 0 ]; then
41- echo " Bucket $CDK_PARAM_S3_BUCKET_NAME created with versioning enabled."
42- else
43- echo " Error creating bucket $CDK_PARAM_S3_BUCKET_NAME with versioning enabled."
44- exit 1
45- fi
46- fi
47-
48- echo " Bucket exists: $CDK_PARAM_S3_BUCKET_NAME "
49-
50- cd ../
51- zip -rq source.zip . -x " .git/*" -x " **/node_modules/*" -x " **/cdk.out/*" -x " **/.aws-sam/*"
52- export CDK_PARAM_COMMIT_ID=$( aws s3api put-object --bucket " ${CDK_PARAM_S3_BUCKET_NAME} " --key " source.zip" --body " ./source.zip" --output text)
53-
54- rm source.zip
55- echo " Source code uploaded to S3"
16+ echo " CDK_PARAM_COMMIT_ID exists: $CDK_PARAM_COMMIT_ID "
5617
5718# Create ECS service linked role.
58- aws iam create-service-linked-role --aws-service-name ecs.amazonaws.com 2> /dev/null || echo " ECS Service linked role exists"
59-
19+ ECS_ROLE=$( aws iam list-roles --query ' Roles[?contains(RoleName, `AWSServiceRoleForECS`)].Arn' --output text)
20+ if [ -z " $ECS_ROLE " ]; then
21+ aws iam create-service-linked-role --aws-service-name ecs.amazonaws.com | cat
22+ else
23+ echo " ECS Service linked role exists: $ECS_ROLE "
24+ fi
6025# Preprovision basic infrastructure
61- cd ./server
26+ cd ../server
27+
28+ sed " s/<REGION>/$REGION /g; s/<ACCOUNT_ID>/$ACCOUNT_ID /g" ./service-info.txt > ./lib/service-info.json
6229
63- export ECR_REGION=$( aws ec2 describe-availability-zones --output text --query ' AvailabilityZones[0].[RegionName]' )
64- export ACCOUNT_ID=$( aws sts get-caller-identity --query Account --output text)
65- sed " s/<REGION>/$ECR_REGION /g; s/<ACCOUNT_ID>/$ACCOUNT_ID /g" ./service-info.txt > ./lib/service-info.json
6630# npx cdk bootstrap
6731export CDK_PARAM_ONBOARDING_DETAIL_TYPE=' Onboarding'
6832export CDK_PARAM_PROVISIONING_DETAIL_TYPE=$CDK_PARAM_ONBOARDING_DETAIL_TYPE
@@ -76,31 +40,15 @@ export CDK_BASIC_CLUSTER="$CDK_PARAM_STAGE-$CDK_PARAM_TIER"
7640npm install
7741npx cdk bootstrap
7842
79- npx cdk diff tenant-template-stack-basic > ./diff_output.txt 2>&1
80- if grep -q " There were no differences" ./diff_output.txt; then
81- echo " No changes detected in tenant-template-stack-basic."
82- else
83- echo " Changes detected in tenant-template-stack-basic."
84-
85- SERVICES=$( aws ecs list-services --cluster $CDK_BASIC_CLUSTER --query ' serviceArns[*]' --output text || true)
86- for SERVICE in $SERVICES ; do
87- SERVICE_NAME=$( echo $SERVICE | rev | cut -d ' /' -f 1 | rev)
88-
89- echo -n " ==== Service Connect Disable: "
90- aws ecs update-service \
91- --cluster $CDK_BASIC_CLUSTER \
92- --service $SERVICE_NAME \
93- --service-connect-configuration ' enabled=false' \
94- --no-cli-pager --query ' service.serviceArn' --output text
95- done
96- fi
97- rm diff_output.txt
43+ SERVICES=$( aws ecs list-services --cluster $CDK_BASIC_CLUSTER --query ' serviceArns[*]' --output text || true)
44+ for SERVICE in $SERVICES ; do
45+ SERVICE_NAME=$( echo $SERVICE | rev | cut -d ' /' -f 1 | rev)
46+ echo -n " ==== Service Connect re-set if any... "
47+ aws ecs update-service \
48+ --cluster $CDK_BASIC_CLUSTER \
49+ --service $SERVICE_NAME \
50+ --service-connect-configuration ' enabled=false' \
51+ --no-cli-pager --query ' service.serviceArn' --output text
52+ done
9853
9954npx cdk deploy --all --require-approval=never
100-
101-
102- # # Get SaaS application url
103- ADMIN_SITE_URL=$( aws cloudformation describe-stacks --stack-name controlplane-stack --query " Stacks[0].Outputs[?OutputKey=='adminSiteUrl'].OutputValue" --output text)
104- APP_SITE_URL=$( aws cloudformation describe-stacks --stack-name core-appplane-stack --query " Stacks[0].Outputs[?OutputKey=='appSiteUrl'].OutputValue" --output text)
105- echo " Admin site url: $ADMIN_SITE_URL "
106- echo " Application site url: $APP_SITE_URL "
0 commit comments