-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodePipeline ECS POC.yml
1030 lines (944 loc) · 30.7 KB
/
CodePipeline ECS POC.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
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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
# General:
NamingPrefix:
Type: String
Description: The naming prefix for resources created by this template, and exported values that can be referenced by other stacks.
VPCIPv4CidrBlock:
Description: VPC CIDR block for IPv4. Default of 10.0.0.0/16 is recommended for testing.
Type: String
Default: 10.0.0.0/16
VPCSubnetIPv4Size:
Description: Host bit mask length of each subnet, e.g. default of 4 will be a /28 subnet size.
Type: Number
Default: 8
VPCNumberOfIPv4Subnets:
Description: Number of equally sized IPv4 subnets that will be created within the VPC CIDR block.
Type: Number
Default: 256
AvailabilityZone1:
Description: First AZ used by the system.
Type: AWS::EC2::AvailabilityZone::Name
AvailabilityZone2:
Description: Second AZ used by the system.
Type: AWS::EC2::AvailabilityZone::Name
AvailabilityZoneBastion:
Description: AZ used by the bastion.
Type: AWS::EC2::AvailabilityZone::Name
InstanceType:
Description: EC2 instance type
Type: String
Default: t2.micro
AllowedValues:
- t1.micro
- t2.nano
- t2.micro
- t2.small
- t2.medium
ConstraintDescription: Must be a valid EC2 instance type.
WebBucketName:
Description: Name of bucket used for the front-end.
Type: String
AllowedPattern: "(?=^.{3,63}$)(?!^(\\d+\\.)+\\d+$)(^(([a-z0-9]|[a-z0-9][a-z0-9\\-]*[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]*[a-z0-9])$)"
ConstraintDescription: Must follow valid bucket naming conventions.
KeyName:
Description: The EC2 Key Pair to allow SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Must be the name of an existing EC2 KeyPair.
SafeNetwork:
Description: The IP address range that can be used to SSH to bastion hosts.
Type: String
MinLength: 9
MaxLength: 18
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.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "General Configuration"
Parameters:
- NamingPrefix
- VPCIPv4CidrBlock
- VPCSubnetIPv4Size
- VPCNumberOfIPv4Subnets
- AvailabilityZone1
- AvailabilityZone2
- AvailabilityZoneBastion
- InstanceType
- SafeNetwork
- KeyName
- WebBucketName
Mappings:
ECSInstanceRegionMap:
# ECS Optimized AMIs (didn't work for some reason)
us-east-1:
"HVM64": "ami-088beb3aba8c353f1"
us-east-2:
"HVM64": "ami-0888e1d90572c95b6"
us-west-1:
"HVM64": "ami-0f678e27648aa0093"
us-west-2:
"HVM64": "ami-0e735c9fbcca3efed"
InstanceRegionMap:
# Regular AWS Linux 2 AMIs
us-east-1:
"HVM64": "ami-047a51fa27710816e"
us-east-2:
"HVM64": "ami-01aab85a5e4a5a0fe"
us-west-1:
"HVM64": "ami-005c06c6de69aee84"
us-west-2:
"HVM64": "ami-0e999cbd62129e3b1"
Resources:
#
# S3 Bucket for React front-end
#
WebBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
Properties:
BucketName: !Sub '${WebBucketName}'
Tags:
- Key: Name
Value: !Sub '${WebBucketName}'
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
WebBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebBucket
PolicyDocument:
Id: WebBucketPolicy
Version: 2012-10-17
Statement:
- Sid: PublicReadForGetBucketObjects
Effect: Allow
Principal: '*'
Action: 's3:GetObject'
Resource:
- !Sub
- '${BucketArn}/*'
- { BucketArn: !GetAtt WebBucket.Arn }
#
# VPC
#
VPC:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: !Ref VPCIPv4CidrBlock
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-VPC'
#
# Internet Gateway
#
IGW:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-internetgateway.html
Type: 'AWS::EC2::InternetGateway'
Properties:
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-IGW'
IGWAttach:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc-gateway-attachment.html
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref IGW
#VpnGatewayId
#
# Route Stuffs
#
RouteTableInternet:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route-table.html
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Internet'
RouteTablePrivate:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route-table.html
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Private'
RouteInternet:
Type: 'AWS::EC2::Route'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route.html
Properties:
DestinationCidrBlock: 0.0.0.0/0
#DestinationIpv6CidrBlock: ::/0
#EgressOnlyInternetGatewayId
GatewayId: !Ref IGW
#InstanceId
#NatGatewayId
#NetworkInterfaceId
RouteTableId: !Ref RouteTableInternet
#VpcPeeringConnectionId
RoutePrivate:
Type: AWS::EC2::Route
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route.html
Properties:
DestinationCidrBlock: 0.0.0.0/0
#DestinationIpv6CidrBlock: ::/0
#EgressOnlyInternetGatewayId
#GatewayId
#InstanceId
NatGatewayId: !Ref NATGateway
#NetworkInterfaceId
RouteTableId: !Ref RouteTablePrivate
#VpcPeeringConnectionId
RouteTableAssocBastion:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetBastion
- RouteTableInternet
Properties:
RouteTableId: !Ref RouteTableInternet
SubnetId: !Ref SubnetBastion
RouteTableAssocPublic1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetPublic1
- RouteTableInternet
Properties:
RouteTableId: !Ref RouteTableInternet
SubnetId: !Ref SubnetPublic1
RouteTableAssocPublic2:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetPublic2
- RouteTableInternet
Properties:
RouteTableId: !Ref RouteTableInternet
SubnetId: !Ref SubnetPublic2
RouteTableAssocPrivate1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetPrivate1
- RouteTablePrivate
Properties:
RouteTableId: !Ref RouteTablePrivate
SubnetId: !Ref SubnetPrivate1
RouteTableAssocPrivate2:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetPrivate2
- RouteTablePrivate
Properties:
RouteTableId: !Ref RouteTablePrivate
SubnetId: !Ref SubnetPrivate2
RouteTableAssocMongo1:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetMongo1
- RouteTablePrivate
Properties:
RouteTableId: !Ref RouteTablePrivate
SubnetId: !Ref SubnetMongo1
RouteTableAssocMongo2:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html
DependsOn:
- SubnetMongo2
- RouteTablePrivate
Properties:
RouteTableId: !Ref RouteTablePrivate
SubnetId: !Ref SubnetMongo2
#
# Subnets
#
SubnetBastion:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZoneBastion
CidrBlock: !Select [ 0, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Bastion'
SubnetPublic1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: !Select [ 1, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Public-1'
SubnetPublic2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone2
CidrBlock: !Select [ 2, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Public-2'
SubnetPrivate1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: !Select [ 3, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Private-1'
SubnetPrivate2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone2
CidrBlock: !Select [ 4, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Private-2'
SubnetMongo1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone1
CidrBlock: !Select [ 5, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Mongo-1'
SubnetMongo2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Ref AvailabilityZone2
CidrBlock: !Select [ 6, !Cidr [ !GetAtt VPC.CidrBlock, !Ref VPCNumberOfIPv4Subnets, !Ref VPCSubnetIPv4Size ]]
MapPublicIpOnLaunch: False
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Mongo-2'
#
# Network ACLs
#
NetworkAclBastion:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Bastion'
AclAssocationBastion:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetBastion
NetworkAclId: !Ref NetworkAclBastion
AclEntryBastion:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclBastion
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'false'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
AclEgressBastion:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclBastion
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'true'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
NetworkAclPublic:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Public'
AclAssocationPublic1:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetPublic1
NetworkAclId: !Ref NetworkAclPublic
AclAssocationPublic2:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetPublic2
NetworkAclId: !Ref NetworkAclPublic
AclEntryPublic:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclPublic
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'false'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
AclEgressPublic:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclPublic
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'true'
CidrBlock: 0.0.0.0/0
PortRange:
From: '1'
To: '65535'
NetworkAclPrivate:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Private'
AclAssocationPrivate1:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetPrivate1
NetworkAclId: !Ref NetworkAclPrivate
AclAssocationPrivate2:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetPrivate2
NetworkAclId: !Ref NetworkAclPrivate
AclEntryPrivate:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclPrivate
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'false'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
AclEgressPrivate:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclPrivate
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'true'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
NetworkAclMongo:
Type: 'AWS::EC2::NetworkAcl'
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Mongo'
AclAssocationMongo1:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetMongo1
NetworkAclId: !Ref NetworkAclMongo
AclAssocationMongo2:
Type: 'AWS::EC2::SubnetNetworkAclAssociation'
Properties:
SubnetId: !Ref SubnetMongo2
NetworkAclId: !Ref NetworkAclMongo
AclEntryMongo:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclMongo
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'false'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
AclEgressMongo:
Type: 'AWS::EC2::NetworkAclEntry'
Properties:
NetworkAclId: !Ref NetworkAclMongo
RuleNumber: '100'
Protocol: '-1'
RuleAction: allow
Egress: 'true'
CidrBlock: '0.0.0.0/0'
PortRange:
From: '1'
To: '65535'
#
# Security Groups
#
SecurityGroupBastion:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub '${NamingPrefix}-Bastion'
GroupDescription: Allows connections to bastion instances
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Bastion'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref SafeNetwork
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref SafeNetwork
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref SafeNetwork
SecurityGroupElb:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub '${NamingPrefix}-ELB'
GroupDescription: Allows connections to ELB
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-ELB'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref SafeNetwork
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref SafeNetwork
SecurityGroupPublic:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub '${NamingPrefix}-Public'
GroupDescription: Allow connections to EC2 instances in public subnet
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Public'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 1
ToPort: 65535
SourceSecurityGroupId: !Ref SecurityGroupBastion
- IpProtocol: tcp
FromPort: 1
ToPort: 65535
SourceSecurityGroupId: !Ref SecurityGroupElb
- IpProtocol: tcp
FromPort: 1
ToPort: 65535
SourceSecurityGroupId: !Ref SecurityGroupPrivate
SecurityGroupPrivate:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub '${NamingPrefix}-Private'
GroupDescription: Allow connections to EC2 instances in private subnet
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Private'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 1
ToPort: 65535
SourceSecurityGroupId: !Ref SecurityGroupBastion
- IpProtocol: tcp
FromPort: 32768
ToPort: 65535
SourceSecurityGroupId: !Ref SecurityGroupElb
SecurityGroupMongo:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub '${NamingPrefix}-Mongo'
GroupDescription: Allows connections to mongodb instance
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Mongo'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
SourceSecurityGroupId: !Ref SecurityGroupBastion
- IpProtocol: tcp
FromPort: 27017
ToPort: 27017
SourceSecurityGroupId: !Ref SecurityGroupPrivate
#
# NAT Gateway
#
NATGateway:
Type: AWS::EC2::NatGateway
DependsOn: NATGatewayEIP
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-natgateway.html
Properties:
AllocationId: !GetAtt NATGatewayEIP.AllocationId
SubnetId: !Ref SubnetPublic1
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-NATGateway'
NATGatewayEIP:
Type: AWS::EC2::EIP
Properties:
Domain: VPC
#
# Database Instance
#
MongoInstance:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
Type: AWS::EC2::Instance
DependsOn:
- RouteTableInternet
- RouteTablePrivate
- SubnetMongo1
- SubnetMongo2
- SecurityGroupMongo
- NATGateway
Properties:
SubnetId: !Ref SubnetMongo1
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-MongoDB'
KeyName:
!Ref KeyName
DisableApiTermination: false
Monitoring: false
ImageId:
!FindInMap [InstanceRegionMap, !Ref "AWS::Region", HVM64]
SecurityGroupIds:
- !Ref SecurityGroupMongo
InstanceType:
!Ref InstanceType
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
echo -n 'W21vbmdvZGItb3JnLTQuNF0KbmFtZT1Nb25nb0RCIFJlcG9zaXRvcnkKYmFzZXVybD1odHRwczovL3JlcG8ubW9uZ29kYi5vcmcveXVtL2FtYXpvbi8yL21vbmdvZGItb3JnLzQuNC94ODZfNjQvCmdwZ2NoZWNrPTEKZW5hYmxlZD0xCmdwZ2tleT1odHRwczovL3d3dy5tb25nb2RiLm9yZy9zdGF0aWMvcGdwL3NlcnZlci00LjQuYXNjCg==' | base64 -d > /etc/yum.repos.d/mongodb-org.repo
yum install -y htop mongodb-org
# Make MongoDB listen on all interfaces
sed -i 's/bindIp: 127.0.0.1/bindIp: 0.0.0.0/' /etc/mongod.conf
# Enable and start MongoDB
systemctl enable mongod
systemctl start mongod
# Create Mongo collections and insert a few documents
echo 'dXNlIHMzdXBsb2FkCmRiLmNyZWF0ZUNvbGxlY3Rpb24oICdmaWxlVHlwZXMnLCB7fSApCmRiLmNyZWF0ZUNvbGxlY3Rpb24oICdmaWxlcycsIHt9ICkKZGIuY3JlYXRlQ29sbGVjdGlvbiggJ29yaWdpbnMnLCB7fSApCmRiLmZpbGVUeXBlcy5pbnNlcnRPbmUoewogICAgICAgICJ0eXBlIjogImJvb2siLAogICAgICAgICJ0ZXh0IjogIkJvb2tzIiwKICAgICAgICAiaGVscCI6ICJCb29rcyBtdXN0IG1lIGluIFBERiBmb3JtYXQuIE1heCBmaWxlIHNpemUgaXMgMU1CLiIsCiAgICAgICAgImV4dGVuc2lvbnMiOiBbICJwZGYiIF0sCiAgICAgICAgIm1pbWV0eXBlcyI6IFsgImFwcGxpY2F0aW9uL3BkZiIgXSwKICAgICAgICAiYnVja2V0IjoiZm9vLnJ5YW53ZW5kZWwuY29tIiwKICAgICAgICAicGF0aCI6ImJvb2tzIgogICAgfQopCmRiLmZpbGVUeXBlcy5pbnNlcnRPbmUoewogICAgICAgICJ0eXBlIjogImltYWdlIiwKICAgICAgICAidGV4dCI6ICJJbWFnZXMiLAogICAgICAgICJoZWxwIjogIkltYWdlcyBtdXN0IGJlIGluIEpQRywgSlBFRywgUE5HLCBvciBHSUYgZm9ybWF0LiBNYXggZmlsZSBzaXplIGlzIDFNQi4iLAogICAgICAgICJleHRlbnNpb25zIjogWyAianBnIiwgImpwZWciLCAicG5nIiwgImdpZiIgXSwKICAgICAgICAibWltZXR5cGVzIjogWyAiaW1hZ2UvanBlZyIsICJpbWFnZS9wbmciLCAiaW1hZ2UvZ2lmIiBdLAogICAgICAgICJidWNrZXQiOiJiYXIucnlhbndlbmRlbC5jb20iLAogICAgICAgICJwYXRoIjoiZmlsZXMvaW1hZ2VzIgogICAgfQopCg==' | base64 -d | mongo
# Insert an origin document to help with CORS
echo -e 'use s3upload\ndb.origins.insertOne({"origin": "http://${WebBucketName}.s3-website.${AWS::Region}.amazonaws.com"})' | mongo
# Finish up by setting passwords on everything
echo 'dXNlIHMzdXBsb2FkCmRiLmNyZWF0ZVVzZXIoewogICAgICAgIHVzZXI6ICJzM3VwbG9hZCIsCiAgICAgICAgcHdkOiAiUzNVcGxvYWRCQlEiLAogICAgICAgIHJvbGVzOiBbIHsgcm9sZTogInJlYWRXcml0ZSIsIGRiOiAiczN1cGxvYWQiIH0gXQogICAgfQopCnVzZSBhZG1pbgpkYi5jcmVhdGVVc2VyKHsKICAgICAgICB1c2VyOiAicm9vdCIsCiAgICAgICAgcHdkOiAiQmFkUGFzc3dvcmQxMjMhIiwKICAgICAgICByb2xlczogWyB7IHJvbGU6ICJ1c2VyQWRtaW5BbnlEYXRhYmFzZSIsIGRiOiAiYWRtaW4iIH0sICJyZWFkV3JpdGVBbnlEYXRhYmFzZSIgXQogICAgfQopCg==' | base64 -d | mongo
# Enable security for MongoDB
echo -e 'security:\n authorization: enabled' >> /etc/mongod.conf
# Restart MongoDB
systemctl restart mongod
#
# Bastion Instance
#
BastionInstance:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
Type: AWS::EC2::Instance
DependsOn:
- RouteTableInternet
- RouteTablePrivate
- SubnetBastion
- SecurityGroupBastion
- NATGateway
Properties:
SubnetId: !Ref SubnetBastion
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Bastion'
KeyName:
!Ref KeyName
DisableApiTermination: false
Monitoring: false
ImageId:
!FindInMap [InstanceRegionMap, !Ref "AWS::Region", HVM64]
SecurityGroupIds:
- !Ref SecurityGroupBastion
InstanceType:
!Ref InstanceType
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum install -y htop
BastionEIP:
Type: AWS::EC2::EIP
Properties:
InstanceId: !Ref BastionInstance
Domain: VPC
#
# ECS Stuffs
#
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Sub '${NamingPrefix}-Cluster'
Tags:
- Key: Name
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-Cluster'
#
# ECS Task Execution Role
#
ECSTaskExecutionRole:
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
#
# ECS Task Role
#
ECSTaskRole:
# https://docs.aws.amazon.com/AmazonECS/latest/userguide/task-iam-roles.html
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: S3AccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:PutObject'
- 's3:PutObjectAcl'
- 's3:DeleteObject'
Resource:
- 'arn:aws:s3:::foo.ryanwendel.com/books/*'
- 'arn:aws:s3:::bar.ryanwendel.com/files/images/*'
#
# Instance Role
#
InstanceRole:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html
# https://docs.amazonaws.cn/en_us/AmazonECS/latest/developerguide/instance_IAM_role.html
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: ECSServicePolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'ec2:DescribeTags'
- 'ecs:DeregisterContainerInstance'
- 'ecs:DiscoverPollEndpoint'
- 'ecs:Poll'
- 'ecs:RegisterContainerInstance'
- 'ecs:StartTelemetrySession'
- 'ecs:UpdateContainerInstancesState'
- 'ecs:Submit*'
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
#
# Instance Profile
#
InstanceProfile:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-instanceprofile.html
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- !Ref InstanceRole
ECSInstance1:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
Type: AWS::EC2::Instance
DependsOn:
- RouteTableInternet
- RouteTablePrivate
- SubnetPrivate1
- SecurityGroupPrivate
- NATGateway
Properties:
IamInstanceProfile:
!Ref InstanceProfile
SubnetId:
!Ref SubnetPrivate1
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-ECS-1'
KeyName:
!Ref KeyName
DisableApiTermination: false
Monitoring: false
ImageId:
!FindInMap [InstanceRegionMap, !Ref "AWS::Region", HVM64]
SecurityGroupIds:
- !Ref SecurityGroupPrivate
InstanceType:
!Ref InstanceType
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
amazon-linux-extras install -y ecs
echo ECS_CLUSTER='${NamingPrefix}-Cluster' >> /etc/ecs/ecs.config
echo ECS_ENABLE_TASK_IAM_ROLE=true >> /etc/ecs/ecs.config
echo ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true >> /etc/ecs/ecs.config
systemctl enable --now --no-block docker.service
systemctl enable --now --no-block ecs.service
#yum install -y aws-cfn-bootstrap
#/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource --region ${AWS::Region}
ECSInstance2:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
Type: AWS::EC2::Instance
DependsOn:
- RouteTableInternet
- RouteTablePrivate
- SubnetPrivate2
- SecurityGroupPrivate
- NATGateway
Properties:
IamInstanceProfile:
!Ref InstanceProfile
SubnetId:
!Ref SubnetPrivate2
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-ECS-2'
KeyName:
!Ref KeyName
DisableApiTermination: false
Monitoring: false
ImageId:
!FindInMap [InstanceRegionMap, !Ref "AWS::Region", HVM64]
SecurityGroupIds:
- !Ref SecurityGroupPrivate
InstanceType:
!Ref InstanceType
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
amazon-linux-extras install -y ecs
echo ECS_CLUSTER='${NamingPrefix}-Cluster' >> /etc/ecs/ecs.config
echo ECS_ENABLE_TASK_IAM_ROLE=true >> /etc/ecs/ecs.config
echo ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true >> /etc/ecs/ecs.config
systemctl enable --now --no-block docker.service
systemctl enable --now --no-block ecs.service
#yum install -y aws-cfn-bootstrap
#/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource InstanceScaleGroup --region ${AWS::Region}
#
# Load Balancer Stuffs
#
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub '${NamingPrefix}-ELB-1'
Type: application
Subnets:
- !Ref SubnetPublic1
- !Ref SubnetPublic2
Scheme: internet-facing
SecurityGroups:
- !Ref SecurityGroupElb
Tags:
- Key: Name
Value: !Sub '${NamingPrefix}-ELB-1'
LoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref LoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- Type: forward
TargetGroupArn: !Ref TargetGroup
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn: LoadBalancer
Properties:
Name: !Sub '${NamingPrefix}-Target-Group-1'
VpcId: !Ref VPC
Port: 8000