-
Notifications
You must be signed in to change notification settings - Fork 4
/
infrastructure.template
1894 lines (1805 loc) · 66.5 KB
/
infrastructure.template
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
# vim:set ft=yaml ts=2 sts=2 sw=2 et:
AWSTemplateFormatVersion: 2010-09-09
Description: 'Web Hosting: Infrastructure (meta-stack)'
Parameters:
VpcName:
Description: The Name tag of the VPC (will use the name of the stack if empty)
Type: String
Default: ''
CidrBlock:
Description: The IP address range for the VPC
Type: String
MinLength: 9
MaxLength: 18
Default: 10.0.0.0/16
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
AvailabilityZones:
Description: List of Availability Zones this infrastructure will be deployed to
Type: CommaDelimitedList
Default: ''
AvailabilityZonesCount:
Description: The number of Availability Zones this infrastructure will be deployed to
Type: Number
MinValue: 1
Default: 2
KeyName:
Description: SSH key registered with AWS EC2
Type: String
Default: ''
DatabaseEngine:
Description: RDS Engine to use
Type: String
Default: ''
Encryption:
Description: Encryption configuration for the stack
Type: String
AllowedValues:
- ''
- builtin
- custom-key
- custom-keys
Default: ''
BastionImageId:
Description: AMI ID for the bastion instance
Type: String
Default: ''
AppImageId:
Description: AMI ID for the application instances
Type: String
Default: ''
WebImageId:
Description: AMI ID for the web instances
Type: String
Default: ''
Mappings:
DatabaseConfig:
mysql:
Port: 3306
postgres:
Port: 5432
Conditions:
useAvailabilityZones: !Not [ !Equals [ '', !Join [ '', !Ref AvailabilityZones ]]]
useAZ9: !Equals [ 9, !Ref AvailabilityZonesCount ]
useAZ8: !Or [ !Equals [ 8, !Ref AvailabilityZonesCount ], Condition: useAZ9 ]
useAZ7: !Or [ !Equals [ 7, !Ref AvailabilityZonesCount ], Condition: useAZ8 ]
useAZ6: !Or [ !Equals [ 6, !Ref AvailabilityZonesCount ], Condition: useAZ7 ]
useAZ5: !Or [ !Equals [ 5, !Ref AvailabilityZonesCount ], Condition: useAZ6 ]
useAZ4: !Or [ !Equals [ 4, !Ref AvailabilityZonesCount ], Condition: useAZ5 ]
useAZ3: !Or [ !Equals [ 3, !Ref AvailabilityZonesCount ], Condition: useAZ4 ]
useAZ2: !Or [ !Equals [ 2, !Ref AvailabilityZonesCount ], Condition: useAZ3 ]
useAZ1: !Or [ !Equals [ 1, !Ref AvailabilityZonesCount ], Condition: useAZ2 ]
useKeyName: !Not [ !Equals [ '', !Ref KeyName ]]
useEncryption: !Or [ !Not [ !Equals [ '', !Ref Encryption ]], !Equals [ 'builtin', !Ref Encryption ]]
useSingleCustomKey: !Equals [ 'custom-key', !Ref Encryption ]
useSeparateCustomKeys: !Equals [ 'custom-keys', !Ref Encryption ]
useDatabase: !Not [ !Equals [ '', !Ref DatabaseEngine ]]
useBastionImageId: !Not [ !Equals [ '', !Ref BastionImageId ]]
useAppImageId: !Not [ !Equals [ '', !Ref AppImageId ]]
useWebImageId: !Not [ !Equals [ '', !Ref WebImageId ]]
Resources:
StackRole:
DependsOn: CloudFormationServiceRole
Type: AWS::CloudFormation::WaitConditionHandle
keyEFS:
Condition: useSeparateCustomKeys
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/kms.template
Parameters:
Service: elasticfilesystem
KeyDescription: A key for encrypting EFS
EnableKeyRotation: true
# XXX: add SNS alerting?
# AlertTopic: !Ref snsKMS
keyRDS:
Condition: useSeparateCustomKeys
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/kms.template
Parameters:
Service: rds
KeyDescription: A key for encrypting RDS
EnableKeyRotation: true
# XXX: add SNS alerting?
# AlertTopic: !Ref snsKMS
keyS3:
Condition: useSeparateCustomKeys
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/kms.template
Parameters:
Service: s3
KeyDescription: A key for encrypting S3 buckets
EnableKeyRotation: true
# XXX: add SNS alerting?
# AlertTopic: !Ref snsKMS
keySM:
Condition: useSeparateCustomKeys
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/kms.template
Parameters:
Service: secretsmanager
KeyDescription: A key for encrypting secrets in SecretsManager
EnableKeyRotation: true
# XXX: add SNS alerting?
# AlertTopic: !Ref snsKMS
keyCustom:
Condition: useSingleCustomKey
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/kms.template
Parameters:
Service: ALL_SERVICES
KeyDescription: A custom key for encrypting resources
EnableKeyRotation: true
# XXX: add SNS alerting?
# AlertTopic: !Ref snsKMS
ArtifactStore:
DependsOn: StackRole
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
BucketEncryption: !If
- useEncryption
- ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
KMSMasterKeyID: !If
- useSeparateCustomKeys
- !GetAtt [ keyS3, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
SSEAlgorithm: aws:kms
- !Ref 'AWS::NoValue'
LifecycleConfiguration:
Rules:
- Prefix: ''
Status: Enabled
AbortIncompleteMultipartUpload:
DaysAfterInitiation: 1
NoncurrentVersionExpirationInDays: 6
VersioningConfiguration:
# Required for CodePipeline (XXX: is it now?)
Status: Enabled
Repository:
DependsOn: StackRole
Type: AWS::CodeCommit::Repository
Properties:
RepositoryName: !Sub ${AWS::StackName}
VPC:
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/vpc.template
Parameters:
Name: !Ref VpcName
CidrBlock: !Ref CidrBlock
AvailabilityZones: !If
- useAvailabilityZones
- !Join [ ',', !Ref AvailabilityZones ]
- !Join [ ',', !GetAZs '' ]
AvailabilityZonesCount: !Ref AvailabilityZonesCount
EnableDnsSupport: true
EnableDnsHostnames: true
TimeoutInMinutes: 15
VpcSgDefault:
Type: AWS::CloudFormation::Stack
DependsOn: VPC
Properties:
TemplateURL: library/vpc-sg.template
Parameters:
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
TimeoutInMinutes: 15
sgIngressNatMail:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 25
ToPort: 25
CidrIp: !Ref CidrBlock
sgEgressNatMail:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 587
ToPort: 587
CidrIp: 0.0.0.0/0
sgIngressNatHttp:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref CidrBlock
sgEgressNatHttp:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
sgIngressNatHttps:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref CidrBlock
sgEgressNatHttps:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
sgEgressNatSshApp:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
Description: Allow SSH to App instances through NAT
IpProtocol: tcp
FromPort: 22
ToPort: 22
DestinationSecurityGroupId: !Ref sgAppInstances
sgEgressNatSshWeb:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
Description: Allow SSH to Web instances through NAT
IpProtocol: tcp
FromPort: 22
ToPort: 22
DestinationSecurityGroupId: !Ref sgWebInstances
sgEgressNatEfs:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
Description: EFS
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
DestinationSecurityGroupId: !Ref sgEfsMountPoint
sgAppInstances:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Application instances
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: EFS
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
DestinationSecurityGroupId: !Ref sgEfsMountPoint
Tags:
- Key: Name
Value: app
sgWebInstances:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Web instances
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: EFS
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
DestinationSecurityGroupId: !Ref sgEfsMountPoint
Tags:
- Key: Name
Value: web
sgEgressWebToAppElb:
Type: AWS::EC2::SecurityGroupEgress
Properties:
GroupId: !Ref sgWebInstances
Description: Allows Web instances to talk to App ELB
IpProtocol: tcp
FromPort: 9000
ToPort: 9000
DestinationSecurityGroupId: !Ref sgAppElb
sgAppElb:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the internal application NLB
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 9000
ToPort: 9000
SourceSecurityGroupId: !Ref sgWebInstances
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 9000
ToPort: 9000
DestinationSecurityGroupId: !Ref sgAppInstances
Tags:
- Key: Name
Value: elb/app
sgWebElb:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the public web ALB
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIpv6: ::/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIpv6: ::/0
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
DestinationSecurityGroupId: !Ref sgWebInstances
Tags:
- Key: Name
Value: elb/web
sgDatabase:
Condition: useDatabase
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the RDS instance
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !FindInMap [DatabaseConfig, !Ref DatabaseEngine, Port]
ToPort: !FindInMap [DatabaseConfig, !Ref DatabaseEngine, Port]
SourceSecurityGroupId: !Ref sgAppInstances
- IpProtocol: tcp
FromPort: !FindInMap [DatabaseConfig, !Ref DatabaseEngine, Port]
ToPort: !FindInMap [DatabaseConfig, !Ref DatabaseEngine, Port]
SourceSecurityGroupId: !Ref sgWebInstances
SecurityGroupEgress:
- IpProtocol: '-1'
FromPort: '-1'
ToPort: '-1'
CidrIp: 127.0.0.1/32
Tags:
- Key: Name
Value: in/rds
sgFastCGI:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the FastCGI interface
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 9000
ToPort: 9000
SourceSecurityGroupId: !Ref sgAppElb
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 9000
ToPort: 9000
DestinationSecurityGroupId: !Ref sgAppElb
Tags:
- Key: Name
Value: fastcgi
sgOut:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines a set of standard outbound rules for updates
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: HTTP
IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- Description: HTTPS
IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: out
sgDirectoryOut:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the directory service
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: DNS
IpProtocol: tcp
FromPort: 53
ToPort: 53
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: DNS
IpProtocol: udp
FromPort: 53
ToPort: 53
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos authentication
IpProtocol: tcp
FromPort: 88
ToPort: 88
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos authentication
IpProtocol: udp
FromPort: 88
ToPort: 88
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: NTP
IpProtocol: udp
FromPort: 123
ToPort: 123
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: LDAP
IpProtocol: tcp
FromPort: 389
ToPort: 389
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: LDAP
IpProtocol: udp
FromPort: 389
ToPort: 389
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos Password Change
IpProtocol: udp
FromPort: 464
ToPort: 464
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Global Catalog from client to domain controller
IpProtocol: tcp
FromPort: 3268
ToPort: 3269
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: DNS
IpProtocol: tcp
FromPort: 53
ToPort: 53
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: DNS
IpProtocol: udp
FromPort: 53
ToPort: 53
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos authentication
IpProtocol: tcp
FromPort: 88
ToPort: 88
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos authentication
IpProtocol: udp
FromPort: 88
ToPort: 88
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: NTP
IpProtocol: udp
FromPort: 123
ToPort: 123
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: LDAP
IpProtocol: tcp
FromPort: 389
ToPort: 389
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: LDAP
IpProtocol: udp
FromPort: 389
ToPort: 389
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Kerberos Password Change
IpProtocol: udp
FromPort: 464
ToPort: 464
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: Global Catalog from client to domain controller
IpProtocol: tcp
FromPort: 3268
ToPort: 3269
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
Tags:
- Key: Name
Value: out/directory
sgDirectoryOutSMB:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Defines access to the directory service via SMB
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: NBT
IpProtocol: tcp
FromPort: 139
ToPort: 139
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: SMB
IpProtocol: tcp
FromPort: 445
ToPort: 445
CidrIp: !Join [ '', [ !Select [ 0, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: NBT
IpProtocol: tcp
FromPort: 139
ToPort: 139
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
- Description: SMB
IpProtocol: tcp
FromPort: 445
ToPort: 445
CidrIp: !Join [ '', [ !Select [ 1, !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]], /32 ]]
sgEfsMountPoint:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Suppresses the default free for all rule
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
SecurityGroupEgress:
- Description: Egress follows the whitelist approach
IpProtocol: '-1'
FromPort: '-1'
ToPort: '-1'
CidrIp: 127.0.0.1/32
Tags:
- Key: Name
Value: in/efs
sgEfsMountPointAppRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Allows application instances to mount EFS
GroupId: !Ref sgEfsMountPoint
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
SourceSecurityGroupId: !Ref sgAppInstances
sgEfsMountPointWebRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Allows web instances to mount EFS
GroupId: !Ref sgEfsMountPoint
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
SourceSecurityGroupId: !Ref sgWebInstances
# XXX: We are abusing Nat to do the EFS initialisation
sgEfsMountPointNatRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Allows NAT instances to mount EFS
GroupId: !Ref sgEfsMountPoint
IpProtocol: tcp
FromPort: 2049
ToPort: 2049
SourceSecurityGroupId: !GetAtt [ VpcSgDefault, Outputs.nat ]
efsFileSystem:
DependsOn: StackRole
Type: AWS::EFS::FileSystem
Properties:
PerformanceMode: generalPurpose
FileSystemTags:
- Key: Name
Value: !Sub Shared Storage for ${AWS::StackName}
Encrypted: !If
- useEncryption
- true
- false
KmsKeyId: !If
- useEncryption
- !If
- useSeparateCustomKeys
- !GetAtt [ keyEFS, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
efsMountTarget1:
Condition: useAZ1
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 0, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget2:
Condition: useAZ2
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 1, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget3:
Condition: useAZ3
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 2, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget4:
Condition: useAZ4
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 3, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget5:
Condition: useAZ5
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 4, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget6:
Condition: useAZ6
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 5, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget7:
Condition: useAZ7
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 6, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget8:
Condition: useAZ8
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 7, !Split [ ',', !GetAtt VPC.Outputs.SubnetPrivateIds ]]
SecurityGroups:
- !Ref sgEfsMountPoint
efsMountTarget9:
Condition: useAZ9
Type: AWS::EFS::MountTarget
Properties:
FileSystemId: !Ref efsFileSystem
SubnetId: !Select [ 8, !Split [ ',', !GetAtt VPC.Outputs.SubnetPrivateIds ]]
SecurityGroups:
- !Ref sgEfsMountPoint
PrivateZone:
DependsOn: StackRole
Type: AWS::Route53::HostedZone
Properties:
HostedZoneConfig:
Comment: !Sub 'An internal zone for stack ${AWS::StackName}'
HostedZoneTags:
- Key: Name
Value: !Sub ${AWS::StackName}.internal.
Name: !Sub ${AWS::StackName}.internal.
VPCs:
- VPCId: !GetAtt VPC.Outputs.VpcId
VPCRegion: !Ref AWS::Region
EfsRoute53Record:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref PrivateZone
Name: !Sub efs.${AWS::StackName}.internal.
Type: CNAME
TTL: 60
ResourceRecords:
- !Sub ${efsFileSystem}.efs.${AWS::Region}.amazonaws.com.
PhpRoute53Record:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref PrivateZone
Name: !Sub php.elb.${AWS::StackName}.internal.
Type: A
AliasTarget:
HostedZoneId: !GetAtt AppLoadBalancer.CanonicalHostedZoneNameID
DNSName: !GetAtt AppLoadBalancer.DNSName
secretDirectory:
DependsOn: StackRole
Type: AWS::SecretsManager::Secret
Properties:
Name: !Sub ${AWS::StackName}/SimpleAD/Administrator
Description: !Sub Credentials for Directory Service in the ${AWS::StackName} stack
KmsKeyId: !If
- useEncryption
- !If
- useSeparateCustomKeys
- !GetAtt [ keySM, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
GenerateSecretString:
SecretStringTemplate: '{"username": "Administrator"}'
GenerateStringKey: "password"
PasswordLength: 30
Tags:
- Key: Name
Value: directory/admin
secretDirectoryJoiner:
DependsOn: StackRole
Type: AWS::SecretsManager::Secret
Properties:
Name: !Sub ${AWS::StackName}/SimpleAD/Joiner
Description: !Sub Credentials for Directory Service in the ${AWS::StackName} stack (Joiner)
KmsKeyId: !If
- useEncryption
- !If
- useSeparateCustomKeys
- !GetAtt [ keySM, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
GenerateSecretString:
SecretStringTemplate: '{"username": "Joiner"}'
GenerateStringKey: "password"
ExcludeCharacters: "\"!$'{}|`\\"
PasswordLength: 30
Tags:
- Key: Name
Value: directory/joiner
Directory:
Condition: useAZ2
DependsOn: StackRole
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/simple-ad.template
Parameters:
Name: !Sub id.${AWS::StackName}.internal # trailing dor is not allowed
Description: This directory holds all hosting accounts and active EC2 instances
ShortName: ID
Size: Small
Password: !Sub 'resolve:secretsmanager:${secretDirectory}:SecretString:password'
SubnetIds: !Join
- ','
- - !Select [ 0, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
- !Select [ 1, !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPrivateIds ]]]
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
TimeoutInMinutes: 15
DhcpOptions:
Type: AWS::EC2::DHCPOptions
Properties:
DomainName: !Sub ${AWS::StackName}.internal.
DomainNameServers: !Split [ ',', !GetAtt [ Directory, Outputs.DnsIpAddresses ]]
DhcpOptionsAssociation:
Type: AWS::EC2::VPCDHCPOptionsAssociation
Properties:
DhcpOptionsId: !Ref DhcpOptions
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
S3Endpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal: '*'
Action:
- s3:*
Resource:
- !Sub 'arn:aws:s3:::cloudformation-custom-resource-response-${AWS::Region}/*'
- !Sub 'arn:aws:s3:::cloudformation-waitcondition-${AWS::Region}/*'
- !Sub 'arn:aws:s3:::${ArtifactStore}/*'
RouteTableIds: [ !GetAtt [ VPC, Outputs.RouteTablePrivateId ]]
ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
secretRDS:
Condition: useDatabase
DependsOn: StackRole
Type: "AWS::SecretsManager::Secret"
Properties:
Name: !Sub ${AWS::StackName}/RDS/master
Description: !Sub Credentials for the RDS Service in the ${AWS::StackName} stack
KmsKeyId: !If
- useEncryption
- !If
- useSeparateCustomKeys
- !GetAtt [ keySM, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
GenerateSecretString:
SecretStringTemplate: '{"username": "master"}'
GenerateStringKey: "password"
PasswordLength: 30
ExcludeCharacters: '"@/\'
Tags:
- Key: Name
Value: database
RDS:
Condition: useDatabase
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: library/rds.template
Parameters:
AllocatedStorage: 20 # XXX: need to parametrise
DBInstanceClass: db.t2.medium
DBInstanceIdentifier: !Sub ${AWS::StackName}
Engine: !Ref DatabaseEngine
MasterUsername: !Sub '{{resolve:secretsmanager:${secretRDS}:SecretString:username}}'
MasterUserPassword: !Sub 'resolve:secretsmanager:${secretRDS}:SecretString:password'
MultiAZ: False
SubnetIds: !GetAtt [ VPC, Outputs.SubnetPrivateIds ]
VPCSecurityGroups: !Join [ ',', [ !Ref sgDatabase ]]
KmsKeyId: !If
- useEncryption
- !If
- useSeparateCustomKeys
- !GetAtt [ keyRDS, Outputs.KeyId ]
- !If
- useSingleCustomKey
- !GetAtt [ keyCustom, Outputs.KeyId ]
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
TimeoutInMinutes: 60
secretRDSInstanceAttachment:
Condition: useDatabase
Type: AWS::SecretsManager::SecretTargetAttachment
Properties:
SecretId: !Ref secretRDS
TargetId: !GetAtt [ RDS, Outputs.RdsId ]
TargetType: AWS::RDS::DBInstance
WebLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Type: application
IpAddressType: ipv4
LoadBalancerAttributes:
- Key: access_logs.s3.enabled
Value: False
- Key: deletion_protection.enabled
Value: False
- Key: idle_timeout.timeout_seconds
Value: 60
- Key: routing.http2.enabled
Value: True
Scheme: internet-facing
SecurityGroups:
- !Ref sgWebElb
Subnets: !Split [ ',', !GetAtt [ VPC, Outputs.SubnetPublicIds ]]
Tags:
- Key: Name
Value: web
WebLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref WebTargetGroup
Type: forward
LoadBalancerArn: !Ref WebLoadBalancer
Port: 80
Protocol: HTTP
WebTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Port: 80
Protocol: HTTP
HealthCheckPath: /healthcheck
HealthCheckIntervalSeconds: 6
HealthCheckTimeoutSeconds: 3
HealthyThresholdCount: 3
UnhealthyThresholdCount: 2
TargetGroupAttributes:
- Key: deregistration_delay.timeout_seconds
Value: 120
- Key: slow_start.duration_seconds
Value: 60
VpcId: !GetAtt [ VPC, Outputs.VpcId ]
WebLaunchTemplate:
Condition: useWebImageId
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateData:
InstanceType: t3.micro
# TagSpecifications:
# - TagSpecification
ImageId: !Ref WebImageId
KeyName: !If
- useKeyName
- !Ref KeyName
- !Ref AWS::NoValue
UserData:
Fn::Base64: !Sub |
PHP_DNS=${PhpRoute53Record}
PHP_PORT=9000
BlockDeviceMappings:
- DeviceName: /dev/sdc
Ebs:
DeleteOnTermination: True
VolumeSize: 100
VolumeType: gp2