forked from yabebalFantaye/aws_bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs_deploy.sh
executable file
·171 lines (133 loc) · 5.41 KB
/
ecs_deploy.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/bash
#
# Adapted from Ref:
# https://aws.amazon.com/blogs/containers/maintaining-transport-layer-security-all-the-way-to-your-container-using-the-application-load-balancer-with-amazon-ecs-and-envoy/
#
#https://stackoverflow.com/questions/60122188/how-to-turn-off-the-pager-for-aws-cli-return-value
export AWS_PAGER=""
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Starting from `pwd` dir .."
#--------------------------------------------------------#
###--------Define necessary environment variables-----##
##------------------------------------------------------#
if [ ! -z "$1" ]; then
echo "Loading variables from $1"
source $1
else
echo "Usage: ecs_deploy <path to params file>"
exit 0
fi
#---------------------------------------------------------#
### ----------- Change dir to deploy folder----------##
##-------------------------------------------------------#
cd $deployDir
echo "Current working dir: $deployDir"
#--------------------------------------------------------#
##------- Generate and Push CI/CD Config to the repository --------#
##------------------------------------------------------#
#check which CI/CD config to write
if $github_actions ; then
template=template/github-actions-template.yml
fout=$configoutputdir/github_actions_config.yml
else
template=template/circleci-template.yml
fout=$configoutputdir/circleci_config.yml
fi
#write template
if [ -f $template ]; then
echo "Creating and setting up CI/CD config file ..."
envsubst <${template}>$fout
echo "$template variables replaced and saved as $fout"
fi
if $push_cicd_template ; then
echo "Pushing CI/CD config to repo ..."
source push_cicd_template.sh ""
fi
#--------------------------------------------------------#
###-------- Create the Application Load Balancer -----##
##------------------------------------------------------#
if $create_and_setup_alb ; then
if [ -z $loadbalancerArn ] || [ -z $targetGroupArn ]; then
echo "Creating and setting up ALB ..."
source create_alb.sh "" #returns needed variables
fi
fi
#stop if variables are not set
if [ -z $loadbalancerArn ] || [ -z $targetGroupArn ]; then
exit 0
fi
#--------------------------------------------------------#
###-------- Create the Application Auto Scaling Group -----##
##------------------------------------------------------#
if $create_launch_template ; then
if [[ $AwsImageId == $AwsImageOurs ]]; then
echo "Updating Launch Template from pre-made AMI ..."
source update_launch_template.sh ""
else
echo "Creating and setting up Launch Template ..."
source create_launch_template.sh "" #returns AsgId variables
fi
fi
if $create_and_setup_asg && [ $ECSLaunchType == "EC2" ]; then
echo "Creating and setting up ASG ..."
source create_asg.sh "" #no variable returned
fi
#--------------------------------------------------------#
##------- Create two ECR repositories to store
#-------- the application and Envoy container images.
##------------------------------------------------------#
if $create_ecr_repo ; then
echo "Creating ECR repo .."
source create_ecr_repos.sh "" #no variables returned
fi
# push test image if requested
if $docker_push_test_app; then
echo "Pushing docker to ECS cluster .."
source push_test_images_to_ecr.sh ""
fi
#--------------------------------------------------------#
###-------- Create cluster and task definition -----##
##------------------------------------------------------#
if $create_ecs_cluster_and_task; then
echo "Creating ECS cluster .."
#no variables returned
source create_ecs_cluster.sh ""
fi
#--------------------------------------------------------#
###- Certificate setup: PLEASE SETUP WILD ACM CERTIFICATES --##
##------------------------------------------------------#
# if $create_acm_certificate && [ -z $certificateArn ]; then
# echo "Getting ACM certificate ..."
# source acm_certificate_setup.sh "" #returns needed variables
# fi
# #stop if variable is not set
# if [ -z $certificateArn ]; then
# echo "certificateArn is not set"
# exit 0
# fi
#--------------------------------------------------------#
###-------- Certificate ecs service -----##
##------------------------------------------------------#
if $create_ecs_service; then
echo "Creating ECS service .."
source create_ecs_service.sh ""
fi
#--------------------------------------------------------#
###-------- --------Route53 Setup ---------------------##
##------------------------------------------------------#
if $create_route53_record; then
echo "Creating Route53 Record ..."
source create_route53_record.sh ""
fi
#--------------------------------------------------------#
##-------- Certificate setup -----##
#------------------------------------------------------#
# echo quit | openssl s_client -showcerts -servername ecs-encryption.awsblogs.info -connect ecs-encryption.awsblogs.info:443 > cacert.pem
# #Hit the service
# curl --cacert cacert.pem https://ecs-encryption.awsblogs.info/service
#--------------------------------------------------------#
##-------- sync output folder to s3 -----##
#------------------------------------------------------#
outputdir=$(dirname $logoutputdir)
aws s3 cp $outputdir $s3bucket/aws_bash_output/$region/ --recursive \
--region $region --profile $profile_name