-
Notifications
You must be signed in to change notification settings - Fork 20
/
find-ami
executable file
·150 lines (128 loc) · 3.62 KB
/
find-ami
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
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# find-ami - Find the latest AMI based on region, description, name, etc.
#
# Author: Michal Ludvig <[email protected]>
# License: BSD
# Homepage: https://aws.nz/aws-utils/find-ami
function show_help() {
cat << __EOF__
Usage: $0 [-h] [options]
Purge AMI (Images) older than a given date, optionally matching certain
filters.
-d AMI-ID Describe AMI details (Name, Owner, etc)
-l List all AMIs matching the NAME and OWNER.
By default only the newest one is returned.
-n NAME AMI Name, can be regexp.
E.g: amzn-ami-hvm-[^-]*-gp2
-o OWNER AMI Owner, can be 'amazon' or numeric owner ID.
-p PROFILE AWS-CLI Profile name as defined in ~/.aws/config.
Optional, will use default or instance meta-data if
not specified.
-r REGION AWS Region where to look for the AMI.
-t VIRT_TYPE Virtualization type:
* hvm (default)
* paravirtual
-x PRESET Name/Description/Owner preset for common AMIs:
* amazon-linux
* bitnami-wordpress
You can override preset NAME with -n
-h Display help.
Visit https://aws.nz/aws-utils/find-ami for more info and examples.
__EOF__
exit 1
}
function fatal() {
echo $1 >&2
exit 1
}
AWS=$(which aws 2>/dev/null)
test -z "${AWS}" && fatal "The 'aws' utility not found. Please install it or add its location to \$PATH"
JQ=$(which jq 2>/dev/null)
test -z "${JQ}" && fatal "The 'jq' utility not found. Please install it or add its location to \$PATH"
VIRT_TYPE="hvm"
LIST_ALL=0
while getopts "d:ln:o:p:r:x:h" OPT; do
case "${OPT}" in
d)
DESCRIBE_AMI="${OPTARG}"
;;
l)
LIST_ALL=1
;;
n)
NAME="${OPTARG}"
# Escape "()" to "\(\)"
NAME="${NAME//\(/\\\\(}"
NAME="${NAME//\)/\\\\)}"
;;
o)
OWNER="${OPTARG}"
;;
p)
AWS="${AWS} --profile=${OPTARG}"
;;
r)
AWS="${AWS} --region=${OPTARG}"
;;
t)
VIRT_TYPE="${OPTARG}"
;;
x)
PRESET="${OPTARG}"
;;
h)
show_help
;;
esac
done
if [ -n "${DESCRIBE_AMI}" ]; then
IMAGES_JSON=$(${AWS} ec2 describe-images --filter Name=image-id,Values=${DESCRIBE_AMI})
${JQ} -r ".Images[] |
@text \"OWNER: \(.OwnerId)\",
@text \"NAME: \(.Name)\",
@text \"DESC: \(.Description)\",
@text \"VIRT: \(.VirtualizationType)\"
" <<< ${IMAGES_JSON}
exit 0
fi
if [ -n "${PRESET}" ]; then
case "${PRESET}" in
amazon-linux)
OWNER=${OWNER:=amazon}
NAME=${NAME:=amzn-ami-[^-]*-[^-]*-gp2}
;;
redhat-el7)
OWNER=${OWNER:=309956199498}
NAME=${NAME:=RHEL-7.*_HVM.*x86_64.*Hourly2-GP2}
;;
bitnami-wordpress)
OWNER=${OWNER:=679593333241}
NAME=${NAME:=bitnami-wordpress-[0-9.-]+-linux-ubuntu-.*-ebs}
;;
*)
fatal "Preset ID '${PRESET}' isn't configured."
;;
esac
fi
echo "# Listing AMIs from owner ${OWNER} matching pattern: ${NAME}"
echo "# Please wait, this may take a long time..."
IMAGES_JSON=$(${AWS} ec2 describe-images --owners=${OWNER} --filter Name=virtualization-type,Values=${VIRT_TYPE})
if [ "${LIST_ALL}" = "1" ]; then
# List all matching AMIs
${JQ} -r "[.Images[]
| select(.Name | test(\"${NAME}\"))
] | sort_by(.CreationDate) | reverse | .[] |
@text \"\(.ImageId) \(.CreationDate) \(.Name)\"
" <<< ${IMAGES_JSON}
else
# Sort AMIs by date and display only the newest one
${JQ} -r "[.Images[]
| select(.Name | test(\"${NAME}\"))
] | max_by(.CreationDate) |
@text \"export AMI_ID='\(.ImageId)'\",
@text \"export AMI_NAME='\(.Name)'\",
@text \"export AMI_OWNER='\(.OwnerId)'\",
@text \"export AMI_DESCRIPTION='\(.Description)'\",
@text \"export AMI_VIRT_TYPE='\(.VirtualizationType)'\"
" <<< ${IMAGES_JSON}
fi