-
Notifications
You must be signed in to change notification settings - Fork 12
/
aws-s3-create-bucket-replicated.sh
executable file
·140 lines (96 loc) · 2.87 KB
/
aws-s3-create-bucket-replicated.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
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
#!/bin/sh
# exit on any error
set -e
# aws-s3-create-bucket-replicated.sh
# This is an example of how to create an AWS s3 bucket that is replicated to another region
# fail on any error
#set -e
usage() {
echo "Usage $0 (create|delete) bucket-name [ aws-profile ]"
}
if test $# -lt 2
then
usage
exit 1
fi
AWSPROFILE=""
if test $# -eq 3
then
# if a third argument was provided assume it is the aws profile to use for the operations
AWSPROFILE=" --profile $3 "
echo "using aws profile 3"
fi
# what to do
ACTION="$1"
# bucket name to be created, note that this will have source and destination regions suffixed to it
NAME=$2
# detination region to replicate to
REGIONDEST=us-west-1
# source region to create bucket to be replicated
REGIONSRC=us-east-1
NAMEDEST=$NAME-$REGIONDEST
NAMESRC=$NAME-$REGIONSRC
STACKMAIN=aws-s3-crr-primary
STACKREP=aws-s3-crr-dr
validate() {
# always check for template validity before we do anything
for i in $STACKMAIN.yaml $STACKREP.yaml
do
echo "validating $i"
aws $AWSPROFILE cloudformation validate-template --template-body file://$i
echo
# cfn-lint (pip install cfn-lint) is a very good linter for templates,
# if it is available then also run it
if [ -x $(which cfn-lint) ]; then
echo "running cfn-lint"
cfn-lint $i
else
echo "cfn-lint not found, continuing"
fi
done
}
create() {
# user is responsible for ensuring that the buckets to be created do not already exist
echo "creating replication bucket"
# the destination region bucket must be created first
aws $AWSPROFILE cloudformation create-stack --stack-name $STACKREP \
--template-body file://$STACKREP.yaml \
--region "$REGIONDEST" \
--parameters ParameterKey=NAME,ParameterValue="$NAMEDEST"
STATUS=""
while :; do
if test "$STATUS" == "CREATE_COMPLETE"
then
break
fi
echo "checking status"
STATUS=$(aws cloudformation describe-stacks --stack-name $STACKREP --query Stacks[].StackStatus --output text --region $REGIONDEST)
sleep 5
done
echo "creating primary bucket"
aws $AWSPROFILE cloudformation create-stack --stack-name $STACKMAIN --template-body file://$STACKMAIN.yaml \
--region $REGIONSRC \
--capabilities CAPABILITY_NAMED_IAM \
--parameters ParameterKey=NAME,ParameterValue="$NAME" ParameterKey=REGIONDEST,ParameterValue="$REGIONDEST"
}
delete() {
# user is responsible for ensuring that the buckets to be deleted have already been emptied
echo "deleting stack $STACKMAIN"
aws $AWSPROFILE cloudformation delete-stack --stack-name $STACKMAIN --region $REGIONSRC
sleep 5
echo "deleting stack $STACKREP"
aws $AWSPROFILE cloudformation delete-stack --stack-name $STACKREP --region $REGIONDEST
}
validate
case "$ACTION" in
create)
create $NAME
;;
delete)
delete
;;
*)
usage
exit 1
;;
esac