-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeploy_data_consumer.sh
executable file
·133 lines (109 loc) · 3.52 KB
/
deploy_data_consumer.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
#!/bin/bash
# Deploy/Update project on AWS by CloudFormation.
# Get script location.
SHELL_PATH=$(cd "$(dirname "$0")";pwd)
# Import global variables
source $SHELL_PATH/../bootstrapping/config.sh
arg_count=$#
script_name=$(basename $0)
stack_action=update
input_template_file="$SHELL_PATH/data_consumer_template.yaml"
output_template_file_name="packaged-template-output.yaml"
output_template_file="$SHELL_PATH/$output_template_file_name"
cf_stack_name="$project_name-data-consumer"
cf_change_set_name="$cf_stack_name-change-set"
if test $arg_count -eq 1; then
if [[ $1 =~ ^(create|update)$ ]]; then
stack_action=$1
else
echo "Stack Action must be create or update"
echo "Usage: $script_name [create|update]"
exit -1
fi
else
echo "Usage: $script_name [create|update]"
echo ""
echo "Examples:"
echo "$script_name create"
echo ""
exit 0
fi
echo "${stack_action^^} $cf_stack_name cloudformation stack..."
if [ $stack_action = update ]; then
echo "NOTE: Before UPDATE a stack, be sure you already have the corresponding stack in cloudformation"
fi
if [ -f $output_template_file ]; then
rm -rf $output_template_file
fi
echo "Packaging..."
aws cloudformation package \
--template-file $input_template_file \
--s3-bucket $s3_deployment_bucket \
--output-template-file $output_template_file
result=$?
if test $result -ne 0; then
echo "Failed to package template $input_template_file"
exit $result
fi
echo "Uploading template file..."
aws s3api put-object \
--bucket $s3_deployment_bucket \
--key $output_template_file_name \
--body $output_template_file
latest_image_degest="$(aws ecr describe-images \
--repository-name $ecr_lambda_consumer_repo \
--image-ids imageTag=latest \
--query 'imageDetails[0].imageDigest' \
--output text)"
latest_image_uri="$ecr_lambda_consumer_repo_uri@$latest_image_degest"
echo "Latest Image Uri: $latest_image_uri"
echo "Creating change set..."
aws cloudformation create-change-set \
--change-set-type ${stack_action^^} \
--stack-name $cf_stack_name \
--change-set-name $cf_change_set_name \
--template-url https://$s3_deployment_bucket.s3.$deployment_region.amazonaws.com/$output_template_file_name \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--parameters ParameterKey="Prefix",ParameterValue=$project_name \
ParameterKey="ImageUri",ParameterValue="$latest_image_uri" \
ParameterKey="KinesisStreamName",ParameterValue=$fb_kinesis_stream
result=$?
if test $result -ne 0; then
echo "Failed to create change set $cf_change_set_name"
if [ -f $output_template_file ]; then
rm -rf $output_template_file
fi
exit $result
fi
echo "Waiting for change-set-create-complete..."
aws cloudformation wait \
change-set-create-complete \
--stack-name $cf_stack_name \
--change-set-name $cf_change_set_name
result=$?
if test $result -ne 0; then
echo "create-change-set return failed"
if [ -f $output_template_file ]; then
rm -rf $output_template_file
fi
exit $result
fi
echo "Executing change set..."
aws cloudformation execute-change-set \
--change-set-name $cf_change_set_name \
--stack-name $cf_stack_name
echo "Waiting for stack executing complete..."
aws cloudformation wait \
stack-${stack_action}-complete \
--stack-name $cf_stack_name
result=$?
if test $result -ne 0; then
echo "Deleting change set..."
aws cloudformation delete-change-set \
--stack-name $cf_stack_name \
--change-set-name $cf_change_set_name
fi
if [ -f $output_template_file ]; then
rm -rf $output_template_file
fi
echo "done"