forked from bash-my-aws/bash-my-aws
/
cloudformation-functions
282 lines (257 loc) · 9.47 KB
/
cloudformation-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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/bash
# cloudformation-functions.sh
#
# Create, List, Update, Delete (plus diff, tail, etc) CF stacks
#
# Requires awscli (and jq-1.4 or later for cf_diff)
#
# Hint: Naming templates as [stack_name + '.json'] will allow
# you to use tab completion to provide stack name. The functions
# will drop the '.json' if present in the stack argument.
# List CF stacks. Optionally filter on stack name.
# e.g.
# cf_list foo
#
# To make it fly we omit stacks with status of DELETE_COMPLETE
cf_list() {
aws cloudformation list-stacks \
--stack-status \
CREATE_COMPLETE \
CREATE_FAILED \
CREATE_IN_PROGRESS \
DELETE_FAILED \
DELETE_IN_PROGRESS \
ROLLBACK_COMPLETE \
ROLLBACK_FAILED \
ROLLBACK_IN_PROGRESS \
UPDATE_COMPLETE \
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS \
UPDATE_IN_PROGRESS \
UPDATE_ROLLBACK_COMPLETE \
UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS \
UPDATE_ROLLBACK_FAILED \
UPDATE_ROLLBACK_IN_PROGRESS \
--query "StackSummaries[].StackName" \
--output text | tr '\t' '\n' | sort | grep "$1"
}
alias cf_stacks=cf_list
# Report on what changes would be made to stack by applying template
#
# Requires jq-1.4 or later # http://stedolan.github.io/jq/download/
#
cf_diff() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [template]"; return 1; fi
local stack="$(basename $1 .json)"
if ! aws cloudformation describe-stacks --stack-name $stack 2>/dev/null 1>/dev/null; then
echo "Stack: $stack does not exist"
return 1;
fi
local template="$(cf_template $stack $2)"
if [ ! -f "$template" ]; then
echo "Could not find ${stack}.json. You can specify alternative template as second argument."
return 1
fi
if [ "x$( type -P colordiff )" != "x" ]; then
local DIFF_CMD=colordiff
else
local DIFF_CMD=diff
fi
$DIFF_CMD -u <(aws cloudformation get-template --stack-name $stack --query TemplateBody | jq --sort-keys .) <(jq --sort-keys . $template)
if [ $? -eq 0 ]; then
echo "no difference"
fi
}
# Show all events for CF stack until update completes or fails.
cf_tail() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
local current
local final_line
local output
local previous
until echo "$current" | tail -1 | egrep -q "${stack}.*_(COMPLETE|FAILED)"
do
if ! output=$(cf_events $stack); then
# Something went wrong with cf_events (like stack not known)
return 1
fi
if [ -z "$output" ]; then sleep 1; continue; fi
current=$(echo "$output" | sed '$d')
final_line=$(echo "$output" | tail -1)
if [ -z "$previous" ]; then
echo "$current"
elif [ "$current" != "$previous" ]; then
comm -13 <(echo "$previous") <(echo "$current")
fi
previous="$current"
sleep 1
done
echo $final_line
}
cf_events() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
shift
local output
if output=$(aws cloudformation describe-stack-events --stack-name $stack --query 'sort_by(StackEvents, &Timestamp)[].{Resource: LogicalResourceId, Type: ResourceType, Status: ResourceStatus}' --output table $@); then
echo "$output" | uniq -u
else
return $?
fi
}
cf_status() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].StackStatus" --output text
}
cf_get() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
aws cloudformation get-template --stack-name $stack --query TemplateBody
}
cf_template() {
local stack="$(basename $1 .json)"
local template=$2
if [ -z "$template" ]; then
if [ -f "${stack}.json" ]; then
template="${stack}.json"
elif [ -f "${stack%-*}.json" ]; then
template="${stack%-*}.json"
fi
fi
echo $template
}
cf_create() {
# XXX accept params from arguments
# XXX show account_alias_or_id
# XXX confirm if not -f
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [template-file] [params-file]"; return 1; fi
local stack="$(basename $1 .json)"
local template="$(cf_template $stack $2)"
if [ ! -f "$template" ]; then
echo "Could not find ${stack}.json. You can specify alternative template as second argument."
return 1
fi
if [ -n "$3" ] ; then
local parameters="--parameters file://$3";
fi
if aws cloudformation create-stack --stack-name $stack --template-body file://$template $parameters --capabilities CAPABILITY_IAM --disable-rollback
then
cf_tail $stack
fi
}
cf_update() {
# XXX accept params from arguments
# XXX show diff
# XXX show account_alias_or_id
# XXX confirm if not -f
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [template-file] [params-file]"; return 1; fi
local stack="$(basename $1 .json)"
local template="$(cf_template $stack $2)"
if [ ! -f "$template" ]; then
echo "Could not find ${stack}.json. You can specify alternative template as second argument."
return 1
fi
if [ -n "$3" ] ; then local parameters="--parameters file://$3"; fi
if aws cloudformation update-stack --stack-name $stack --template-body file://$template $parameters --capabilities CAPABILITY_IAM
then
cf_tail $stack
fi
}
cf_delete() {
# XXX confirm if not -f
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
if aws cloudformation delete-stack --stack-name $stack
then
cf_tail $stack
fi
}
cf_outputs() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
shift
aws cloudformation describe-stacks --stack-name $stack --query 'Stacks[].Outputs[]' --output table $@
}
cf_resources() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
shift
aws cloudformation describe-stack-resources --stack-name $stack --query "StackResources[].{LogicalResourceId: LogicalResourceId, PhysicalResourceId: PhysicalResourceId}" --output table $@
}
cf_validate() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME cf-template-file"; return 1; fi
aws cloudformation validate-template --template-body file://$1
}
# Show reasons for stack failure
cf_failure() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
aws cloudformation describe-stack-events --stack-name $stack --query 'StackEvents[?contains(ResourceStatus,`FAILED`)].[LogicalResourceId, ResourceStatus, ResourceType, ResourceStatusReason]'
}
cf_params() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
aws cloudformation describe-stacks --query "Stacks[].Parameters[]" --stack-name $stack | jq --sort-keys .
}
# Shows current value of particular parameter in a stack
cf_param() {
if [ -z "$2" ] ; then echo "Usage: $FUNCNAME stack parameter"; return 1; fi
local stack="$(basename $1 .json)"
local param=$2
aws cloudformation describe-stacks --query "Stacks[].Parameters[?ParameterKey==\`$2\`].ParameterValue" --stack-name $stack --output text
}
##
## Functions to work with stack resources
##
cf_asg() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
aws cloudformation describe-stack-resources --stack-name $stack --query "StackResources[?ResourceType==\`AWS::AutoScaling::AutoScalingGroup\`].PhysicalResourceId" --output text
}
cf_asg_instances() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack"; return 1; fi
local stack="$(basename $1 .json)"
local instance_ids="$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names "$(cf_asg $stack)" --query "AutoScalingGroups[].Instances[].[InstanceId]" --output text)"
aws ec2 describe-instances --instance-ids $instance_ids --query "Reservations[].Instances[].[PrivateIpAddress, PublicIpAddress, InstanceId, State.Name, InstanceType, join(\` \`, [Tags[?Key==\`Name\`].Value][])]" --output text
}
cf_asg_instance_ssh() {
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [user]"; return 1; fi
local stack="$(basename $1 .json)"
local user=${2:-root}
local instance_ip="$(cf_asg_instances $stack | head -1 | cut -f 1)"
ssh ${user}@${instance_ip}
}
## Start instance
cf_asg_scale_up() {
# XXX don't scale down
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [desired_capacity]"; return 1; fi
local stack="$(basename $1 .json)"
if [ -n "$2" ]; then
local desired_capacity="$2"
else
local desired_capacity="$(cf_param $stack desiredCapacity)"
fi
if [ -z "$desired_capacity" ]; then
local desired_capacity=1
fi
local asg_name="$(cf_asg $stack)"
echo "Scaling up AutoScalingGroup [$asg_name] to [$desired_capacity] instances"
aws autoscaling set-desired-capacity --auto-scaling-group-name $asg_name --desired-capacity $desired_capacity
}
## Stop Instance
cf_asg_scale_down() {
# XXX don't scale up
if [ -z "$1" ] ; then echo "Usage: $FUNCNAME stack [desired_capacity]"; return 1; fi
local stack="$(basename $1 .json)"
local desired_capacity="${2:-0}"
local instances="$(cf_asg_instances $stack | cut -f 3)"
if [ -n "$instances" ]; then
aws autoscaling set-desired-capacity --auto-scaling-group-name "$(cf_asg $stack)" --desired-capacity $desired_capacity
echo -n "Waiting for instances to terminate ($instances)..."
aws ec2 wait instance-terminated --instance-ids $instances
echo "Done"
else
echo "No running instances found for AutoScalingGroup $stack"
fi
}