Skip to content

Commit f71a91e

Browse files
Clean up lingering clusters after test runs
1 parent 1120c21 commit f71a91e

4 files changed

Lines changed: 143 additions & 1 deletion

File tree

.github/scripts/clean-clusters.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Clean up Aurora DSQL Clusters
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
aws_region:
7+
required: false
8+
type: string
9+
default: 'us-east-1'
10+
description: 'Default AWS region for credentials, does not limit access to other regions'
11+
secrets:
12+
AWS_IAM_ROLE:
13+
required: true
14+
description: 'AWS IAM role to assume for cluster cleanup'
15+
16+
jobs:
17+
cleanup:
18+
name: Clean up Aurora DSQL Clusters
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 10
21+
permissions:
22+
id-token: write
23+
contents: read
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Configure AWS Credentials
30+
uses: aws-actions/configure-aws-credentials@v4
31+
with:
32+
role-to-assume: ${{ secrets.AWS_IAM_ROLE }}
33+
aws-region: ${{ inputs.aws_region }}
34+
35+
- name: Run cluster cleanup script
36+
env:
37+
IS_CI: "true"
38+
run: .github/scripts/clean-clusters.sh

.github/workflows/dotnet-cm-integ-tests.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Dotnet cluster management integration tests
1+
name: Dotnet cluster management integration tests
22

33
on:
44
push:
@@ -52,3 +52,14 @@ jobs:
5252
run: |
5353
dotnet restore
5454
dotnet test
55+
56+
cleanup:
57+
needs: test
58+
uses: ./.github/workflows/clean-clusters.yml
59+
with:
60+
aws_region: 'us-east-1'
61+
secrets:
62+
AWS_IAM_ROLE: ${{ secrets.DOTNET_IAM_ROLE }}
63+
permissions:
64+
id-token: write
65+
contents: read

.github/workflows/rust-cm-integ-tests.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ jobs:
6565
working-directory: ./rust/cluster_management
6666
run: |
6767
cargo test -- --nocapture
68+
69+
cleanup:
70+
needs: test
71+
uses: ./.github/workflows/clean-clusters.yml
72+
with:
73+
aws_region: 'us-east-1'
74+
secrets:
75+
AWS_IAM_ROLE: ${{ secrets.RUST_IAM_ROLE }}
76+
permissions:
77+
id-token: write
78+
contents: read

0 commit comments

Comments
 (0)