forked from aws-samples/amazon-ecs-mythicalmysfits-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.yml
executable file
·1190 lines (1119 loc) · 38.9 KB
/
core.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'
Description: This stack deploys the core network infrastructure and IAM resources
to be used for a service hosted in Amazon ECS using AWS Fargate.
Parameters:
SkipBucket:
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
# Name for ES Domain
DOMAINNAME:
Description: Enter a name for the Amazon ES domain that this template will create. Domain names must start with a lowercase letter and must be between 3 and 28 characters. Valid characters are a-z (lowercase only), 0-9.
Type: String
Default: mythicalmysfits
# Email address for Cognito Admin user
CognitoAdminEmail:
Type: String
Default: [email protected]
AllowedPattern: '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
Description: Enter a valid email address to be used for Kibana Cognito authentication.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Elasticsearch Configuration
Parameters:
- DOMAINNAME
- Label:
default: Cognito Configuration
Parameters:
- CognitoAdminEmail
- Label:
default: S3 Configuration
Parameters:
- SkipBucket
ParameterLabels:
CognitoAdminEmail:
default: Enter a valid email address to be used for Kibana Cognito authentication. Do NOT leave this default value, it is there only as an example.
DOMAINNAME:
default: Enter a name for an Amazon Elasticsearch domain that this template will create. Domain names must start with a lowercase letter and must be between 3 and 28 characters. Valid characters are a-z (lowercase only), 0-9. You can leave this default value.
SkipBucket:
default: Skip S3 bucket creation
Conditions:
MakeBucket:
Fn::Equals: ["false", !Ref SkipBucket]
Mappings:
# Hard values for the subnet masks. These masks define
# the range of internal IP addresses that can be assigned.
# The VPC can have all IP's from 10.0.0.0 to 10.0.255.255
# There are four subnets which cover the ranges:
#
# 10.0.0.0 - 10.0.0.255
# 10.0.1.0 - 10.0.1.255
# 10.0.2.0 - 10.0.2.255
# 10.0.3.0 - 10.0.3.255
#
# If you need more IP addresses (perhaps you have so many
# instances that you run out) then you can customize these
# ranges to add more
SubnetConfig:
VPC:
CIDR: '10.0.0.0/16'
PublicOne:
CIDR: '10.0.0.0/24'
PublicTwo:
CIDR: '10.0.1.0/24'
PrivateOne:
CIDR: '10.0.2.0/24'
PrivateTwo:
CIDR: '10.0.3.0/24'
Resources:
MythicalBucket:
Type: AWS::S3::Bucket
Condition: MakeBucket
Properties:
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
MythicalMonolithGitRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryDescription: Repository for the Mythical Mysfits monolith service
RepositoryName: !Sub ${AWS::StackName}-monolith-service
MythicalLikeGitRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryDescription: Repository for the Mythical Mysfits like service
RepositoryName: !Sub ${AWS::StackName}-like-service
Mono:
Type: AWS::ECR::Repository
Like:
Type: AWS::ECR::Repository
Xray:
Type: AWS::ECR::Repository
MythicalEcsCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Sub Cluster-${AWS::StackName}
MythicalMonolithLogGroup:
Type: AWS::Logs::LogGroup
MythicalLikeLogGroup:
Type: AWS::Logs::LogGroup
MythicalMonolithTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Cpu: 256
ExecutionRoleArn: !GetAtt EcsServiceRole.Arn
Family: !Sub Mythical-Mysfits-Monolith-Service-${AWS::StackName}
Memory: 512
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !GetAtt ECSTaskRole.Arn
ContainerDefinitions:
- Name: monolith-service
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Mono}:latest
PortMappings:
- ContainerPort: 80
Protocol: http
Environment:
- Name: UPSTREAM_URL
Value: !GetAtt MythicalLoadBalancer.DNSName
- Name: DDB_TABLE_NAME
Value: !Ref MythicalDynamoTable
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref MythicalMonolithLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: awslogs-mythicalmysfits-service
Essential: true
MythicalLikeTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Cpu: 256
ExecutionRoleArn: !GetAtt EcsServiceRole.Arn
Family: !Sub Mythical-Mysfits-Like-Service-${AWS::StackName}
Memory: 512
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !GetAtt ECSTaskRole.Arn
ContainerDefinitions:
- Name: like-service
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Like}:latest
PortMappings:
- ContainerPort: 80
Protocol: http
Environment:
- Name: MONOLITH_URL
Value: !GetAtt MythicalLoadBalancer.DNSName
- Name: CHAOSMODE
Value: 'on'
- Name: LOGLEVEL
Value: 'ERROR'
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Ref MythicalLikeLogGroup
awslogs-region: !Ref AWS::Region
awslogs-stream-prefix: awslogs-mythicalmysfits-service
Essential: true
MythicalLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub SecurityGroup-${AWS::StackName}
GroupDescription: Access to the load balancer
VpcId: !Ref 'VPC'
SecurityGroupIngress:
# Allow access to ALB from anywhere on the internet
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
MythicalLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Sub alb-${AWS::StackName}
Scheme: internet-facing
Type: application
SecurityGroups:
- !Ref MythicalLoadBalancerSecurityGroup
Subnets:
- !Ref PublicSubnetOne
- !Ref PublicSubnetTwo
MythicalListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref MythicalMonolithTargetGroup
Type: forward
LoadBalancerArn: !Ref MythicalLoadBalancer
Port: 80
Protocol: HTTP
MythicalMonolithTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthyThresholdCount: 3
UnhealthyThresholdCount: 3
Port: 80
Protocol: HTTP
VpcId: !Ref VPC
TargetType: ip
MythicalLikeTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthyThresholdCount: 3
UnhealthyThresholdCount: 3
Port: 80
Protocol: HTTP
VpcId: !Ref VPC
TargetType: ip
MythicalLikeListenerRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref MythicalLikeTargetGroup
Conditions:
- Field: path-pattern
Values:
- "/mysfits/*/like"
ListenerArn: !Ref MythicalListener
Priority: 1
# VPC in which containers will be networked.
# It has two public subnets, and two private subnets.
# We distribute the subnets across the first two available subnets
# for the region, for high availability.
VPC:
Type: AWS::EC2::VPC
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
CidrBlock: !FindInMap ['SubnetConfig', 'VPC', 'CIDR']
Tags:
- Key: Name
Value: !Sub Mysfits-VPC-${AWS::StackName}
# Two public subnets, where a public load balancer will later be created.
PublicSubnetOne:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR']
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub MysfitsPublicOne-${AWS::StackName}
PublicSubnetTwo:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PublicTwo', 'CIDR']
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Sub MysfitsPublicTwo-${AWS::StackName}
# Two private subnets where containers will only have private
# IP addresses, and will only be reachable by other members of the
# VPC
PrivateSubnetOne:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PrivateOne', 'CIDR']
Tags:
- Key: Name
Value: !Sub MysfitsPrivateOne-${AWS::StackName}
PrivateSubnetTwo:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PrivateTwo', 'CIDR']
Tags:
- Key: Name
Value: !Sub MysfitsPrivateTwo-${AWS::StackName}
# Setup networking resources for the public subnets.
InternetGateway:
Type: AWS::EC2::InternetGateway
GatewayAttachement:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref 'VPC'
InternetGatewayId: !Ref 'InternetGateway'
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
PublicRoute:
Type: AWS::EC2::Route
DependsOn: GatewayAttachement
Properties:
RouteTableId: !Ref 'PublicRouteTable'
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref 'InternetGateway'
PublicSubnetOneRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetOne
RouteTableId: !Ref PublicRouteTable
PublicSubnetTwoRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetTwo
RouteTableId: !Ref PublicRouteTable
# Setup networking resources for the private subnets. Containers
# in these subnets have only private IP addresses, and must use a NAT
# gateway to talk to the internet. We launch two NAT gateways, one for
# each private subnet.
NatGatewayOneAttachment:
Type: AWS::EC2::EIP
DependsOn: GatewayAttachement
Properties:
Domain: vpc
NatGatewayTwoAttachment:
Type: AWS::EC2::EIP
DependsOn: GatewayAttachement
Properties:
Domain: vpc
NatGatewayOne:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatGatewayOneAttachment.AllocationId
SubnetId: !Ref PublicSubnetOne
NatGatewayTwo:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatGatewayTwoAttachment.AllocationId
SubnetId: !Ref PublicSubnetTwo
PrivateRouteTableOne:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
PrivateRouteOne:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTableOne
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGatewayOne
PrivateRouteTableOneAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTableOne
SubnetId: !Ref PrivateSubnetOne
PrivateRouteTableTwo:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
PrivateRouteTwo:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTableTwo
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGatewayTwo
PrivateRouteTableTwoAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTableTwo
SubnetId: !Ref PrivateSubnetTwo
# VPC Endpoint for DynamoDB
# If a container needs to access DynamoDB (coming in module 3) this
# allows a container in the private subnet to talk to DynamoDB directly
# without needing to go via the NAT gateway.
DynamoDBEndpoint:
Type: AWS::EC2::VPCEndpoint
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "*"
Principal: "*"
Resource: "*"
RouteTableIds:
- !Ref 'PrivateRouteTableOne'
- !Ref 'PrivateRouteTableTwo'
ServiceName: !Join [ "", [ "com.amazonaws.", { "Ref": "AWS::Region" }, ".dynamodb" ] ]
VpcId: !Ref 'VPC'
# The security group for our service containers to be hosted in Fargate.
# Even though traffic from users will pass through a Network Load Balancer,
# that traffic is purely TCP passthrough, without security group inspection.
# Therefore, we will allow for traffic from the Internet to be accepted by our
# containers. But, because the containers will only have Private IP addresses,
# the only traffic that will reach the containers is traffic that is routed
# to them by the public load balancer on the specific ports that we configure.
FargateContainerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Access to the fargate containers from the Internet
VpcId: !Ref 'VPC'
SecurityGroupIngress:
# Allow access to NLB from anywhere on the internet
- CidrIp: !FindInMap ['SubnetConfig', 'VPC', 'CIDR']
IpProtocol: -1
# This is an IAM role which authorizes ECS to manage resources on your
# account on your behalf, such as updating your load balancer with the
# details of where your containers are, so that traffic can reach your
# containers.
EcsServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs.amazonaws.com
- ecs-tasks.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: ecs-service
PolicyDocument:
Statement:
- Effect: Allow
Action:
# Rules which allow ECS to attach network interfaces to instances
# on your behalf in order for awsvpc networking mode to work right
- 'ec2:AttachNetworkInterface'
- 'ec2:CreateNetworkInterface'
- 'ec2:CreateNetworkInterfacePermission'
- 'ec2:DeleteNetworkInterface'
- 'ec2:DeleteNetworkInterfacePermission'
- 'ec2:Describe*'
- 'ec2:DetachNetworkInterface'
# Rules which allow ECS to update load balancers on your behalf
# with the information sabout how to send traffic to your containers
- 'elasticloadbalancing:DeregisterInstancesFromLoadBalancer'
- 'elasticloadbalancing:DeregisterTargets'
- 'elasticloadbalancing:Describe*'
- 'elasticloadbalancing:RegisterInstancesWithLoadBalancer'
- 'elasticloadbalancing:RegisterTargets'
# Rules which allow ECS to run tasks that have IAM roles assigned to them.
- 'iam:PassRole'
# Rules that let ECS interact with container images.
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
# Rules that let ECS create and push logs to CloudWatch.
- 'logs:DescribeLogStreams'
- 'logs:CreateLogStream'
- 'logs:CreateLogGroup'
- 'logs:PutLogEvents'
Resource: '*'
# This is a role which is used by the ECS tasks. Tasks in Amazon ECS define
# the containers that should be deployed togehter and the resources they
# require from a compute/memory perspective. So, the policies below will define
# the IAM permissions that our Mythical Mysfits docker containers will have.
# If you attempted to write any code for the Mythical Mysfits service that
# interacted with different AWS service APIs, these roles would need to include
# those as allowed actions.
ECSTaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ecs-tasks.amazonaws.com
# Also add EC2 for testing in Cloud9
- ec2.amazonaws.com
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: AmazonECSTaskRolePolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
# Allow the ECS Tasks to download images from ECR
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
# Allow the ECS tasks to upload logs to CloudWatch
- 'logs:CreateLogStream'
- 'logs:CreateLogGroup'
- 'logs:PutLogEvents'
Resource: '*'
- Effect: Allow
Action:
# Allows the ECS tasks to interact with only the MysfitsTable
# in DynamoDB
- 'dynamodb:Scan'
- 'dynamodb:Query'
- 'dynamodb:UpdateItem'
- 'dynamodb:GetItem'
Resource: !GetAtt MythicalDynamoTable.Arn
- Effect: Allow
Action:
# Allows the X-Ray sidecar to send telemetry data to X-Ray API
- 'xray:PutTraceSegments'
- 'xray:PutTelemetryRecords'
- 'xray:GetSamplingRules'
- 'xray:GetSamplingTargets'
- 'xray:GetSamplingStatisticSummaries'
Resource: '*'
MythicalProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref ECSTaskRole
MythicalDynamoTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub Table-${AWS::StackName}
AttributeDefinitions:
- AttributeName: MysfitId
AttributeType: S
- AttributeName: GoodEvil
AttributeType: S
- AttributeName: LawChaos
AttributeType: S
GlobalSecondaryIndexes:
- IndexName: LawChaosIndex
KeySchema:
- AttributeName: LawChaos
KeyType: HASH
- AttributeName: MysfitId
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
- IndexName: GoodEvilIndex
KeySchema:
- AttributeName: GoodEvil
KeyType: HASH
- AttributeName: MysfitId
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
KeySchema:
- AttributeName: MysfitId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
MythicalEnvironment:
Type: AWS::Cloud9::EnvironmentEC2
Properties:
AutomaticStopTimeMinutes: 30
InstanceType: t2.small
Name: !Sub Project-${AWS::StackName}
SubnetId: !Ref PublicSubnetOne
# Begin WS2 Bootstrapping
# This role is used by CodePipeline to trigger deployments
CodePipelineServiceRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: [codepipeline.amazonaws.com]
Action: ['sts:AssumeRole']
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
- s3:GetBucketVersioning
- Resource: "arn:aws:s3:::*"
Effect: Allow
Action:
- s3:PutObject
- Resource: "*"
Effect: Allow
Action:
- codecommit:*
- codebuild:StartBuild
- codebuild:BatchGetBuilds
- iam:PassRole
- iam:CreateRole
- iam:DetachRolePolicy
- iam:AttachRolePolicy
- iam:PassRole
- iam:PutRolePolicy
- cloudwatch:*
- Resource: "*"
Effect: Allow
Action:
- ecs:*
# CodeBuild requires access to push images to repos and this is the role that provides that access
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPowerUser
RoleName: !Sub ${AWS::StackName}-CodeBuildServiceRole
Path: /
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "codebuild.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
Policies:
- PolicyName: root
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- ecr:GetAuthorizationToken
- Resource: !Sub arn:aws:s3:::${MythicalArtifactBucket}/*
Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:GetObjectVersion
- Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/${MythicalLikeLogGroup}/codebuild/tests/
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/${MythicalLikeLogGroup}/codebuild/tests:*
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvent
- Resource:
- !Sub arn:aws:s3:::codepipeline-${AWS::Region}-*
Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
# CodeBuild needs a place to put artifacts in the interim. This bucket is where it goes
MythicalArtifactBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Delete
# Actual CodeBuild project that builds the Docker images and outputs imagedefinitions.json
DockerBuildCodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: "CODEPIPELINE"
Source:
Type: "CODEPIPELINE"
BuildSpec: "buildspec_prod.yml"
Environment:
PrivilegedMode: true
ComputeType: "BUILD_GENERAL1_SMALL"
Image: "aws/codebuild/docker:17.09.0"
Type: "LINUX_CONTAINER"
EnvironmentVariables:
- Name: AWS_ACCOUNT_ID
Value: !Ref AWS::AccountId
Type: PLAINTEXT
Name: !Sub ${AWS::StackName}-like-service-build
ServiceRole: !Ref CodeBuildServiceRole
# CodePipeline for deployments. Uses CodeCommit + CodeBuild + ECS, deploying
# to an existing ECS service. It looks for imagedefinitions.json as well as
# buildspec_prod for this
MythicalLikeServicePipeline:
Type: 'AWS::CodePipeline::Pipeline'
Properties:
ArtifactStore:
Type: S3
Location: !Ref MythicalArtifactBucket
Name: !Sub ${AWS::StackName}
RoleArn: !GetAtt CodePipelineServiceRole.Arn
Stages:
- Name: 'Source'
Actions:
- Name: 'Source'
ActionTypeId:
Category: 'Source'
Owner: 'AWS'
Version: '1'
Provider: 'CodeCommit'
OutputArtifacts:
- Name: SourceArtifact
Configuration:
PollForSourceChanges: 'false'
BranchName: master
RepositoryName: !GetAtt MythicalLikeGitRepository.Name
RunOrder: 1
- Name: 'Build_Docker_Container'
Actions:
- Name: CodeBuild
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
InputArtifacts:
- Name: SourceArtifact
OutputArtifacts:
- Name: BuildArtifact
Configuration:
ProjectName: !Ref DockerBuildCodeBuildProject
RunOrder: 1
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: ECS
InputArtifacts:
- Name: BuildArtifact
Configuration:
ClusterName: !Ref MythicalEcsCluster
ServiceName: !Sub ${AWS::StackName}_Mythical-Like-Service
FileName: imagedefinitions.json
RunOrder: 1
# CodePipeline does not automatically create this for you as part of
# creation, but this role is for the CWE Hook to trigger CodePipeline once
# there's an update in CodeCommit. Otherwise, we have to have CodePipeline
# poll for changes, which is slower.
CodeCommitCloudWatchEventRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
-
PolicyName: cwe-pipeline-execution
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action: codepipeline:StartPipelineExecution
Resource: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${MythicalLikeServicePipeline}
# Actual CWE Rule to push changes over to CodePipeline.
CodeCommitCloudWatchEventRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Repository State Change'
resources:
- !GetAtt MythicalLikeGitRepository.Arn
detail:
event:
- referenceCreated
- referenceUpdated
referenceType:
- branch
referenceName:
- master
Targets:
-
Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${MythicalLikeServicePipeline}
RoleArn: !GetAtt CodeCommitCloudWatchEventRole.Arn
Id: codepipeline-LikePipeline
#
# Cognito and IAM
#
# Creates a user pool in cognito to auth against
UserPool:
Type: 'AWS::Cognito::UserPool'
Properties:
UserPoolName: !Sub ${DOMAINNAME}_kibana_access
AutoVerifiedAttributes:
- email
MfaConfiguration: 'OFF'
EmailVerificationSubject: !Ref AWS::StackName
Schema:
- Name: name
AttributeDataType: String
Mutable: true
Required: true
- Name: email
AttributeDataType: String
Mutable: false
Required: true
# Creates a needed group in Cognito for Kibana access
UserPoolGroup:
Type: "AWS::Cognito::UserPoolGroup"
Properties:
Description: 'User pool group for Kibana access'
GroupName: !Sub ${DOMAINNAME}_kibana_access_group
Precedence: 0
UserPoolId: !Ref UserPool
# Creates a User Pool Client to be used by the identity pool
UserPoolClient:
Type: 'AWS::Cognito::UserPoolClient'
Properties:
ClientName: !Sub ${DOMAINNAME}-client
GenerateSecret: false
UserPoolId: !Ref UserPool
# Creates a federated Identity pool
IdentityPool:
Type: 'AWS::Cognito::IdentityPool'
Properties:
IdentityPoolName: !Sub ${DOMAINNAME}Identity
AllowUnauthenticatedIdentities: true
CognitoIdentityProviders:
- ClientId: !Ref UserPoolClient
ProviderName: !GetAtt UserPool.ProviderName
# Create a role for unauthorized access to AWS resources. Very limited access.
# Only allows users in the previously created Identity Pool
CognitoUnAuthorizedRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Federated: 'cognito-identity.amazonaws.com'
Action:
- 'sts:AssumeRoleWithWebIdentity'
Condition:
StringEquals:
'cognito-identity.amazonaws.com:aud': !Ref IdentityPool
'ForAnyValue:StringLike':
'cognito-identity.amazonaws.com:amr': unauthenticated
Policies:
- PolicyName: 'CognitoUnauthorizedPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'mobileanalytics:PutEvents'
- 'cognito-sync:BulkPublish'
- 'cognito-sync:DescribeIdentityPoolUsage'
- 'cognito-sync:GetBulkPublishDetails'
- 'cognito-sync:GetCognitoEvents'
- 'cognito-sync:GetIdentityPoolConfiguration'
- 'cognito-sync:ListIdentityPoolUsage'
- 'cognito-sync:SetCognitoEvents'
- 'congito-sync:SetIdentityPoolConfiguration'
Resource: !Sub 'arn:aws:cognito-identity:${AWS::Region}:${AWS::AccountId}:identitypool/${IdentityPool}'
# Create a role for authorized access to AWS resources.
# Only allows users in the previously created Identity Pool
CognitoAuthorizedRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Federated: 'cognito-identity.amazonaws.com'
Action:
- 'sts:AssumeRoleWithWebIdentity'
Condition:
StringEquals:
'cognito-identity.amazonaws.com:aud': !Ref IdentityPool
'ForAnyValue:StringLike':
'cognito-identity.amazonaws.com:amr': authenticated
Policies:
- PolicyName: 'CognitoAuthorizedPolicy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'mobileanalytics:PutEvents'
- 'cognito-sync:BulkPublish'
- 'cognito-sync:DescribeIdentityPoolUsage'
- 'cognito-sync:GetBulkPublishDetails'
- 'cognito-sync:GetCognitoEvents'
- 'cognito-sync:GetIdentityPoolConfiguration'
- 'cognito-sync:ListIdentityPoolUsage'
- 'cognito-sync:SetCognitoEvents'
- 'congito-sync:SetIdentityPoolConfiguration'
- 'cognito-identity:DeleteIdentityPool'
- 'cognito-identity:DescribeIdentityPool'
- 'cognito-identity:GetIdentityPoolRoles'
- 'cognito-identity:GetOpenIdTokenForDeveloperIdentity'
- 'cognito-identity:ListIdentities'
- 'cognito-identity:LookupDeveloperIdentity'
- 'cognito-identity:MergeDeveloperIdentities'
- 'cognito-identity:UnlikeDeveloperIdentity'
- 'cognito-identity:UpdateIdentityPool'
Resource: !Sub 'arn:aws:cognito-identity:${AWS::Region}:${AWS::AccountId}:identitypool/${IdentityPool}'
CognitoESAccessRole:
Type: 'AWS::IAM::Role'
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonESCognitoAccess
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Principal:
Service: 'es.amazonaws.com'
Action:
- 'sts:AssumeRole'