-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-terminate-ec2.sh
More file actions
38 lines (30 loc) · 1.04 KB
/
5-terminate-ec2.sh
File metadata and controls
38 lines (30 loc) · 1.04 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
#!/bin/bash
# Configuration
REGION=$1
INSTANCE_NAME=$2
if [ -z "$REGION" ] || [ -z "$INSTANCE_NAME" ]; then
echo "Usage: $0 <REGION> <INSTANCE_NAME>"
exit 1
fi
echo "Retrieving ID for instance: $INSTANCE_NAME..."
# 1. Get Instance ID
INSTANCE_ID=$(aws ec2 describe-instances \
--region $REGION \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=running,stopped,stopping,pending" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text)
if [ -z "$INSTANCE_ID" ]; then
echo "Error: Could not find Instance ID for $INSTANCE_NAME. It may already be terminated."
exit 0
fi
# 2. Terminate the Instance
echo "Terminating instance $INSTANCE_ID..."
aws ec2 terminate-instances \
--region $REGION \
--instance-ids $INSTANCE_ID
# 3. Wait for 'terminated' state
echo "Waiting for instance to reach 'terminated' state..."
aws ec2 wait instance-terminated \
--region $REGION \
--instance-ids $INSTANCE_ID
echo "Success: Instance $INSTANCE_NAME ($INSTANCE_ID) has been terminated."