-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-cf-create-stack.sh
executable file
·81 lines (73 loc) · 2.71 KB
/
aws-cf-create-stack.sh
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
#!/usr/bin/env bash
AWSRegs="ap-northeast-1 ap-northeast-2 ap-south-1 ap-southeast-1 ap-southeast-2 ca-central-1 eu-central-1 eu-west-1 eu-west-2 eu-west-3 sa-east-1 us-east-1 us-east-2 us-west-1 us-west-2"
profile=${AWS_DEFAULT_PROFILE:-none}
regions=${AWS_DEFAULT_REGION:-us-east-1}
usage() {
echo \
"$(tput bold)$(basename ${BASH_SOURCE[0]})$(tput sgr0)
Usage: Creates CloudFormation stack STACKNAME in region(s) REGION[,REGION,...]
using template TEMPLATE
Example: $(basename ${BASH_SOURCE[0]}) -s svc-stack -r us-east-1,us-west2 -t svc.json
Required: -s STACKNAME -t TEMPLATE
Options:
-s STACKNAME name of stack
-t TEMPLATE name of template
-p AWS profile (default: AWS_DEFAULT_PROFILE or \"none\")
-r comma-delimited list of regions
(default: AWS_DEFAULT_REGION or us-east-1; \"all\" = all)
-h help"
}
prereq="Prerequisites are missing and must be installed before continuing:\n"
missing_req=false
if ! aws --version >/dev/null 2>&1; then
prereq+="\t'aws' python cli from http://aws.amazon.com/cli/\n"
missing_req=true
fi
if $missing_req; then
echo -e "Error: $prereq" >&2
exit 1
fi
[[ "$*" =~ "--help" ]] && { usage | less; exit; }
while getopts ":p:r:s:t:h" opt; do
case $opt in
p) profile=$OPTARG
;;
r) [ "${OPTARG,,}" == "all" ] && { regions=$AWSRegs; continue; }
unset regions
while IFS=',' read -ra REGION; do
for i in "${REGION[@]}"; do
FOUND=false
for reg in $AWSRegs; do
[ "$reg" == "$i" ] && FOUND=true
done
if [ $FOUND ]; then
regions+="$i "
else
echo "Error: invalid region - $i" >&2
echo "Valid regions are: $AWSRegs" | fold -s >&2
exit 1
fi
done
done <<< "${OPTARG,,}" # ${OPTARG,,} converts $OPTARG to all lowercase letters
;;
s) STACKNAME=$OPTARG
;;
t) TEMPLATE=$OPTARG
;;
h) usage ; exit
;;
*) echo "Error: invalid option -$OPTARG" >&2
usage ; exit 1
;;
esac
done
[[ -n "$STACKNAME" || -n "$TEMPLATE" ]] || { usage; exit 1; }
## MAIN
for reg in $regions; do
if aws --profile $profile --region $reg cloudformation describe-stacks --stack-name $STACKNAME >/dev/null 2>&1; then
echo "Error: Stack $STACKNAME already exists in region $reg" >&2
exit 1
fi
echo aws --profile $profile --region $reg cloudformation create-stack --stack-name $STACKNAME --template-body file://$TEMPLATE --capabilities CAPABILITY_IAM
aws --profile $profile --region $reg cloudformation create-stack --stack-name $STACKNAME --template-body file://$TEMPLATE --capabilities CAPABILITY_IAM
done