File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#! /bin/bash
2- # Empty and delete the shared tutorial S3 bucket, then delete the CloudFormation stack.
3- # Usage: ./cfn/cleanup-bucket.sh
2+ # Clean up the shared tutorial S3 bucket and/or CloudFormation stack.
3+ # Usage: ./cleanup-prereqs-bucket.sh [--stack-only]
4+ # --stack-only: delete the CFN stack but keep the bucket
45set -eo pipefail
56
67STACK_NAME=" tutorial-prereqs-bucket"
8+ STACK_ONLY=false
9+ [ " $1 " = " --stack-only" ] && STACK_ONLY=true
710
811BUCKET_NAME=$( aws cloudformation describe-stacks --stack-name " $STACK_NAME " \
912 --query ' Stacks[0].Outputs[?OutputKey==`BucketName`].OutputValue' --output text 2> /dev/null)
@@ -13,6 +16,14 @@ if [ -z "$BUCKET_NAME" ] || [ "$BUCKET_NAME" = "None" ]; then
1316 exit 0
1417fi
1518
19+ if [ " $STACK_ONLY " = " true" ]; then
20+ echo " Deleting stack only (keeping bucket: $BUCKET_NAME )"
21+ aws cloudformation delete-stack --stack-name " $STACK_NAME "
22+ aws cloudformation wait stack-delete-complete --stack-name " $STACK_NAME "
23+ echo " Done. Stack deleted, bucket retained."
24+ exit 0
25+ fi
26+
1627echo " Bucket: $BUCKET_NAME "
1728echo " "
1829echo " Contents:"
Original file line number Diff line number Diff line change 2222UNIQUE_ID=$( cat /dev/urandom | tr -dc ' a-f0-9' | fold -w 12 | head -n 1)
2323# Check for shared prereq bucket
2424PREREQ_BUCKET=$( aws cloudformation describe-stacks --stack-name tutorial-prereqs-bucket \
25- --query ' Stacks[0].Outputs[?OutputKey==`BucketName`].OutputValue' --output text 2> /dev/null)
25+ --query ' Stacks[0].Outputs[?OutputKey==`BucketName`].OutputValue' --output text 2> /dev/null || true )
2626if [ -n " $PREREQ_BUCKET " ] && [ " $PREREQ_BUCKET " != " None" ]; then
2727 BUCKET_NAME=" $PREREQ_BUCKET "
2828 BUCKET_IS_SHARED=true
@@ -92,9 +92,11 @@ cleanup() {
9292 done <<< " $DELETE_MARKERS_OUTPUT"
9393 fi
9494
95- echo " Deleting bucket: ${BUCKET_NAME} "
9695 if [ " $BUCKET_IS_SHARED " = " false" ]; then
96+ echo " Deleting bucket: ${BUCKET_NAME} "
9797 aws s3api delete-bucket --bucket " $BUCKET_NAME " 2>&1 || echo " WARNING: Failed to delete bucket ${BUCKET_NAME} "
98+ else
99+ echo " Keeping shared bucket: ${BUCKET_NAME} "
98100 fi
99101
100102 echo " "
@@ -128,6 +130,7 @@ trap 'handle_error "line $LINENO"' ERR
128130# ============================================================================
129131
130132echo " Step 1: Creating bucket ${BUCKET_NAME} ..."
133+ if [ " $BUCKET_IS_SHARED " = " false" ]; then
131134
132135# CreateBucket requires LocationConstraint for all regions except us-east-1
133136REGION=" ${AWS_REGION:- ${AWS_DEFAULT_REGION:- ${CONFIGURED_REGION} } } "
142145echo " $CREATE_OUTPUT "
143146CREATED_RESOURCES+=(" s3:bucket:${BUCKET_NAME} " )
144147echo " Bucket created."
148+ fi
145149echo " "
146150
147151# ============================================================================
You can’t perform that action at this time.
0 commit comments