#!/bin/bash # Enterprise CI/CD Foundation Cleanup Script # This script removes the bootstrap S3 bucket and DynamoDB table # Run this AFTER terraform destroy to completely clean up set -e echo "๐Ÿงน Foundation Layer Cleanup Script" # Load configuration if available if [ -f .backend-config ]; then echo "๐Ÿ“‹ Loading configuration from .backend-config..." source .backend-config else echo "โš ๏ธ No .backend-config found. Using environment variables..." BUCKET_NAME="${BUCKET_NAME:-}" TABLE_NAME="${TABLE_NAME:-}" AWS_REGION="${AWS_REGION:-us-east-1}" PROJECT_NAME="${PROJECT_NAME:-enterprise-cicd}" ENVIRONMENT="${ENVIRONMENT:-dev}" fi # Verify AWS credentials echo "๐Ÿ” Verifying AWS credentials..." aws sts get-caller-identity || { echo "โŒ AWS credentials not configured or invalid" exit 1 } # Interactive confirmation echo "" echo "โš ๏ธ WARNING: This will permanently delete bootstrap resources!" echo "" echo "Resources to delete:" echo " S3 Bucket: ${BUCKET_NAME}" echo " DynamoDB Table: ${TABLE_NAME}" echo " Region: ${AWS_REGION}" echo "" read -p "Are you sure you want to proceed? (type 'DELETE' to confirm): " confirmation if [ "$confirmation" != "DELETE" ]; then echo "โŒ Cleanup cancelled" exit 1 fi echo "" echo "๐Ÿ’€ Starting cleanup process..." # Check if Terraform state still exists if [ -f terraform.tfstate ] || [ -f .terraform/terraform.tfstate ]; then echo "โŒ Error: Terraform state files still exist!" echo "Please run 'terraform destroy' first to destroy all infrastructure" echo "Then run this cleanup script to remove bootstrap resources" exit 1 fi # Check if S3 bucket contains state files if [ -n "${BUCKET_NAME}" ] && aws s3api head-bucket --bucket "${BUCKET_NAME}" 2>/dev/null; then STATE_FILES=$(aws s3 ls "s3://${BUCKET_NAME}/foundation/" --recursive 2>/dev/null || echo "") if [ -n "${STATE_FILES}" ]; then echo "โŒ Error: S3 bucket contains Terraform state files!" echo "Found state files:" echo "${STATE_FILES}" echo "" echo "Please run 'terraform destroy' first to clean up all infrastructure" echo "This will remove the state files from S3" exit 1 fi fi # Remove S3 bucket if [ -n "${BUCKET_NAME}" ] && aws s3api head-bucket --bucket "${BUCKET_NAME}" 2>/dev/null; then echo "๐Ÿ—‘๏ธ Removing S3 bucket: ${BUCKET_NAME}" # Remove all objects and versions echo " Removing all objects and versions..." aws s3api list-object-versions --bucket "${BUCKET_NAME}" \ --query 'Versions[].[Key,VersionId]' --output text | \ while read key version; do if [ -n "$key" ] && [ -n "$version" ]; then aws s3api delete-object --bucket "${BUCKET_NAME}" --key "$key" --version-id "$version" fi done # Remove delete markers aws s3api list-object-versions --bucket "${BUCKET_NAME}" \ --query 'DeleteMarkers[].[Key,VersionId]' --output text | \ while read key version; do if [ -n "$key" ] && [ -n "$version" ]; then aws s3api delete-object --bucket "${BUCKET_NAME}" --key "$key" --version-id "$version" fi done # Delete the bucket aws s3api delete-bucket --bucket "${BUCKET_NAME}" --region "${AWS_REGION}" echo "โœ… S3 bucket ${BUCKET_NAME} deleted" else echo "โ„น๏ธ S3 bucket ${BUCKET_NAME} not found or already deleted" fi # Remove DynamoDB table if [ -n "${TABLE_NAME}" ] && aws dynamodb describe-table --table-name "${TABLE_NAME}" --region "${AWS_REGION}" >/dev/null 2>&1; then echo "๐Ÿ—‘๏ธ Removing DynamoDB table: ${TABLE_NAME}" aws dynamodb delete-table --table-name "${TABLE_NAME}" --region "${AWS_REGION}" # Wait for deletion to complete echo "โณ Waiting for table deletion to complete..." aws dynamodb wait table-not-exists --table-name "${TABLE_NAME}" --region "${AWS_REGION}" echo "โœ… DynamoDB table ${TABLE_NAME} deleted" else echo "โ„น๏ธ DynamoDB table ${TABLE_NAME} not found or already deleted" fi # Clean up local files echo "๐Ÿงน Cleaning up local files..." rm -f backend.tf rm -f .backend-config rm -f terraform.tfstate.backup rm -f .terraform.lock.hcl rm -rf .terraform/ echo "" echo "๐ŸŽ‰ Cleanup completed successfully!" echo "" echo "๐Ÿ“‹ What was removed:" echo " โœ… S3 bucket: ${BUCKET_NAME}" echo " โœ… DynamoDB table: ${TABLE_NAME}" echo " โœ… Local backend configuration files" echo "" echo "๐Ÿ’ก You can now run the bootstrap script again to create new resources"