#!/bin/bash set -e set -o pipefail # ----- Config ----- BUCKET_NAME=${1:-customersupport} INFRA_STACK_NAME=${2:-CustomerSupportStackInfra} COGNITO_STACK_NAME=${3:-CustomerSupportStackCognito} REGION=$(aws configure get region) ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) FULL_BUCKET_NAME="${BUCKET_NAME}-${ACCOUNT_ID}" ZIP_FILE="lambda.zip" S3_KEY="lambda.zip" # ----- Confirm Deletion ----- read -p "โš ๏ธ Are you sure you want to delete stacks '$INFRA_STACK_NAME', '$COGNITO_STACK_NAME' and clean up S3? (y/N): " confirm if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then echo "โŒ Cleanup cancelled." exit 1 fi # ----- 1. Delete CloudFormation stacks ----- echo "๐Ÿงจ Deleting stack: $INFRA_STACK_NAME..." aws cloudformation delete-stack --stack-name "$INFRA_STACK_NAME" --region "$REGION" echo "โณ Waiting for $INFRA_STACK_NAME to be deleted..." aws cloudformation wait stack-delete-complete --stack-name "$INFRA_STACK_NAME" --region "$REGION" echo "โœ… Stack $INFRA_STACK_NAME deleted." echo "๐Ÿงจ Deleting stack: $COGNITO_STACK_NAME..." aws cloudformation delete-stack --stack-name "$COGNITO_STACK_NAME" --region "$REGION" echo "โณ Waiting for $COGNITO_STACK_NAME to be deleted..." aws cloudformation wait stack-delete-complete --stack-name "$COGNITO_STACK_NAME" --region "$REGION" echo "โœ… Stack $COGNITO_STACK_NAME deleted." # ----- 2. Delete zip file from S3 ----- echo "๐Ÿงน Deleting all contents of s3://$FULL_BUCKET_NAME..." aws s3 rm "s3://$FULL_BUCKET_NAME" --recursive || echo "โš ๏ธ Failed to clean bucket or it is already empty." # ----- 3. Optionally delete the bucket ----- read -p "๐Ÿชฃ Do you want to delete the bucket '$FULL_BUCKET_NAME'? (y/N): " delete_bucket if [[ "$delete_bucket" == "y" || "$delete_bucket" == "Y" ]]; then echo "๐Ÿšฎ Deleting bucket $FULL_BUCKET_NAME..." aws s3 rb "s3://$FULL_BUCKET_NAME" --force echo "โœ… Bucket deleted." else echo "๐Ÿชฃ Bucket retained: $FULL_BUCKET_NAME" fi # ----- 4. Clean up local zip file ----- echo "๐Ÿ—‘๏ธ Removing local file $ZIP_FILE..." rm -f "$ZIP_FILE" echo "๐Ÿ—‘๏ธ Deleting Knowledgebase" python prerequisite/knowledge_base.py --mode delete echo "โœ… Cleanup complete."