Skip to content

Commit 0a7d69b

Browse files
committed
Fix shared bucket check: add || true for set -e compatibility, fix cleanup message
All 7 tutorials with shared bucket check now handle: - WITH stack: uses shared bucket, keeps it on cleanup - WITHOUT stack: creates own bucket, deletes on cleanup - Stack-only cleanup: ./cleanup-prereqs-bucket.sh --stack-only Bug: describe-stacks returns non-zero when stack doesn't exist, which triggers set -e. Fixed with || true in all 7 scripts.
1 parent 6de65bd commit 0a7d69b

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

tuts/000-prereqs-bucket/cleanup-prereqs-bucket.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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
45
set -eo pipefail
56

67
STACK_NAME="tutorial-prereqs-bucket"
8+
STACK_ONLY=false
9+
[ "$1" = "--stack-only" ] && STACK_ONLY=true
710

811
BUCKET_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
1417
fi
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+
1627
echo "Bucket: $BUCKET_NAME"
1728
echo ""
1829
echo "Contents:"

tuts/003-s3-gettingstarted/s3-gettingstarted.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fi
2222
UNIQUE_ID=$(cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 12 | head -n 1)
2323
# Check for shared prereq bucket
2424
PREREQ_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)
2626
if [ -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

130132
echo "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
133136
REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-${CONFIGURED_REGION}}}"
@@ -142,6 +145,7 @@ fi
142145
echo "$CREATE_OUTPUT"
143146
CREATED_RESOURCES+=("s3:bucket:${BUCKET_NAME}")
144147
echo "Bucket created."
148+
fi
145149
echo ""
146150

147151
# ============================================================================

0 commit comments

Comments
 (0)