-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_infrastructure.sh
executable file
·187 lines (162 loc) · 6.19 KB
/
build_infrastructure.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
set -e
########################
### NAMING ARGUMENTS ###
########################
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-r|--region)
REGION="$2"
shift # past argument
;;
-p|--profile)
PROFILE="$2"
shift # past argument
;;
-k|--key-name)
PEM_KEY="$2"
shift # past argument
;;
--project-name)
PROJECT_NAME="$2"
shift # past argument
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift # past argument or value
done
echo ' __ _ _ ____ ____ __ __ ____ ____ ____ ____'
echo ' / _\ / )( \/ ___) ( _ \ / \ / \ / ___)(_ _)( __)( _ \'
echo '/ \\ /\ /\___ \ ) _ (( O )( O )\___ \ )( ) _) ) /'
echo '\_/\_/(_/\_)(____/ (____/ \__/ \__/ (____/ (__) (____)(__\_)'
# We check if AWS Cli profile is in parameters to set env var
if [ -z "$PROFILE" ]
then
echo "/!\ Profile parameter is empty, please provide one using --profile or -p !"
exit
fi
if [ -z "$REGION" ]
then
echo "/!\ Region parameter is empty, please provide one using --region or -r !"
exit
fi
if [ -z "$PROJECT_NAME" ]
then
echo "/!\ Project name parameter is empty, please provide one using --project-name !"
exit
fi
if [ -z "$PEM_KEY" ]
then
echo "/!\ PEM key name parameter is empty, please provide one using --key-name or -k !"
exit
fi
VPC_ID=$(aws ec2 describe-vpcs --region ${REGION} --profile ${PROFILE} --filters "Name=isDefault, Values=true" | grep -oe 'VpcId": "[^"]*' | grep -oe 'vpc-.*')
SUBNETS=$(aws ec2 describe-subnets --region ${REGION} --profile ${PROFILE} | grep -oe 'SubnetId": "[^"]*' | grep -oe 'subnet-.*' | awk '{print $0}' ORS=',' | sed 's/,$//')
## Create security group
aws cloudformation deploy \
--stack-name ${PROJECT_NAME}-sg \
--profile ${PROFILE} \
--template-file infrastructure/security-group.yml \
--region ${REGION} \
--parameter-overrides \
ProjectName=${PROJECT_NAME} \
VpcId=${VPC_ID} \
--no-fail-on-empty-changeset
# Export the security group variable
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-sg --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
## Create LoadBalancer
aws cloudformation deploy \
--stack-name ${PROJECT_NAME}-load-balancer \
--profile ${PROFILE} \
--template-file infrastructure/load-balancer.yml \
--region ${REGION} \
--parameter-overrides \
ProjectName=${PROJECT_NAME} \
Subnets=${SUBNETS} \
SecurityGroup=${AlbSg} \
VpcId=${VPC_ID} \
--no-fail-on-empty-changeset
# Export the LoadBalancer variable
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-load-balancer --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
## Create cloud front
aws cloudformation deploy \
--profile ${PROFILE} \
--stack-name ${PROJECT_NAME}-cloud-front \
--template-file infrastructure/cloud-front.yml \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset \
--region ${REGION} \
--parameter-overrides \
ProjectName=${PROJECT_NAME} \
AlbDNS=${AlbDNS}
## Create IAM Roles
aws cloudformation deploy \
--profile ${PROFILE} \
--stack-name ${PROJECT_NAME}-iam \
--template-file infrastructure/iam.yml \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset \
--region ${REGION} \
--parameter-overrides ProjectName=${PROJECT_NAME}
# Export the TaskRole variable
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-iam --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
# Create an ECS Repository for Dockerfile
aws cloudformation deploy \
--profile ${PROFILE} \
--stack-name ${PROJECT_NAME}-ecs-repository \
--template-file infrastructure/ecs-repository.yml \
--region ${REGION} \
--no-fail-on-empty-changeset \
--parameter-overrides RepositoryName=${PROJECT_NAME}-ecr-repository
# Export the ECSRepository variable
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-ecs-repository --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
# Push the Dockerfile on ECS
$(aws ecr get-login --no-include-email --region ${REGION} --profile ${PROFILE})
docker build --tag "${ECSRepository}:latest" .
docker push "${ECSRepository}:latest"
## Create an ECS Cluster
aws cloudformation deploy \
--stack-name ${PROJECT_NAME}-ecs-cluster \
--profile ${PROFILE} \
--template-file infrastructure/ecs-cluster.yml \
--region ${REGION} \
--parameter-overrides ClusterName=${PROJECT_NAME}-cluster \
--no-fail-on-empty-changeset
# Export the ECSCluster variable
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-ecs-cluster --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
## ECS AutoScaling group
aws cloudformation deploy \
--stack-name ${PROJECT_NAME}-ecs-autoscaling \
--profile ${PROFILE} \
--template-file infrastructure/autoscaling.yml \
--region ${REGION} \
--no-fail-on-empty-changeset \
--parameter-overrides \
InstanceProfile=${EC2InstanceProfile} \
AssiociatePublicIp=true \
Subnets=${SUBNETS} \
ProjectName=${PROJECT_NAME} \
KeyName=${PEM_KEY} \
SecurityGroup=${DockerSg} \
ECSCluster=${ECSCluster}
export $(aws cloudformation describe-stacks --stack-name ${PROJECT_NAME}-ecs-autoscaling --region ${REGION} --profile ${PROFILE} --output text --query 'Stacks[].Outputs[]' | tr '\t' '=')
## Enable ECS Services for the cluster
aws cloudformation deploy \
--stack-name ${PROJECT_NAME}-ecs-services \
--profile ${PROFILE} \
--template-file infrastructure/ecs-services.yml \
--region ${REGION} \
--parameter-overrides \
ECSTaskRole=${TaskRole} \
DockerRepository=${ECSRepository} \
ECSServiceRole=${ServiceRole} \
ECSCluster=${ECSCluster} \
TargetGroup=${TargetGroup} \
ProjectName=${PROJECT_NAME} \
--no-fail-on-empty-changeset