-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.yml
498 lines (494 loc) · 16.5 KB
/
stack.yml
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
AWSTemplateFormatVersion: "2010-09-09"
Description: Todobackend Application
Parameters:
ApplicationDesiredCount:
Type: Number
Description: Desired EC2 instance count
ApplicationImageId:
Type: String
Description: ECS Amazon Machine Image (AMI) ID
ApplicationImageTag:
Type: String
Description: Application Docker Image Tag
Default: latest
ApplicationSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Target subnets for EC2 instances
DatabasePassword:
Type: String
Description: Database password
NoEcho: "true"
VpcId:
Type: AWS::EC2::VPC::Id
Description: Target VPC
Resources:
MigrateTask:
Type: AWS::CloudFormation::CustomResource
DependsOn:
- ApplicationAutoscaling
- ApplicationDatabase
Properties:
ServiceToken: !Sub ${EcsTaskRunner.Arn}
Cluster: !Ref ApplicationCluster
TaskDefinition: !Ref MigrateTaskDefinition
MigrateTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: todobackend-migrate
ContainerDefinitions:
- Name: migrate
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/aws-learn-devops/todobackend:${ApplicationImageTag}
MemoryReservation: 5
Cpu: 5
Environment:
- Name: DJANGO_SETTINGS_MODULE
Value: todobackend.settings_release
- Name: MYSQL_HOST
Value: !Sub ${ApplicationDatabase.Endpoint.Address}
- Name: MYSQL_USER
Value: todobackend
- Name: MYSQL_PASSWORD
Value: !Ref DatabasePassword
- Name: MYSQL_DATABASE
Value: todobackend
Command:
- python3
- manage.py
- migrate
- --no-input
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub /${AWS::StackName}/ecs/todobackend
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: docker
EcsTaskRunner:
Type: AWS::Lambda::Function
DependsOn:
- EcsTaskRunnerLogGroup
Properties:
FunctionName: !Sub ${AWS::StackName}-ecsTasks
Description: !Sub ${AWS::StackName} ECS Task Runner
Handler: index.handler
MemorySize: 128
Runtime: python3.6
Timeout: 300
Role: !Sub ${EcsTaskRunnerRole.Arn}
Code:
ZipFile: |
import cfnresponse
import boto3
client = boto3.client('ecs')
def handler(event, context):
try:
print("Received event %s" % event)
if event['RequestType'] == 'Delete':
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, event['PhysicalResourceId'])
return
tasks = client.run_task(
cluster=event['ResourceProperties']['Cluster'],
taskDefinition=event['ResourceProperties']['TaskDefinition'],
overrides=event['ResourceProperties'].get('Overrides',{}),
count=1,
startedBy=event['RequestId']
)
task = tasks['tasks'][0]['taskArn']
print("Started ECS task %s" % task)
waiter = client.get_waiter('tasks_stopped')
waiter.wait(
cluster=event['ResourceProperties']['Cluster'],
tasks=[task],
)
result = client.describe_tasks(
cluster=event['ResourceProperties']['Cluster'],
tasks=[task]
)
exitCode = result['tasks'][0]['containers'][0]['exitCode']
if exitCode > 0:
print("ECS task %s failed with exit code %s" % (task, exitCode))
cfnresponse.send(event, context, cfnresponse.FAILED, {}, task)
else:
print("ECS task %s completed successfully" % task)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, task)
except Exception as e:
print("A failure occurred with exception %s" % e)
cfnresponse.send(event, context, cfnresponse.FAILED, {})
EcsTaskRunnerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: EcsTaskRunnerPermissions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: EcsTasks
Effect: Allow
Action:
- ecs:DescribeTasks
- ecs:ListTasks
- ecs:RunTask
Resource: "*"
Condition:
ArnEquals:
ecs:cluster: !Sub ${ApplicationCluster.Arn}
- Sid: ManageLambdaLogs
Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource: !Sub ${EcsTaskRunnerLogGroup.Arn}
EcsTaskRunnerLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${AWS::StackName}-ecsTasks
RetentionInDays: 7
ApplicationService:
Type: AWS::ECS::Service
DependsOn:
- ApplicationAutoscaling
- ApplicationLogGroup
- ApplicationLoadBalancerHttpListener
- MigrateTask
Properties:
TaskDefinition: !Ref ApplicationTaskDefinition
Cluster: !Ref ApplicationCluster
DesiredCount: !Ref ApplicationDesiredCount
LoadBalancers:
- ContainerName: todobackend
ContainerPort: 8000
TargetGroupArn: !Ref ApplicationServiceTargetGroup
Role: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100
ApplicationTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: todobackend
Volumes:
- Name: public
Host:
SourcePath: /data/public
ContainerDefinitions:
- Name: todobackend
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/aws-learn-devops/todobackend:${ApplicationImageTag}
MemoryReservation: 395
Cpu: 245
MountPoints:
- SourceVolume: public
ContainerPath: /public
Environment:
- Name: DJANGO_SETTINGS_MODULE
Value: todobackend.settings_release
- Name: MYSQL_HOST
Value: !Sub ${ApplicationDatabase.Endpoint.Address}
- Name: MYSQL_USER
Value: todobackend
- Name: MYSQL_PASSWORD
Value: !Ref DatabasePassword
- Name: MYSQL_DATABASE
Value: todobackend
- Name: SECRET_KEY
Value: some-random-secret-should-be-here
Command:
- uwsgi
- --http=0.0.0.0:8000
- --module=todobackend.wsgi
- --master
- --die-on-term
- --processes=4
- --threads=2
- --check-static=/public
PortMappings:
- ContainerPort: 8000
HostPort: 0
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub /${AWS::StackName}/ecs/todobackend
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: docker
- Name: collectstatic
Essential: false
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/aws-learn-devops/todobackend:${ApplicationImageTag}
MemoryReservation: 5
Cpu: 5
MountPoints:
- SourceVolume: public
ContainerPath: /public
Environment:
- Name: DJANGO_SETTINGS_MODULE
Value: todobackend.settings_release
Command:
- python3
- manage.py
- collectstatic
- --no-input
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Sub /${AWS::StackName}/ecs/todobackend
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: docker
ApplicationLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /${AWS::StackName}/ecs/todobackend
RetentionInDays: 7
ApplicationServiceTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Protocol: HTTP
Port: 8000
VpcId: !Ref VpcId
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 30
ApplicationLoadBalancerHttpListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref ApplicationLoadBalancer
Protocol: HTTP
Port: 80
DefaultActions:
- TargetGroupArn: !Ref ApplicationServiceTargetGroup
Type: forward
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing
Subnets: !Ref ApplicationSubnets
SecurityGroups:
- !Ref ApplicationLoadBalancerSecurityGroup
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: 30
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-alb
ApplicationLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Application Load Balancer Security Group
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value:
Fn::Sub: ${AWS::StackName}-alb-sg
ApplicationLoadBalancerToApplicationIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 32768
ToPort: 60999
GroupId: !Ref ApplicationAutoscalingSecurityGroup
SourceSecurityGroupId: !Ref ApplicationLoadBalancerSecurityGroup
ApplicationLoadBalancerToApplicationEgress:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 32768
ToPort: 60999
GroupId: !Ref ApplicationLoadBalancerSecurityGroup
DestinationSecurityGroupId: !Ref ApplicationAutoscalingSecurityGroup
ApplicationDatabase:
Type: AWS::RDS::DBInstance
Properties:
Engine: MySQL
EngineVersion: 5.7
DBInstanceClass: db.t2.micro
AllocatedStorage: 10
StorageType: gp2
MasterUsername: todobackend
MasterUserPassword:
Ref: DatabasePassword
DBName: todobackend
VPCSecurityGroups:
- !Ref ApplicationDatabaseSecurityGroup
DBSubnetGroupName: !Ref ApplicationDatabaseSubnetGroup
MultiAZ: "false"
AvailabilityZone: !Sub ${AWS::Region}a
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-db
ApplicationDatabaseSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: Application Database Subnet Group
SubnetIds: !Ref ApplicationSubnets
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-db-subnet-group
ApplicationDatabaseSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Sub ${AWS::StackName} Application Database Security Group
VpcId: !Ref VpcId
SecurityGroupEgress:
- IpProtocol: icmp
FromPort: -1
ToPort: -1
CidrIp: 192.0.2.0/32
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-db-sg
ApplicationToApplicationDatabaseIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 3306
ToPort: 3306
GroupId: !Ref ApplicationDatabaseSecurityGroup
SourceSecurityGroupId: !Ref ApplicationAutoscalingSecurityGroup
ApplicationToApplicationDatabaseEgress:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 3306
ToPort: 3306
GroupId: !Ref ApplicationAutoscalingSecurityGroup
DestinationSecurityGroupId: !Ref ApplicationDatabaseSecurityGroup
ApplicationAutoscalingSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Sub ${AWS::StackName} Application Autoscaling Security Group
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: udp
FromPort: 53
ToPort: 53
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
ApplicationAutoscalingInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- Ref: ApplicationAutoscalingInstanceRole
ApplicationAutoscalingInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: ECSContainerInstancePermissions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ecs:RegisterContainerInstance
- ecs:DeregisterContainerInstance
- ecs:UpdateContainerInstancesState
Resource: !Sub ${ApplicationCluster.Arn}
- Effect: Allow
Action:
- ecs:DiscoverPollEndpoint
- ecs:Submit*
- ecs:Poll
- ecs:StartTelemetrySession
Resource: "*"
- Effect: Allow
Action:
- ecr:BatchCheckLayerAvailability
- ecr:BatchGetImage
- ecr:GetDownloadUrlForLayer
- ecr:GetAuthorizationToken
Resource: "*"
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- logs:DescribeLogStreams
Resource: !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/${AWS::StackName}*
ApplicationCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: todobackend-cluster
ApplicationAutoscalingLaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
commands:
05_public_volume:
command: mkdir -p /data/public
06_public_volume_permissions:
command: chown -R 1000:1000 /data/public
10_first_run:
command: sh firstrun.sh
cwd: /home/ec2-user
env:
ECS_CLUSTER: !Ref ApplicationCluster
STACK_NAME: !Ref AWS::StackName
AUTOSCALING_GROUP: ApplicationAutoscaling
AWS_DEFAULT_REGION: !Ref AWS::Region
Properties:
ImageId: !Ref ApplicationImageId
InstanceType: t2.micro
KeyName: ac-test
IamInstanceProfile: !Ref ApplicationAutoscalingInstanceProfile
SecurityGroups:
- !Ref ApplicationAutoscalingSecurityGroup
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} \
--resource ApplicationAutoscalingLaunchConfiguration \
--region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} \
--resource ApplicationAutoscaling \
--region ${AWS::Region}
ApplicationAutoscaling:
Type: AWS::AutoScaling::AutoScalingGroup
CreationPolicy:
ResourceSignal:
Count: !Ref ApplicationDesiredCount
Timeout: PT15M
Properties:
LaunchConfigurationName: !Ref ApplicationAutoscalingLaunchConfiguration
MinSize: 0
MaxSize: 4
DesiredCapacity: !Ref ApplicationDesiredCount
VPCZoneIdentifier: !Ref ApplicationSubnets
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-ApplicationAutoscaling-instance
PropagateAtLaunch: "true"
Outputs:
PublicURL:
Description: Public DNS name of Application Load Balancer
Value: !Sub ${ApplicationLoadBalancer.DNSName}