-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-all-instances.sh
More file actions
executable file
·54 lines (42 loc) · 1.57 KB
/
cleanup-all-instances.sh
File metadata and controls
executable file
·54 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Cleanup script to terminate all EC2 instances
# Use with caution - this will terminate ALL instances in the region!
set -e
echo "🧹 Starting EC2 cleanup process..."
# Get all instance IDs
INSTANCE_IDS=$(aws ec2 describe-instances \
--region eu-west-1 \
--query 'Reservations[*].Instances[*].InstanceId' \
--output text \
--filters Name=instance-state-name,Values=running,pending,stopping,stopped)
if [ -z "$INSTANCE_IDS" ]; then
echo "✅ No instances found to terminate"
exit 0
fi
echo "🔍 Found instances to terminate:"
for instance_id in $INSTANCE_IDS; do
# Get instance details
INSTANCE_INFO=$(aws ec2 describe-instances \
--region eu-west-1 \
--instance-ids "$instance_id" \
--query 'Reservations[0].Instances[0].[InstanceId,InstanceType,State.Name,Tags[?Key==`Name`].Value|[0]]' \
--output text)
echo " - $INSTANCE_INFO"
done
echo ""
echo "⚠️ WARNING: This will terminate ALL instances listed above!"
echo "Press Enter to continue or Ctrl+C to cancel..."
read
echo "🔥 Terminating instances..."
aws ec2 terminate-instances \
--region eu-west-1 \
--instance-ids $INSTANCE_IDS
echo "✅ Termination requests sent for all instances"
echo "🕐 Instances are now shutting down..."
# Wait for all instances to reach terminated state
echo "⏳ Waiting for instances to terminate..."
aws ec2 wait instance-terminated \
--region eu-west-1 \
--instance-ids $INSTANCE_IDS
echo "🎉 All instances have been successfully terminated!"
echo "💰 Cleanup complete - no more EC2 charges!"