forked from bash-my-aws/bash-my-aws
/
instance-functions
137 lines (119 loc) · 5.23 KB
/
instance-functions
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# instance-functions.sh
#
# List, run, start, stop and ssh to Amazon AWS EC2 instances
# Fields to include when listing EC2 Instances
INSTANCE_OUTPUT=${BMA_INSTANCE_OUTPUT:-"[InstanceId, State.Name, StateTransitionReason, InstanceType, PrivateIpAddress, PublicIpAddress, join(\` \`, [Tags[?Key==\`Name\`].Value][]), join(\` \`, [Tags[?Key==\`contact\`].Value][]), LaunchTime]"}
#
# This can be overridden to customise output
#
# Example of ENV setting to generate CSV output
#
# INSTANCE_OUTPUT='[join(`,`, [InstanceId,State.Name])]'
# Fields to include when listing EBS volumes
VOLUME_OUTPUT='[VolumeId, Size, VolumeType, State, CreateTime]'
instances() {
if [ -n "$1" ]
then
local filters="--filters Name=tag:Name,Values=${1}"
fi
aws ec2 describe-instances $filters --query "Reservations[].Instances[].${INSTANCE_OUTPUT}" --output text
}
instances_running() {
aws ec2 describe-instances --query "Reservations[].Instances[?State.Name==\`running\`].$INSTANCE_OUTPUT" --output text
}
instances_stopped() {
aws ec2 describe-instances --query "Reservations[].Instances[?State.Name==\`stopped\`].$INSTANCE_OUTPUT" --output text
}
instances_with_tag() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME tag_key [tag_value]"; return 1; fi
local tag_name=$1
local tag_value=$2
if [ -z "$tag_value" ]; then
aws ec2 describe-instances --output text --query "Reservations[].Instances[?contains(Tags[].Key[], \`$tag_name\`) == \`true\`][].${INSTANCE_OUTPUT}"
else
aws ec2 describe-instances --output text --query "Reservations[].Instances[?contains(Tags[?Key==\`$tag_key\`].Value[], \`$tag_value\`) == \`true\`][].${INSTANCE_OUTPUT}"
fi
}
instances_without_tag() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME tag_key [tag_value]"; return 1; fi
local tag_key=$1
local tag_value=$2
if [ -z "$tag_value" ]; then
aws ec2 describe-instances --output text --query "Reservations[].Instances[?contains(Tags[].Key[], \`$tag_key\`) != \`true\`][].${INSTANCE_OUTPUT}"
else
aws ec2 describe-instances --output text --query "Reservations[].Instances[?contains(Tags[?Key==\`$tag_key\`].Value[], \`$tag_value\`) != \`true\`][].${INSTANCE_OUTPUT}"
fi
}
instance_types() {
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query "Reservations[].Instances[].[InstanceType]" --output text | sort | uniq -c
}
instance_volumes() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance_id [instance_id]"; return 1; fi
local instance_ids=$@
if local volume_ids=$(aws ec2 describe-instances --instance-ids $instance_ids --query "Reservations[].Instances[].BlockDeviceMappings[].Ebs[].VolumeId" --output text)
then
aws ec2 describe-volumes --volume-ids $volume_ids --query "Volumes[].$VOLUME_OUTPUT" --output text
else
>&2 echo "No volumes found"
fi
}
instance_userdata() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance-id"; return 1; fi
local instance_id=$1
aws ec2 describe-instance-attribute --attribute userData --instance-id $instance_id --query UserData --output text | base64 --decode
}
instance_console_output() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance_id"; return 1; fi
local instance_id=$1
aws ec2 get-console-output --instance-id $instance_id --query Output --output text
}
instance_role() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance_id"; return 1; fi
local instance_id=$1
local iam_instance_profile_id=$(aws ec2 describe-instances --instance-id $instance_id --query "Reservations[].Instances[].IamInstanceProfile.Id" --output text)
aws iam list-instance-profiles --query "InstanceProfiles[?InstanceProfileId==\`$iam_instance_profile_id\`].Roles[].RoleName" --output text
}
instance_ssh_details() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [template]"; return 1; fi
local instance_id=$1
aws ec2 describe-instances --filters Name=instance-id,Values=$instance_id --query "
Reservations[].Instances[0].[
KeyName,
join(\` \`, [Tags[?Key==\`default-user\`].Value][] || [\`root\`])
PrivateIpAddress,
join(\` \`, [Tags[?Key==\`Name\`].Value][] || [\`not-named\`])
]" --output text
}
instance_ssh() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance-id [user]"; return 1; fi
local instance_id=$1
local user=$2
local instance_details=$(instance_ssh_details $instance_id)
local keyname=$(echo "$instance_details" | awk '{print $1}');
local default_user=$(echo "$instance_details" | awk '{print $2}');
local ip=$(echo "$instance_details" | awk '{print $3}');
local instance_name=$(echo "$instance_details" | awk '{print $4}');
local USERNAME=${user:-$default_user}
echo "Connecting to $instance_id $instance_name"
ssh \
-t \
-i ~/.ssh/$keyname \
-o LogLevel=quiet \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-l $USERNAME \
$ip
}
instance_start() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance-id"; return 1; fi
aws ec2 start-instances --instance-id $1
}
instance_stop() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME instance-id"; return 1; fi
aws ec2 stop-instances --instance-id $1
}
instance_terminate() {
aws ec2 modify-instance-attribute --attribute disableApiTermination --value false --instance-id $1
aws ec2 terminate-instances --instance-id $1
}