-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_instances.sh
62 lines (54 loc) · 1.78 KB
/
control_instances.sh
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
55
56
57
58
59
60
61
62
#!/bin/bash
# Check if the awscli is installed
if ! command -v aws &>/dev/null; then
echo "AWS CLI not found. Please install and configure the AWS CLI before running this script."
exit 1
fi
# Prompt the user for the instance name and region
read -p "Enter the instance name: " INSTANCE_NAME
read -p "Enter the AWS region (e.g., us-east-1): " AWS_REGION
# Function to start the EC2 instance
start_instance() {
aws ec2 start-instances --instance-ids "$1" --region "$2"
}
# Function to stop the EC2 instance
stop_instance() {
aws ec2 stop-instances --instance-ids "$1" --region "$2"
}
# Function to terminate the EC2 instance
terminate_instance() {
aws ec2 terminate-instances --instance-ids "$1" --region "$2"
}
# Get the instance ID based on the provided instance name and region
instance_id=$(aws ec2 describe-instances \
--region "$AWS_REGION" \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
--query "Reservations[].Instances[0].InstanceId" \
--output text
)
# Check if the instance ID is empty (no instance found with the provided name)
if [ -z "$instance_id" ]; then
echo "No EC2 instance found with the name $INSTANCE_NAME in the $AWS_REGION region."
else
echo "EC2 instance with ID $instance_id found."
read -p "Enter the action (start, stop, terminate): " ACTION
# Perform the chosen action
case "$ACTION" in
"start")
start_instance "$instance_id" "$AWS_REGION"
echo "Starting the EC2 instance..."
;;
"stop")
stop_instance "$instance_id" "$AWS_REGION"
echo "Stopping the EC2 instance..."
;;
"terminate")
terminate_instance "$instance_id" "$AWS_REGION"
echo "Terminating the EC2 instance..."
;;
*)
echo "Invalid action. Please choose 'start', 'stop', or 'terminate'."
exit 1
;;
esac
fi