Repository navigation
Expand file tree
/
Copy pathmaster_cf.yaml
More file actions
288 lines (251 loc) · 9.57 KB
/
Copy pathmaster_cf.yaml
File metadata and controls
288 lines (251 loc) · 9.57 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
Description: Stack templates generalized.
Parameters:
CreateRepo:
Description: Create from stack a ECR Repository
Type: String
Default: false
AllowedValues:
- true
- false
RepositoryName:
Description: Name for a ECR Repository
Type: String
Default: general_image
IAMUser:
Description: ARN IAM user to have access to the Repository
Type: String
Default: arn:aws:iam::202279780353:user/giorgio
CertificateARN:
Description: Certificate of SSL for HTTPS
Type: String
StackEnv:
Description: The enviroment that we are going to work on
Default: qa
Type: String
AllowedValues:
- qa
- prod
ImageTag:
Description: The tag that will be looked for
Default: test
Type: String
#cluster configuration
InstanceType:
Description: Which instance type should we use to build the ECS cluster?
Default: t2.micro
Type: String
Desired:
Description: Desired number of EC2 Instances in the ASG cannot be less than MinSize
Default: 1
Type: Number
MinSize:
Description: Minimum number of EC2 Instances in the ASG
Default: 1
Type: Number
MaxSize:
Description: Maximum number of EC2 Instances in the ASG
Default: 3
Type: Number
KeyPair:
Description: Amazon EC2 Key Pair
Default: KeyFerdinand
Type: "AWS::EC2::KeyPair::KeyName"
ScaleUpThreshold:
Description: The value against which the specified statistic is compared for scaling up the AutoScalingGroup
Default: 80
Type: Number
ScaleDownThreshold:
Description: The value against which the specified statistic is compared for scaling down the AutoScalingGroup
Default: 40
Type: Number
#service configuration
DesiredCount:
Description: How many instances of this task should we run across our cluster?
Type: Number
Default: 1
#database parameters if you need one
DBMasterUsername:
Description: The database user name for the db instance
Default: demo
Type: String
DBMasterUserPassword:
Description: The password for the master user for the database
Default: ferdinand_demo
Type: String
NoEcho: true
DBInstanceClass:
Description: Name of the compute and capacity clasess of the database
Default: db.t2.micro
Type: String
DBEngine:
Description: Database engine that the DB instance use.
Default: MySQL
Type: String
DBName:
Description: Name of the database provided at the time of creation
Default: demoDB
Type: String
DBInstanceIdentifier:
Description: Name of the database
Default: demoDB
Type: String
DBAllocatedStorage:
Description: Allocated storage size fot the db (GB)
Default: 100
Type: Number
#cache database if you need on
CacheNodeType:
Description: The compute and memory capacity of nodes in a cache cluster.
Default: cache.t2.micro
Type: String
CacheEngine:
Description: Engine of the cache
Default: redis
Type: String
CacheClusterName:
Description: Name of the cache cluster.
Default: redisCache
Type: String
CacheNumCacheNodes:
Description: Number of nodes in the cache cluster.
Default: 1
Type: Number
Conditions:
CreateECR: !Equals [!Ref CreateRepo, true]
Resources:
VPC:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/vpc.yaml
Parameters:
EnvironmentName: !Ref AWS::StackName
VpcCIDR: 10.24.0.0/16
PublicSubnet1CIDR: 10.24.8.0/21
PublicSubnet2CIDR: 10.24.16.0/21
PrivateSubnet1CIDR: 10.24.24.0/21
PrivateSubnet2CIDR: 10.24.32.0/21
#Secutiry Group
#Services that allow us to enable trafic through ports/protocols
STACKSG:
Type: AWS::CloudFormation::Stack
DependsOn: VPC
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/vpc-sg.yaml
Parameters:
VPC: !GetAtt VPC.Outputs.VPC
EnvironmentName: !Sub ${AWS::StackName}-client
#Elastic Load Balancer
#This template enable trafic listeners on ports 80 and 443
#Set a default target group for all listener, but you have to define the target group for each service (see client service)
STACKELB:
Type: AWS::CloudFormation::Stack
DependsOn: STACKSG
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/elb.yaml
Parameters:
EnvironmentName: !Sub ${AWS::StackName}-client
VPC: !GetAtt VPC.Outputs.VPC
Subnets: !GetAtt VPC.Outputs.PublicSubnets
CertificateArn: !Ref CertificateARN
SecurityGroup: !GetAtt STACKSG.Outputs.PublicSG
#Cluster
#This template creates alamrs for the cluster
#Scale up +1 instance when:
#memory +98% for more than 1 minute
#CPU +90% for more than 1 minute
#Scale down -1 instance when:
#memory 45% for more than 3 minutes
#CPU 40% for more than 1 minute
#This template creates a cluster with custom amis.
#EC2 ROLE ECS ALLOW *
#Mappings amis, identifier is region specific.
#LaunchConfiguration is set to the mappings amis
STACKECS:
Type: AWS::CloudFormation::Stack
DependsOn: STACKELB
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/ECS/Master/cluster.yaml
Parameters:
EnvironmentName: !Sub ${AWS::StackName}-client
InstanceType: !Ref InstanceType
Subnets: !GetAtt VPC.Outputs.PrivateSubnets
SecurityGroup: !GetAtt STACKSG.Outputs.PrivateSG
KeyPair: !Ref KeyPair
MinSize: !Ref MinSize
MaxSize: !Ref MaxSize
Desired: !Ref Desired
ScaleUpThreshold: !Ref ScaleUpThreshold
ScaleDownThreshold: !Ref ScaleDownThreshold
#Database
#this template is for a standar rds service.
#You can provide a new instance for the deployment if needed.
STACKDB:
Type: AWS::CloudFormation::Stack
DependsOn: STACKECS
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/databases/rds.yaml
Parameters:
EnvironmentName: !Sub ${AWS::StackName}-client
VPC: !GetAtt VPC.Outputs.VPC
Subnets: !GetAtt VPC.Outputs.PrivateSubnets
VPCSecurityGroups: !GetAtt STACKSG.Outputs.PrivateSG
Engine: !Ref DBEngine
DBName: !Ref DBName
DBInstanceIdentifier: !Ref DBInstanceIdentifier
MasterUsername: !Ref DBMasterUsername
MasterUserPassword: !Ref DBMasterUserPassword
DBInstanceClass: !Ref DBInstanceClass
AllocatedStorage: !Ref DBAllocatedStorage
#Cache
#This template is for a cache instance of redis
STACKCACHE:
Type: AWS::CloudFormation::Stack
DependsOn: STACKECS
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/infrastructure/databases/ec_redis.yaml
Parameters:
EnvironmentName: !Sub ${AWS::StackName}-client
CacheNodeType: !Ref CacheNodeType
ClusterName: !Ref CacheClusterName
Engine: !Ref CacheEngine
NumCacheNodes: !Ref CacheNumCacheNodes
VPCSubnetsIds: !GetAtt VPC.Outputs.PrivateSubnets
VPCSecurityGroupsIds: !GetAtt STACKSG.Outputs.PrivateSG
#Client service
#this template is for a standar client service.
#Importan parameters:
#DesiredCount - this parameter will have a default of 2 instances
#ImageTag - Image tag would search for an ami version with this tag
ClientService:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/services/django_website/django_webapp.yaml
Parameters:
VPC: !GetAtt VPC.Outputs.VPC
LoadBalancer: !GetAtt STACKELB.Outputs.LoadBalancer
Cluster: !GetAtt STACKECS.Outputs.Cluster
DesiredCount: !Ref DesiredCount
HttpListener: !GetAtt STACKELB.Outputs.HttpListener
DBName: !Ref DBName
DBUser: !Ref DBMasterUsername
DBURL: !GetAtt STACKDB.Outputs.DBEndpointAddress
DBPort: !GetAtt STACKDB.Outputs.DBEndpointPort
RedisHost: !GetAtt STACKCACHE.Outputs.EndpointAddress
RedisPort: !GetAtt STACKCACHE.Outputs.EndpointPort
# DBURL: dumy
# DBPort: dumy
# RedisHost: dumy
# RedisPort: dumy
ImageTag: !Ref ImageTag
ECRRepository:
Type: AWS::CloudFormation::Stack
Condition: CreateECR
Properties:
TemplateURL: https://s3.amazonaws.com/ferdinand-bucket/services/django_website/service.yaml
Parameters:
RepositoryName: !Ref RepositoryName
IAMUser: !Ref IAMUser
Outputs:
URL:
Description: URL of demo
Value: !GetAtt STACKELB.Outputs.LoadBalancerUrl