forked from lalyos/bash-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-functions
98 lines (79 loc) · 2.67 KB
/
ec2-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
_getQuery() {
# you can choose fields from the output json:
# http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#output
local fields="id: InstanceId"
#fields="$fields, ami: ImageId"
fields="$fields, pubIp: PublicIpAddress"
fields="$fields, privIp: PrivateIpAddress"
#fields="$fields, state: State.Name"
fields="$fields, idx: AmiLaunchIndex"
fields="$fields"', name: join(``, Tags[?Key==`Name`].Value || `[]`)'
#fields="$fields"', owner: join(``, Tags[?Key==`owner`].Value || `[]`)'
#fields="$fields"', tagz: join(`,`,Tags[].join(`=`,[Key,Value] || `[]`) || `[]`)'
# simple fields example
#fields="$fields"', xxx: yyy'
# field contains jmespath literal
#fields="$fields"', xxx: `literal`'
echo "Reservations[].Instances[].{${fields}}"
}
ec2-list-all() {
local filter=$@
aws ec2 describe-instances $filter --query "$(_getQuery)" --out table
}
ec2-list-filtered() {
local filterName=${1?:" first param REQUIRED: <filtername> example: instance-state-name"}
local filterValue=${2:?" second param RREQUIRED: <values> example: running"}
ec2-list-all --filters Name=$filterName,Values=$filterValue
}
ec2-list-running() {
ec2-list-filtered instance-state-name running
}
ec2-list-by-name() {
ec2-list-filtered tag:Name ${1:?"first param REQUIRED: <name>"}
}
ec2-list-by-owner() {
ec2-list-filtered tag:owner ${1:?"first param REQUIRED: <owner>"}
}
ec2-list-by-tagvalue() {
ec2-list-filtered
}
ec2-regions() {
aws ec2 describe-regions --query "Regions[].RegionName" --out text
}
ec2-list-self-images() {
: ${AWS_OWNER:=755047402263}
for reg in $(ec2-regions); do
aws ec2 describe-images \
--region $reg \
--filters Name=owner-id,Values=$AWS_OWNER \
--query "Images[].[ImageId,Name]" \
--out text | xargs -n 2 echo $reg
done
}
ec2-clean-images() {
local amiWhiteList="ci-2014-06-25\|ci-sequenceiq\|ambari-docker-140716083934"
ec2-list-self-images | grep -v "$amiWhiteList"|xargs -n 3 echo ec2-delete-ami
}
ec2-delete-ami() {
local region=$1
local ami=$2
: ${region:? requeired 1. parameter}
: ${ami:? requeired 2. parameter}
local name=$(aws ec2 describe-images \
--region $region \
--image-ids $ami \
--query "Images[].Name" \
--out text
)
local snap=$(aws ec2 describe-images \
--region $region \
--image-ids $ami \
--query "Images[].BlockDeviceMappings[].Ebs.SnapshotId" \
--out text
)
echo [ec2-deregister-image] $name
aws ec2 deregister-image --region $region --image-id $ami --out text
echo [ec2-delete-snapshot] $snap
aws ec2 delete-snapshot --region $region --snapshot-id $snap --out text
}
alias ec2-reload-functions='. ~/apps/bin/ec2-functions'