-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws-run.sh
executable file
·83 lines (72 loc) · 1.87 KB
/
aws-run.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
#!/bin/bash
COMMAND=$1
REGION=$2
STACK_ID=$3
if ! type -p aws &>/dev/null; then
echo "The aws CLI is not installed."
echo "More info: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"
fi
if [[ -z "$REGION" ]]; then
usage
fi
usage() {
echo "usage is:"
echo "./aws-run.sh create [region]"
echo "./aws-run.sh delete [region] [stack id]"
exit
}
tidy_up() {
aws cloudformation delete-stack \
--stack-name "$(echo "$STACK_ID" | awk -F '/' '{print $2}')" --region="$REGION"
}
create() {
# Create stack
STACK_ID=$(aws cloudformation create-stack \
--capabilities CAPABILITY_IAM \
--template-body file://prerequisites.yaml \
--stack-name aws-service-broker-prerequisites \
--output text --query "StackId" \
--region "${REGION}")
# Wait for stack to complete
until
ST=$(aws cloudformation describe-stacks \
--region "${REGION}" \
--stack-name "${STACK_ID}" \
--query "Stacks[0].StackStatus" \
--output text)
echo "$ST"
echo "$ST" | grep "CREATE_COMPLETE"
do
sleep 5
done
# Get the username from the stack outputs
USERNAME=$(aws cloudformation describe-stacks \
--region "${REGION}" \
--stack-name "${STACK_ID}" \
--query "Stacks[0].Outputs[0].OutputValue" \
--output text)
# Create IAM access key. Note down the output, we'll need it when setting up the broker
aws iam create-access-key \
--user-name "${USERNAME}" \
--output json \
--query 'AccessKey.{KEY_ID:AccessKeyId,SECRET_ACCESS_KEY:SecretAccessKey}'
echo "Stack ID: $STACK_ID"
}
case "$COMMAND" in
"create")
create
;;
"delete")
if [[ -z "$STACK_ID" ]]; then
echo "Please provide a valid stack id."
echo "You can list the stacks with:"
echo "aws cloudformation list-stacks --region <replace with region>"
usage
fi
tidy_up
echo "Deleted stack $STACK_ID"
;;
*)
usage
;;
esac