-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws-cli-scripts.sh
executable file
·92 lines (71 loc) · 2.16 KB
/
aws-cli-scripts.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
print_help() {
cat <<EOF
USAGE: $0 [Options]
Use the following options:
list)
lists all instances created using the .pem file from terraform script.
start)
Starts the EC2 instances listed above.
stop)
Stops the EC2 instances listed above.
Examples:
$0 list <profilename>
lists instances
$0 start <profilename>
starts instances
$0 stop <profilename>
stops instances
EOF
exit 0
}
if [[ $# -lt 2 ]]; then
echo ""
echo "Script needs 2 arguments. See the help section below"
print_help
exit 0
fi
export awsi="aws ec2 describe-instances --profile $2 --query Reservations[].Instances[]"
export ot="--output text"
instances=($(aws ec2 describe-instances --filters "Name=key-name, Values=kubernetes-terraform" --query "Reservations[].Instances[].InstanceId" --filters Name=instance-state-name,Values=running --output text --profile $2))
stopped_instances=($(aws ec2 describe-instances --filters "Name=key-name, Values=kubernetes-terraform" --query "Reservations[].Instances[].InstanceId" --filters Name=instance-state-name,Values=stopped --output text --profile $2))
list_instance () {
echo " "
for i in ${instances[@]}; do
echo $i $($awsi.State[].Name --instance-ids $i $ot) $($awsi.Placement.AvailabilityZone --instance-ids $i $ot) $($awsi.PublicIpAddress --instance-ids $i $ot) $($awsi.Tags[].Value --instance-ids $i $ot)| column -t -o " | "
done
echo " "
}
stop_instance() {
for i in ${instances[@]}; do
echo "Stopping instance $i"
aws ec2 stop-instances --instance-ids $i --profile $1 &> /dev/null
done
if [[ $? -eq 0 ]]; then
echo "Stopped instances"
fi
}
start_instance() {
for i in ${stopped_instances[@]}; do
echo "Starting instance $i"
aws ec2 start-instances --instance-ids $i --profile $1 &> /dev/null
done
if [[ $? -eq 0 ]]; then
echo "Instance started"
echo "list instances using $0 list"
fi
}
case $1 in
list)
list_instance $2
;;
start)
start_instance $2
;;
stop)
stop_instance $2
;;
*)
print_help
;;
esac