-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcfn
executable file
·98 lines (77 loc) · 3.05 KB
/
cfn
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
#!/bin/bash
type aws >/dev/null 2>&1 || { echo "$0: aws-cli not installed" >&2; exit 1; }
opts=he:f:glo:p:tvwD
delete=false
fmt=text
listing=false
events=false
waitfor=false
enum=1
get=false
valid=false
bq="Stacks[*]"
q="$bq.[StackName,StackStatus]"
function _usage {
cat << _USAGE_
`basename $0`: abbreviate common AWS cloudformation operations
Usage:
`basename $0` [-$opts] <stack | template-file>
Options:
-h help (you are here)
-e <int> display n number stack events
-f <format> specify output format
-g display the stack's cloudformation template
-l list stacks using prefix search
-o <output> display stack output value
-p <parameter> display stack parameter value
-t <file> validate a cloudformation template file
-v verbose stack info (in default format)
-w wait for stack to complete
-D delete stack
_USAGE_
}
function liststacks {
if [ -z $1 ]; then
aws cloudformation list-stacks --query="StackSummaries[?StackStatus!=\`DELETE_COMPLETE\`].[StackName]" --output text | sort
else
aws cloudformation list-stacks --query="StackSummaries[?starts_with(StackName,\`$1\`)==\`true\`]|[?StackStatus!=\`DELETE_COMPLETE\`].[StackName]" --output text
fi
}
while getopts $opts opt; do
case $opt in
h) _usage; exit ;;
e) events=true && enum="$OPTARG" ;;
f) [ -n "$OPTARG" ] && fmt="$OPTARG" ;;
g) get=true ;;
l) listing=true ;;
o) [ -n "$OPTARG" ] && q="$bq.Outputs[?OutputKey==\`$OPTARG\`].OutputValue[]" ;;
p) [ -n "$OPTARG" ] && q="$bq.Parameters[?ParameterKey==\`$OPTARG\`].ParameterValue[]" ;;
t) valid=true ;;
w) waitfor=true ;;
v) q=$bq; fmt=$(aws configure get output || echo json) ;;
D) delete=true ;;
?|*) echo; _usage; exit 2 ;;
esac
done
shift $(($OPTIND-1))
$listing && { liststacks $1; exit; }
[ -z "$1" ] && { echo "`basename $0`: specify a stack name" >&2; exit 1; }
( $valid || [ -f "$1" ] ) && { aws cloudformation validate-template --template-body "`cat $1`"; exit; }
$get && { aws cloudformation get-template --stack-name="$1"; exit; }
$delete && aws cloudformation delete-stack --stack-name="$1"
if $events; then
aws cloudformation describe-stack-events --stack-name $1 --output $fmt \
--max-items=$enum --query=StackEvents[].[LogicalResourceId,ResourceStatus,ResourceStatusReason]
exit
fi
aws cloudformation describe-stacks --stack-name="$1" --query "$q" --output $fmt
s=$?
! $waitfor && exit $s
event="IN_PROGRESS"
while [[ $event =~ IN_PROGRESS$ ]]; do
sleep 5
event=`aws cloudformation describe-stacks --stack-name $1 --query Stacks[].StackStatus --output text 2>/dev/null`
aws cloudformation describe-stack-events --stack-name $1 --output text \
--max-items=1 --query=StackEvents[].[LogicalResourceId,ResourceStatus] | head -n 1
done
echo $event