|
| 1 | +#!/bin/bash |
| 2 | +# Run the interactive tutorial steps against resources created by CloudFormation. |
| 3 | +# If the stack does not exist, offers to deploy it first. |
| 4 | +set -eo pipefail |
| 5 | + |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 7 | +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 8 | +TUT_DIR="$(basename "$SCRIPT_DIR")" |
| 9 | +STACK_NAME="tutorial-$(echo "$TUT_DIR" | sed 's/^[0-9]*-//')" |
| 10 | + |
| 11 | +get_output() { aws cloudformation describe-stacks --stack-name "$STACK_NAME" --query "Stacks[0].Outputs[?OutputKey==\`$1\`].OutputValue" --output text 2>/dev/null; } |
| 12 | + |
| 13 | +run_cmd() { |
| 14 | + echo "" |
| 15 | + echo "$ $@" |
| 16 | + eval "$@" |
| 17 | +} |
| 18 | + |
| 19 | +# Check if stack exists, offer to create |
| 20 | +STATUS=$(aws cloudformation describe-stacks --stack-name "$STACK_NAME" --query 'Stacks[0].StackStatus' --output text 2>/dev/null || echo "NONE") |
| 21 | +if [ "$STATUS" = "NONE" ] || [ "$STATUS" = "DELETE_COMPLETE" ]; then |
| 22 | + echo "Stack $STACK_NAME does not exist." |
| 23 | + read -rp "Deploy it now? (y/n): " CHOICE |
| 24 | + if [[ "$CHOICE" =~ ^[Yy]$ ]]; then |
| 25 | + "$REPO_ROOT/deploy.sh" "$TUT_DIR" |
| 26 | + else |
| 27 | + echo "Cannot proceed without the stack. Deploy with: ./deploy.sh $TUT_DIR" |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | +fi |
| 31 | +echo "Stack: $STACK_NAME ($STATUS)" |
| 32 | + |
| 33 | +BUCKET=$(get_output BucketName) |
| 34 | +echo "Bucket: $BUCKET" |
| 35 | + |
| 36 | +echo "" |
| 37 | +echo "--- Step 1: Upload an object ---" |
| 38 | +echo "Hello from the S3 tutorial" > /tmp/s3-tut-test.txt |
| 39 | +run_cmd aws s3 cp /tmp/s3-tut-test.txt "s3://$BUCKET/tutorial/hello.txt" |
| 40 | + |
| 41 | +echo "" |
| 42 | +echo "--- Step 2: List objects ---" |
| 43 | +run_cmd aws s3api list-objects-v2 --bucket "$BUCKET" --prefix tutorial/ --query "'Contents[].{Key:Key,Size:Size}'" --output table |
| 44 | + |
| 45 | +echo "" |
| 46 | +echo "--- Step 3: Download the object ---" |
| 47 | +run_cmd aws s3 cp "s3://$BUCKET/tutorial/hello.txt" /tmp/s3-tut-download.txt |
| 48 | +echo "Content: $(cat /tmp/s3-tut-download.txt)" |
| 49 | + |
| 50 | +echo "" |
| 51 | +echo "--- Step 4: Copy the object ---" |
| 52 | +run_cmd aws s3 cp "s3://$BUCKET/tutorial/hello.txt" "s3://$BUCKET/tutorial/backup/hello.txt" |
| 53 | + |
| 54 | +echo "" |
| 55 | +echo "--- Step 5: List all objects ---" |
| 56 | +run_cmd aws s3api list-objects-v2 --bucket "$BUCKET" --prefix tutorial/ --query "'Contents[].Key'" --output table |
| 57 | + |
| 58 | +echo "" |
| 59 | +echo "--- Step 6: Clean up tutorial objects ---" |
| 60 | +run_cmd aws s3 rm "s3://$BUCKET/tutorial/" --recursive |
| 61 | +rm -f /tmp/s3-tut-test.txt /tmp/s3-tut-download.txt |
| 62 | +echo "Bucket remains for other tutorials." |
0 commit comments