|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +if [ -z "${IS_CI}" ]; then |
| 4 | + echo "Error: This script is intended to run only in CI environments." |
| 5 | + echo "Running it in your account may have destructive effects." |
| 6 | + echo "Set the IS_CI environment variable to run this script." |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +REGIONS=("us-east-1" "us-east-2") |
| 11 | + |
| 12 | +# Region and cluster ID can be extracted from ARN |
| 13 | +# ARN format: arn:aws:dsql:REGION:ACCOUNT:cluster/CLUSTER_ID |
| 14 | +declare -a ARNS=() |
| 15 | +declare -a FILTERED_ARNS=() |
| 16 | + |
| 17 | +# Get clusters from each region and extract ARNs |
| 18 | +for region in "${REGIONS[@]}"; do |
| 19 | + echo "Listing clusters in $region..." |
| 20 | + |
| 21 | + region_arns=$(aws dsql list-clusters --region "$region" | jq -r '.clusters[].arn') |
| 22 | + |
| 23 | + # Add ARNs to the array if any were found |
| 24 | + if [ -n "$region_arns" ]; then |
| 25 | + while IFS= read -r arn; do |
| 26 | + ARNS+=("$arn") |
| 27 | + done <<< "$region_arns" |
| 28 | + fi |
| 29 | +done |
| 30 | + |
| 31 | +echo -e "\nFound ${#ARNS[@]} cluster(s) across all regions:" |
| 32 | +printf '%s\n' "${ARNS[@]}" |
| 33 | + |
| 34 | +echo -e "\nFiltering clusters..." |
| 35 | +for arn in "${ARNS[@]}"; do |
| 36 | + region=$(echo "$arn" | cut -d':' -f4) |
| 37 | + cluster_id=$(echo "$arn" | cut -d'/' -f2) |
| 38 | + |
| 39 | + echo "Checking cluster $cluster_id in region $region..." |
| 40 | + |
| 41 | + cluster_details=$(aws dsql get-cluster --region "$region" --identifier "$cluster_id") |
| 42 | + |
| 43 | + status=$(echo "$cluster_details" | jq -r '.status') |
| 44 | + repo_tag=$(echo "$cluster_details" | jq -r '.tags.Repo // empty') |
| 45 | + |
| 46 | + # We only want clusters that are not already deleting, and have the specific repo tag |
| 47 | + if [[ "$status" != "DELETED" && "$status" != "DELETING" && "$repo_tag" == "aws-samples/aurora-dsql-samples" ]]; then |
| 48 | + echo "Cluster $cluster_id qualifies for update (Status: $status, Repo tag: $repo_tag)" |
| 49 | + FILTERED_ARNS+=("$arn") |
| 50 | + else |
| 51 | + echo "Skipping cluster $cluster_id (Status: $status, Repo tag: $repo_tag)" |
| 52 | + fi |
| 53 | +done |
| 54 | + |
| 55 | +echo -e "\nFound ${#FILTERED_ARNS[@]} cluster(s) that will be updated and deleted:" |
| 56 | +printf '%s\n' "${FILTERED_ARNS[@]}" |
| 57 | + |
| 58 | +# Early exit if no clusters to update |
| 59 | +if [ ${#FILTERED_ARNS[@]} -eq 0 ]; then |
| 60 | + echo -e "\nNo clusters to update or delete. Exiting." |
| 61 | + exit 0 |
| 62 | +fi |
| 63 | + |
| 64 | +echo -e "\nUpdating filtered clusters to disable deletion protection..." |
| 65 | +for arn in "${FILTERED_ARNS[@]}"; do |
| 66 | + region=$(echo "$arn" | cut -d':' -f4) |
| 67 | + cluster_id=$(echo "$arn" | cut -d'/' -f2) |
| 68 | + |
| 69 | + echo "Updating cluster $cluster_id in region $region..." |
| 70 | + aws dsql update-cluster --region "$region" --identifier "$cluster_id" --no-deletion-protection-enabled |
| 71 | + echo "Cluster $cluster_id updated successfully." |
| 72 | +done |
| 73 | + |
| 74 | +echo -e "\nDeleting filtered clusters..." |
| 75 | +for arn in "${FILTERED_ARNS[@]}"; do |
| 76 | + region=$(echo "$arn" | cut -d':' -f4) |
| 77 | + cluster_id=$(echo "$arn" | cut -d'/' -f2) |
| 78 | + |
| 79 | + echo "Deleting cluster $cluster_id in region $region..." |
| 80 | + aws dsql delete-cluster --region "$region" --identifier "$cluster_id" |
| 81 | + echo "Deletion initiated for cluster $cluster_id." |
| 82 | +done |
0 commit comments