-
Notifications
You must be signed in to change notification settings - Fork 7
/
cloudformation-template.yaml
1196 lines (1111 loc) · 33.2 KB
/
cloudformation-template.yaml
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Parameters:
Ami:
Type: String
Description: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI_launch_latest.html
AsgMaxSize:
Type: Number
Description: The maximum size of ASG
Default: 6
AsgMinSize:
Type: Number
Description: The minimum size of ASG
Default: 2
CadvisorImage:
Type: String
Default: google/cadvisor:latest
EmailAlerts:
Type: String
Default: email_for_alerts@domain.com
InstanceType:
Type: String
Default: t2.medium
AllowedValues:
- t2.micro
- t2.medium
- t2.small
- t2.large
- m3.medium
- m3.large
- m3.xlarge
- m3.2xlarge
- m4.large
- m4.xlarge
- m4.2xlarge
- m4.4xlarge
- m4.10xlarge
- c4.large
- c4.xlarge
- c4.2xlarge
- c4.4xlarge
- c4.8xlarge
- c3.large
- c3.xlarge
- c3.2xlarge
- c3.4xlarge
- c3.8xlarge
- r3.large
- r3.xlarge
- r3.2xlarge
- r3.4xlarge
- r3.8xlarge
- i2.xlarge
- i2.2xlarge
- i2.4xlarge
- i2.8xlarge
- g2.2xlarge
- g2.8xlarge
- d2.xlarge
- d2.2xlarge
- d2.4xlarge
- d2.8xlarge
KeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: Name of an existing EC2 KeyPair to enable SSH access to instances
MYSQLDATABASE:
Type: String
Default: wordpress
MYSQLUSER:
Type: String
Default: wordpress
MYSQLPASSWORD:
Type: String
NoEcho: true
NodeExporterImage:
Type: String
Default: quay.io/prometheus/node-exporter:latest
PcpImage:
Type: String
Default: mmitrofan/pcp:latest
ProdWordpressImage:
Type: String
Default: wordpress
UatWordpressImage:
Type: String
Default: wordpress
SubnetID1:
Type: AWS::EC2::Subnet::Id
SubnetID2:
Type: AWS::EC2::Subnet::Id
VpcId:
Type: AWS::EC2::VPC::Id
Resources:
EcsCluster:
Type: AWS::ECS::Cluster
Sg:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription:
Fn::Sub: ${AWS::StackName}-Sg
VpcId: !Ref VpcId
SgSSHports:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'Sg'
IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
SgAllTrafficports:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'Sg'
IpProtocol: tcp
FromPort: '0'
ToPort: '65535'
SourceSecurityGroupId: !Ref 'Sg'
SgALBAllTrafficports:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'Sg'
IpProtocol: tcp
FromPort: '0'
ToPort: '65535'
SourceSecurityGroupId: !Ref 'EcsSecurityGroup'
AlertsTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref EmailAlerts
Protocol: email
TopicName:
Fn::Sub: ${AWS::StackName}-AlertsTopic
HostRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
- autoscaling.amazonaws.com
- ecs-tasks.amazonaws.com
- s3.amazonaws.com
- cloudformation.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
Policies:
- PolicyName:
Fn::Sub: ${AWS::StackName}-DescribeAWSPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- autoscaling:DescribeAutoScalingGroups
- autoscaling:CompleteLifecycleAction
- cloudformation:DescribeStacks
- cloudformation:DescribeStackResources
- cloudwatch:GetMetricStatistics
- cloudwatch:ListMetrics
- ec2:DescribeInstances
- ec2:DescribeTags
- ec2:DescribeRegions
- ec2:DescribeSecurityGroups
- ec2:DescribeSpotPriceHistory
- ec2:DescribeAvailabilityZones
- elasticloadbalancing:DescribeLoadBalancers
Resource: "*"
- PolicyName:
Fn::Sub: ${AWS::StackName}-LogsPolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:*
Resource: "*"
- PolicyName:
Fn::Sub: ${AWS::StackName}-SendMessagesPolicy
PolicyDocument:
Statement:
- Action: sns:Publish
Resource:
Ref: AlertsTopic
Effect: Allow
- PolicyName:
Fn::Sub: ${AWS::StackName}-DumpBackupS3
PolicyDocument:
Statement:
- Action: s3:*
Resource: !GetAtt MySQLBackup.Arn
Effect: Allow
- Action: s3:*
Resource: !Sub
- ${Bucket}/*
- { Bucket: !GetAtt MySQLBackup.Arn }
Effect: Allow
- PolicyName:
Fn::Sub: ${AWS::StackName}-GetCloudformation
PolicyDocument:
Statement:
- Action:
- cloudformation:DescribeStacks
- cloudformation:DescribeStackEvents
- cloudformation:DescribeStackResource
- cloudformation:DescribeStackResources
- cloudformation:GetTemplate
- cloudformation:List*
Resource: "*"
Effect: Allow
HostProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- Ref: HostRole
FileSystemMySQLProd:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
MountTargetMySQLProd1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemMySQLProd
SubnetId: !Ref SubnetID1
SecurityGroups:
- !GetAtt Sg.GroupId
MountTargetMySQLProd2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemMySQLProd
SubnetId: !Ref SubnetID2
SecurityGroups:
- !GetAtt Sg.GroupId
FileSystemMySQLUat:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
MountTargetMySQLUat1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemMySQLUat
SubnetId: !Ref SubnetID1
SecurityGroups:
- !GetAtt Sg.GroupId
MountTargetMySQLUat2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemMySQLUat
SubnetId: !Ref SubnetID2
SecurityGroups:
- !GetAtt Sg.GroupId
FileSystemWPProd:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
MountTargetWPProd1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemWPProd
SubnetId: !Ref SubnetID1
SecurityGroups:
- !GetAtt Sg.GroupId
MountTargetWPProd2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemWPProd
SubnetId: !Ref SubnetID2
SecurityGroups:
- !GetAtt Sg.GroupId
FileSystemWPUat:
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
MountTargetWPUat1:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemWPUat
SubnetId: !Ref SubnetID1
SecurityGroups:
- !GetAtt Sg.GroupId
MountTargetWPUat2:
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref FileSystemWPUat
SubnetId: !Ref SubnetID2
SecurityGroups:
- !GetAtt Sg.GroupId
MySQLBackup:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Asg:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref SubnetID1
- !Ref SubnetID2
NotificationConfiguration:
TopicARN:
Ref: AlertsTopic
NotificationTypes:
- autoscaling:EC2_INSTANCE_LAUNCH
- autoscaling:EC2_INSTANCE_TERMINATE
- autoscaling:EC2_INSTANCE_LAUNCH_ERROR
- autoscaling:EC2_INSTANCE_TERMINATE_ERROR
HealthCheckGracePeriod: '600'
HealthCheckType: EC2
LaunchConfigurationName:
Ref: Lc
MinSize:
Ref: AsgMinSize
MaxSize:
Ref: AsgMaxSize
DesiredCapacity:
Ref: AsgMinSize
Tags:
- Key: Name
Value:
Fn::Sub: EC2ContainerService-${EcsCluster}
PropagateAtLaunch: true
PropagateAtLaunch: true
- Key: Creator
Value:
Ref: KeyName
PropagateAtLaunch: true
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService:
Ref: AsgMinSize
PauseTime: PT5M
Lc:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId:
Ref: Ami
BlockDeviceMappings:
- DeviceName: /dev/xvdcy
Ebs:
VolumeSize: 100
VolumeType: gp2
InstanceType:
Ref: InstanceType
IamInstanceProfile:
Ref: HostProfile
KeyName:
Ref: KeyName
SecurityGroups:
- Ref: Sg
UserData:
Fn::Base64:
Fn::Sub: |
#cloud-config
package_upgrade: true
packages:
- aws-cli
- ntp
- nfs-utils
- mysql
- wget
write_files:
- path: /etc/cron.d/db-backup
content: |
0 0 */3 0 0 root mysqldump --add-drop-table -u ${MYSQLUSER} -p${MYSQLPASSWORD} -h $(aws cloudformation describe-stacks --stack-name ${AWS::StackName} --query "Stacks[0].Outputs[?OutputKey=='MySQLProd'] | [0].OutputValue" --output text) wp > /opt/prod.sql
30 0 */3 0 0 root mysqldump --add-drop-table -u ${MYSQLUSER} -p${MYSQLPASSWORD} -h $(aws cloudformation describe-stacks --stack-name ${AWS::StackName} --query "Stacks[0].Outputs[?OutputKey=='MySQLUat'] | [0].OutputValue" --output text) wp > /opt/uat.sql
0 1 */3 0 0 root aws s3 sync /opt/prod.sql s3://${MySQLBackup}/$(HOSTNAME)/$(date)/prod.sql
30 1 */3 0 0 root aws s3 sync /opt/uat.sql s3://${MySQLBackup}/$(HOSTNAME)/$(date)/uat.sql
permissions: '0600'
- path: /etc/ecs/ecs.config
content: |
ECS_AVAILABLE_LOGGING_DRIVERS=["json-file","gelf","awslogs"]
ECS_CLUSTER=${EcsCluster}
ECS_CONTAINER_STOP_TIMEOUT=125s
ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION=12h
ECS_DISABLE_IMAGE_CLEANUP=false
ECS_ECS_IMAGE_CLEANUP_INTERVAL=15m
ECS_NUM_IMAGES_DELETE_PER_CYCLE=10
runcmd:
- vgextend docker /dev/xvdcy
- lvextend -L+100G /dev/docker/docker-pool
- mkdir -p /mnt/mysql_prod
- mkdir -p /mnt/mysql_uat
- mkdir -p /mnt/wordpress_prod
- mkdir -p /mnt/wordpress_uat
- mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 ${FileSystemMySQLProd}.efs.${AWS::Region}.amazonaws.com:/ /mnt/mysql_prod
- mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 ${FileSystemMySQLUat}.efs.${AWS::Region}.amazonaws.com:/ /mnt/mysql_uat
- mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 ${FileSystemWPProd}.efs.${AWS::Region}.amazonaws.com:/ /mnt/wordpress_prod
- mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 ${FileSystemWPUat}.efs.${AWS::Region}.amazonaws.com:/ /mnt/wordpress_uat
- echo '${FileSystemMySQLProd}.efs.${AWS::Region}.amazonaws.com:/ /mnt/mysql_prod nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' | tee -a /etc/fstab
- echo '${FileSystemMySQLUat}.efs.${AWS::Region}.amazonaws.com:/ /mnt/mysql_uat nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' | tee -a /etc/fstab
- echo '${FileSystemWPProd}.efs.${AWS::Region}.amazonaws.com:/ /mnt/wordpress_prod nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' | tee -a /etc/fstab
- echo '${FileSystemWPUat}.efs.${AWS::Region}.amazonaws.com:/ /mnt/wordpress_uat nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 0 0' | tee -a /etc/fstab
- stop ecs
- service docker restart
- start ecs
CpuUtilization:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName:
Ref: Asg
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
TargetValue: 70
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
MemoryUtilization:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName:
Ref: Asg
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
CustomizedMetricSpecification:
MetricName: MemoryUtilization
Namespace: AWS/ECS
Dimensions:
- Name: ClusterName
Value:
Ref: EcsCluster
Statistic: Maximum
TargetValue: 80
NetworkInAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- Ref: AlertsTopic
MetricName: NetworkIn
ComparisonOperator: GreaterThanThreshold
AlarmDescription: High network input traffic during 15 minutes
Statistic: Average
Period: '300'
Dimensions:
- Value:
Ref: Asg
Name: AutoScalingGroupName
EvaluationPeriods: '3'
InsufficientDataActions:
- Ref: AlertsTopic
Namespace: AWS/EC2
Threshold: '2400000000'
RAMAlarmHigh:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- Ref: AlertsTopic
MetricName: MemoryUtilization
ComparisonOperator: GreaterThanThreshold
AlarmDescription: Memory utilization > 85% during 15 minutes
Statistic: Average
Period: '300'
Dimensions:
- Value:
Ref: Asg
Name: AutoScalingGroupName
EvaluationPeriods: '3'
InsufficientDataActions:
- Ref: AlertsTopic
Namespace: System/Linux
Threshold: '85'
NetworkOutAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- Ref: AlertsTopic
MetricName: NetworkOut
ComparisonOperator: GreaterThanThreshold
AlarmDescription: High network output traffic during 15 minutes
Statistic: Average
Period: '300'
Dimensions:
- Value:
Ref: Asg
Name: AutoScalingGroupName
EvaluationPeriods: '3'
InsufficientDataActions:
- Ref: AlertsTopic
Namespace: AWS/EC2
Threshold: '2400000000'
IOWaitAlarmHigh:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- Ref: AlertsTopic
MetricName: IOWait
ComparisonOperator: GreaterThanThreshold
AlarmDescription: Alarm if IOWait > 85% for 15 minutes
Statistic: Average
Period: '300'
Dimensions:
- Value:
Ref: AWS::StackName
Name: StackName
EvaluationPeriods: '3'
InsufficientDataActions:
- Ref: AlertsTopic
Namespace: AWS/EC2
Threshold: '85'
StatusAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- Ref: AlertsTopic
MetricName: StatusCheckFailed
ComparisonOperator: GreaterThanOrEqualToThreshold
AlarmDescription: Alert if asg has degraded instances
Statistic: Maximum
Period: '60'
Dimensions:
- Value:
Ref: Asg
Name: AutoScalingGroupName
EvaluationPeriods: '2'
InsufficientDataActions:
- Ref: AlertsTopic
Unit: Count
Namespace: AWS/EC2
Threshold: '1'
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName:
Fn::Sub: ${AWS::StackName}-LogGroup
RetentionInDays: 14
ProdWordpressTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: wordpress
Image:
Ref: ProdWordpressImage
Memory: 256
PortMappings:
- ContainerPort: 80
MountPoints:
- ContainerPath: /var/www/html/wp-content
SourceVolume: data
Environment:
- Name: WORDPRESS_DB_HOST
Value: !GetAtt ECSELBMySQLProd.DNSName
- Name: WORDPRESS_DB_USER
Value: !Ref MYSQLUSER
- Name: WORDPRESS_DB_PASSWORD
Value: !Ref MYSQLPASSWORD
- Name: WORDPRESS_DB_NAME
Value: !Ref MYSQLDATABASE
- Name: WORDPRESS_TABLE_PREFIX
Value: wp
- Name: WORDPRESS_AUTH_KEY
Value: '9aaeVk9aKb5mkK452aPa1ragaBPxaAsZMaaIxjaaaDEbthax4ZaaJqRaraBOWaZa'
- Name: WORDPRESS_SECURE_AUTH_KEY
Value: 'atoHakFXaaamqkbY32uZaaaiaa3J1FanJQQsv97baa2aRaaaaatUaGwfUBa1Lmdp'
- Name: WORDPRESS_LOGGED_IN_KEY
Value: 'a1aSaaaaybanZwYaaaaaalaRaSdjTHU0FM9OaaNAVIaaaaa6qaa1aajS2Uy2kaEq'
- Name: WORDPRESS_NONCE_KEY
Value: 'mC8aWvM3a5fadaHaGawsaQiBskaaaraaafPZJQdRaatvQtaOakaaaaQZEaNfafaa'
- Name: WORDPRESS_AUTH_SALT
Value: 'EaAwWaansaa3a3aa1LQrf2af5cLEaaZqabpG6K2adQaaaRJRaaaa8ACwaKYXaaV]'
- Name: WORDPRESS_SECURE_AUTH_SALT
Value: 'aauUa7GIpohbabBaza10GalaisfXDaJIa9m43aFCa90lhaxjg3aPaLnnaaaVQaoQ'
- Name: WORDPRESS_LOGGED_IN_SALT
Value: 'saK94aaaaaa4DyoayGAaaOaKaYQaOaKaXQCQWacpa3uabaqF?7sMaauOdaXaopL2'
- Name: WORDPRESS_NONCE_SALT
Value: 'qaVq8rOcahaagaaa9AaoRh7d7SYJka2irwatunaaavaaaH6aKa5IJXaJu4laaaq^'
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region:
Ref: AWS::Region
awslogs-group: !Ref LogGroup
awslogs-stream-prefix:
Ref: AWS::StackName
Volumes:
- Name: data
Host:
SourcePath: /mnt/wordpress_prod
UatWordpressTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: wordpress
Image:
Ref: UatWordpressImage
Memory: 256
PortMappings:
- ContainerPort: 80
MountPoints:
- ContainerPath: /var/www/html/wp-content
SourceVolume: data
Environment:
- Name: WORDPRESS_DB_HOST
Value: !GetAtt ECSELBMySQLUat.DNSName
- Name: WORDPRESS_DB_USER
Value: !Ref MYSQLUSER
- Name: WORDPRESS_DB_PASSWORD
Value: !Ref MYSQLPASSWORD
- Name: WORDPRESS_DB_NAME
Value: !Ref MYSQLDATABASE
- Name: WORDPRESS_TABLE_PREFIX
Value: wp
- Name: WORDPRESS_AUTH_KEY
Value: '9aaeVk9aKb5mkK452aPa1ragaBPxaAsZMaaIxjaaaDEbthax4ZaaJqRaraBOWaZa'
- Name: WORDPRESS_SECURE_AUTH_KEY
Value: 'atoHakFXaaamqkbY32uZaaaiaa3J1FanJQQsv97baa2aRaaaaatUaGwfUBa1Lmdp'
- Name: WORDPRESS_LOGGED_IN_KEY
Value: 'a1aSaaaaybanZwYaaaaaalaRaSdjTHU0FM9OaaNAVIaaaaa6qaa1aajS2Uy2kaEq'
- Name: WORDPRESS_NONCE_KEY
Value: 'mC8aWvM3a5fadaHaGawsaQiBskaaaraaafPZJQdRaatvQtaOakaaaaQZEaNfafaa'
- Name: WORDPRESS_AUTH_SALT
Value: 'EaAwWaansaa3a3aa1LQrf2af5cLEaaZqabpG6K2adQaaaRJRaaaa8ACwaKYXaaV]'
- Name: WORDPRESS_SECURE_AUTH_SALT
Value: 'aauUa7GIpohbabBaza10GalaisfXDaJIa9m43aFCa90lhaxjg3aPaLnnaaaVQaoQ'
- Name: WORDPRESS_LOGGED_IN_SALT
Value: 'saK94aaaaaa4DyoayGAaaOaKaYQaOaKaXQCQWacpa3uabaqF?7sMaauOdaXaopL2'
- Name: WORDPRESS_NONCE_SALT
Value: 'qaVq8rOcahaagaaa9AaoRh7d7SYJka2irwatunaaavaaaH6aKa5IJXaJu4laaaq^'
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region:
Ref: AWS::Region
awslogs-group: !Ref LogGroup
awslogs-stream-prefix:
Ref: AWS::StackName
Volumes:
- Name: data
Host:
SourcePath: /mnt/wordpress_uat
ApplicationScalingRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- application-autoscaling.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: !Ref AWS::StackName
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "application-autoscaling:*"
- "cloudwatch:DescribeAlarms"
- "cloudwatch:PutMetricAlarm"
- "ecs:DescribeServices"
- "ecs:UpdateService"
Resource: "*"
ECSServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [ecs.amazonaws.com]
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: ecs-service
PolicyDocument:
Statement:
- Effect: Allow
Action: ['elasticloadbalancing:DeregisterInstancesFromLoadBalancer', 'elasticloadbalancing:DeregisterTargets',
'elasticloadbalancing:Describe*', 'elasticloadbalancing:RegisterInstancesWithLoadBalancer',
'elasticloadbalancing:RegisterTargets', 'ec2:Describe*', 'ec2:AuthorizeSecurityGroupIngress']
Resource: '*'
ProdWordpressService:
Type: AWS::ECS::Service
DependsOn: ECSALBListenerRuleProd
Properties:
Cluster: !Ref EcsCluster
LoadBalancers:
- ContainerName: wordpress
ContainerPort: '80'
TargetGroupArn: !Ref 'EcsTargetGroupProd'
DeploymentConfiguration:
MaximumPercent: 100
MinimumHealthyPercent: 50
TaskDefinition: !Ref ProdWordpressTaskDefinition
Role: !Ref 'ECSServiceRole'
DesiredCount: !Ref AsgMinSize
UatWordpressService:
Type: AWS::ECS::Service
DependsOn: ECSALBListenerRuleUat
Properties:
Cluster: !Ref EcsCluster
LoadBalancers:
- ContainerName: wordpress
ContainerPort: '80'
TargetGroupArn: !Ref 'EcsTargetGroupUat'
DeploymentConfiguration:
MaximumPercent: 100
MinimumHealthyPercent: 50
TaskDefinition: !Ref UatWordpressTaskDefinition
Role: !Ref 'ECSServiceRole'
DesiredCount: !Ref AsgMinSize
EcsSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: ECS Allowed Ports
VpcId: !Ref VpcId
EcsSecurityGroupHTTPinbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
EcsSecurityGroupHTTPSinbound:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '443'
ToPort: '443'
CidrIp: 0.0.0.0/0
EcsSecurityGroupECSAllTrafficports:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '0'
ToPort: '65535'
SourceSecurityGroupId: !Ref 'Sg'
EcsSecurityGroupALBAllTrafficports:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'EcsSecurityGroup'
IpProtocol: tcp
FromPort: '0'
ToPort: '65535'
SourceSecurityGroupId: !Ref 'EcsSecurityGroup'
ECSALBProd:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name:
Fn::Sub: ${AWS::StackName}-wordpress-prod
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: '30'
Subnets:
- !Ref SubnetID1
- !Ref SubnetID2
SecurityGroups: [ !Ref EcsSecurityGroup ]
ECSALBUat:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name:
Fn::Sub: ${AWS::StackName}-wordpress-uat
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: '30'
Subnets:
- !Ref SubnetID1
- !Ref SubnetID2
SecurityGroups: [ !Ref EcsSecurityGroup ]
ALBListenerProd:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn: ECSServiceRole
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref 'EcsTargetGroupProd'
LoadBalancerArn: !Ref 'ECSALBProd'
Port: '80'
Protocol: HTTP
ALBListenerUat:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn: ECSServiceRole
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref 'EcsTargetGroupUat'
LoadBalancerArn: !Ref 'ECSALBUat'
Port: '80'
Protocol: HTTP
ECSALBListenerRuleProd:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
DependsOn: ALBListenerProd
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref 'EcsTargetGroupProd'
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref 'ALBListenerProd'
Priority: 1
ECSALBListenerRuleUat:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
DependsOn: ALBListenerUat
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref 'EcsTargetGroupUat'
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref 'ALBListenerUat'
Priority: 1
EcsTargetGroupProd:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: ECSALBProd
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
Matcher:
HttpCode: '200,301,302,346'
Name:
Fn::Sub: ${AWS::StackName}-wordpress-prod
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 2
VpcId: !Ref VpcId
EcsTargetGroupUat:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: ECSALBUat
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 2
Matcher:
HttpCode: '200,302,346'
Name:
Fn::Sub: ${AWS::StackName}-wordpress-uat
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 2
VpcId: !Ref VpcId
ProdMySQLTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
NetworkMode: host
ContainerDefinitions:
- Name: mysql
Image: mysql:5.7
Memory: 256
PortMappings:
- ContainerPort: 3306
MountPoints:
- ContainerPath: /var/lib/mysql
SourceVolume: data
Environment:
- Name: MYSQL_RANDOM_ROOT_PASSWORD
Value: "yes"
- Name: MYSQL_DATABASE
Value: !Ref MYSQLDATABASE
- Name: MYSQL_USER
Value: !Ref MYSQLUSER
- Name: MYSQL_PASSWORD
Value: !Ref MYSQLPASSWORD
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region:
Ref: AWS::Region
awslogs-group: !Ref LogGroup
awslogs-stream-prefix:
Ref: AWS::StackName
Volumes:
- Name: data
Host:
SourcePath: /mnt/mysql_prod
UatMySQLTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
NetworkMode: host
ContainerDefinitions:
- Name: mysql
Image: mysql:5.7
Memory: 256
PortMappings:
- ContainerPort: 3306
MountPoints:
- ContainerPath: /var/lib/mysql
SourceVolume: data
Environment:
- Name: MYSQL_RANDOM_ROOT_PASSWORD
Value: "yes"
- Name: MYSQL_DATABASE
Value: !Ref MYSQLDATABASE
- Name: MYSQL_USER
Value: !Ref MYSQLUSER
- Name: MYSQL_PASSWORD
Value: !Ref MYSQLPASSWORD
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region:
Ref: AWS::Region
awslogs-group: !Ref LogGroup
awslogs-stream-prefix:
Ref: AWS::StackName
Volumes:
- Name: data
Host:
SourcePath: /mnt/mysql_uat
ECSELBMySQLProd:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
LoadBalancerName:
Fn::Sub: ${AWS::StackName}-mysql-prod
Subnets:
- !Ref SubnetID1
- !Ref SubnetID2
SecurityGroups: [ !Ref EcsSecurityGroup ]
Scheme: internal
HealthCheck:
Target: TCP:3306
HealthyThreshold: '2'
UnhealthyThreshold: '10'
Interval: '30'
Timeout: '5'
Listeners:
- LoadBalancerPort: '3306'
InstancePort: '3306'
Protocol: TCP
ECSELBMySQLUat:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
LoadBalancerName: