-
Notifications
You must be signed in to change notification settings - Fork 43
/
metaflow-cfn-template.yml
1896 lines (1888 loc) · 67.1 KB
/
metaflow-cfn-template.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: Stack for complete deployment of Metaflow (last-updated-date 10/26/2021)
Parameters:
SagemakerInstance:
Type: String
Default: ml.t2.xlarge
AllowedValues: ['ml.t2.large','ml.t2.xlarge','ml.t2.2xlarge']
Description: 'Instance type for Sagemaker Notebook.'
VPCCidr:
Type: String
Default: 10.20.0.0/16
Description: 'CIDR for the Metaflow VPC'
Subnet1Cidr:
Type: String
Default: 10.20.0.0/24
Description: 'CIDR for Metaflow VPC Subnet 1'
Subnet2Cidr:
Type: String
Default: 10.20.1.0/24
Description: 'CIDR for Metaflow VPC Subnet 2'
MaxVCPUBatch:
Type: Number
Default: 64
MinValue: 16
MaxValue: 256
Description: 'Maximum VCPUs for Batch Compute Environment [16-256]. You can change the upper limit by editing the Cloudformation template.'
MinVCPUBatch:
Type: Number
Default: 8
MinValue: 0
MaxValue: 16
AllowedValues: [0,2,4,8,16]
Description: 'Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate)'
DesiredVCPUBatch:
Type: Number
Default: 8
MinValue: 0
MaxValue: 16
AllowedValues: [0,2,4,8,16]
Description: 'Desired Starting VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate)'
ComputeEnvInstanceTypes:
Type: CommaDelimitedList
Default: "c4.large,c4.xlarge,c4.2xlarge,c4.4xlarge,c4.8xlarge"
Description: "The instance types for the compute environment as a comma-separated list"
CustomRole:
Type: String
Default: 'false'
AllowedValues: ['false', 'true']
Description: 'Enable custom role with restricted permissions?'
APIBasicAuth:
Type: String
Default: 'true'
AllowedValues: ['false', 'true']
Description: 'Enable basic auth for API Gateway? (requires you to export the API key from API Gateway console)'
EnableSagemaker:
Type: String
Default: 'true'
AllowedValues: ['false', 'true']
Description: 'Enable Sagemaker Notebooks'
BatchType:
Type: String
Default: 'ec2'
AllowedValues: ['ec2', 'fargate']
Description: 'AWS Batch Compute Type'
IAMPartition:
Type: String
Default: 'aws'
AllowedValues: ['aws', 'aws-us-gov']
Description: 'IAM Partition (Select aws-us-gov for AWS GovCloud, otherwise leave as is)'
AdditionalWorkerPolicyArn:
Type: String
Default: ''
Description: 'Additional IAM Policy ARN to attach to Batch Compute Environment (leave empty, unless you know what you are doing)'
EnableUI:
Type: String
Default: 'false'
AllowedValues: ['false', 'true']
Description: 'Enable Metaflow UI. Make sure to specify PublicDomainName and CertificateArn if you do.'
PublicDomainName:
Type: 'String'
Default: ''
Description: 'The custom domain name for UI (e.g., ui.outerbounds.co). Has to match the certificate. Required if UI is enabled.'
CertificateArn:
Default: ''
Type: 'String'
Description: 'The ARN of a ACM certificate valid for the custom domain name. Required if UI is enabled.'
Mappings:
ServiceInfo:
StackName:
value: 'metaflow-infrastructure'
ServiceName:
value: 'metadata-service-v2'
ImageUrl:
value: 'netflixoss/metaflow_metadata_service:v2.4.12'
ContainerPort:
value: 8080
ContainerCpu:
value: 512
ContainerMemory:
value: 1024
Path:
value: '*'
Priority:
value: 1
DesiredCount:
value: 1
Role:
value: ""
ServiceInfoUIService:
StackName:
value: 'metaflow-infrastructure'
ServiceName:
value: 'metaflow-ui-service'
ImageUrl:
value: 'netflixoss/metaflow_metadata_service:v2.4.12'
ContainerPort:
value: 8083
ContainerCpu:
value: 4096
ContainerMemory:
value: 16384
Path:
value: '*'
Priority:
value: 1
DesiredCount:
value: 1
Role:
value: ""
ServiceInfoUIStatic:
StackName:
value: 'metaflow-infrastructure'
ServiceName:
value: 'metadata-ui-static'
ImageUrl:
value: 'public.ecr.aws/outerbounds/metaflow_ui:v1.2.4'
ContainerPort:
value: 3000
ContainerCpu:
value: 512
ContainerMemory:
value: 1024
Path:
value: '*'
Priority:
value: 1
DesiredCount:
value: 1
Role:
value: ""
Conditions:
EnableAuth: !Equals [ !Ref APIBasicAuth, 'true']
EnableRole: !Equals [ !Ref CustomRole, 'true']
EnableFargateOnBatch: !Equals [ !Ref BatchType, 'fargate']
EnableSagemaker: !Equals [ !Ref EnableSagemaker, 'true']
IsGov: !Equals [ !Ref IAMPartition, 'aws-us-gov']
EnableAddtionalWorkerPolicy: !Not [ !Equals [ !Ref AdditionalWorkerPolicyArn, ''] ]
EnableUI: !Equals [ !Ref EnableUI, 'true']
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
CidrBlock: !Ref VPCCidr
Subnet1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !Ref Subnet1Cidr
MapPublicIpOnLaunch: true
Subnet2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !Ref Subnet2Cidr
MapPublicIpOnLaunch: true
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'
DefaultGateway:
Type: AWS::EC2::Route
DependsOn: GatewayAttachement
Properties:
RouteTableId: !Ref 'PublicRouteTable'
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref 'InternetGateway'
Subnet1RTA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref Subnet1
RouteTableId: !Ref PublicRouteTable
Subnet2RTA:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref Subnet2
RouteTableId: !Ref PublicRouteTable
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterSettings:
- Name: containerInsights
Value: enabled
FargateSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security Group for Fargate
VpcId: !Ref 'VPC'
NLBIngressRule:
Type: 'AWS::EC2::SecurityGroupIngress'
Properties:
Description: 'Allow API Calls Internally'
GroupId: !Ref FargateSecurityGroup
IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: !GetAtt 'VPC.CidrBlock'
NLBIngressRuleDBMigrate:
Type: 'AWS::EC2::SecurityGroupIngress'
Properties:
Description: 'Allow API Calls Internally'
GroupId: !Ref FargateSecurityGroup
IpProtocol: tcp
FromPort: 8082
ToPort: 8082
CidrIp: !GetAtt 'VPC.CidrBlock'
FargateInternalRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Internal Communication
GroupId: !Ref 'FargateSecurityGroup'
IpProtocol: -1
SourceSecurityGroupId: !Ref 'FargateSecurityGroup'
FargateAlbRule:
Type: AWS::EC2::SecurityGroupIngress
Condition: EnableUI
Properties:
Description: Internal Communication
GroupId: !Ref 'FargateSecurityGroup'
IpProtocol: -1
SourceSecurityGroupId: !Ref 'LoadBalancerSecurityGroupUI'
NLB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internal
Type: network
Subnets:
- !Ref Subnet1
- !Ref Subnet2
NLBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn:
- NLB
Properties:
DefaultActions:
- TargetGroupArn: !Ref 'NLBTargetGroup'
Type: 'forward'
LoadBalancerArn: !Ref 'NLB'
Port: 80
Protocol: TCP
NLBListenerDBMigrate:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn:
- NLB
Properties:
DefaultActions:
- TargetGroupArn: !Ref 'NLBTargetGroupDBMigrate'
Type: 'forward'
LoadBalancerArn: !Ref 'NLB'
Port: 8082
Protocol: TCP
ECSRole:
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:
- 'ec2:AttachNetworkInterface'
- 'ec2:CreateNetworkInterface'
- 'ec2:CreateNetworkInterfacePermission'
- 'ec2:DeleteNetworkInterface'
- 'ec2:DeleteNetworkInterfacePermission'
- 'ec2:Describe*'
- 'ec2:DetachNetworkInterface'
- 'elasticloadbalancing:DeregisterInstancesFromLoadBalancer'
- 'elasticloadbalancing:DeregisterTargets'
- 'elasticloadbalancing:Describe*'
- 'elasticloadbalancing:RegisterInstancesWithLoadBalancer'
- 'elasticloadbalancing:RegisterTargets'
Resource: '*'
ECSTaskExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [ecs-tasks.amazonaws.com]
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: AmazonECSTaskExecutionRolePolicy
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'ecr:GetAuthorizationToken'
- 'ecr:BatchCheckLayerAvailability'
- 'ecr:GetDownloadUrlForLayer'
- 'ecr:BatchGetImage'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
MetadataSvcECSTaskRole:
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: CustomS3Batch
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ObjectAccessMetadataService
Effect: Allow
Action:
- s3:GetObject
Resource: !Join ['', [ !GetAtt 'MetaflowS3Bucket.Arn', '/*' ]]
- Sid: ObjectAccessMetadataServiceNonExistentKeys
Effect: Allow
Action:
- s3:ListBucket
Resource: !GetAtt 'MetaflowS3Bucket.Arn'
- PolicyName: DenyPresignedBatch
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: DenyPresignedBatch
Effect: Deny
Action: s3:*
Resource: '*'
Condition:
StringNotEquals:
s3:authType: REST-HEADER
ECSInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ECSInstanceRole
ECSInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- 'ec2.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- !Sub arn:${IAMPartition}:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
TaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: !FindInMap ['ServiceInfo', 'ServiceName', 'value']
Cpu: !FindInMap ['ServiceInfo', 'ContainerCpu', 'value']
Memory: !FindInMap ['ServiceInfo', 'ContainerMemory', 'value']
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn: !Ref 'ECSTaskExecutionRole'
TaskRoleArn: !GetAtt 'MetadataSvcECSTaskRole.Arn'
ContainerDefinitions:
- Name: !FindInMap ['ServiceInfo', 'ServiceName', 'value']
Environment:
- Name: "MF_METADATA_DB_HOST"
Value: !GetAtt 'RDSMasterInstance.Endpoint.Address'
- Name: "MF_METADATA_DB_PORT"
Value: "5432"
- Name: "MF_METADATA_DB_SSL_MODE"
Value: "prefer"
- Name: "MF_METADATA_DB_USER"
Value: "master"
- Name: "MF_METADATA_DB_PSWD"
Value: !Join ['', ['{{resolve:secretsmanager:', !Ref MyRDSSecret, ':SecretString:password}}' ]]
- Name: "MF_METADATA_DB_NAME"
Value: "metaflow"
Cpu: !FindInMap ['ServiceInfo', 'ContainerCpu', 'value']
Memory: !FindInMap ['ServiceInfo', 'ContainerMemory', 'value']
Image: !FindInMap ['ServiceInfo', 'ImageUrl', 'value']
PortMappings:
- ContainerPort: !FindInMap ['ServiceInfo', 'ContainerPort', 'value']
- ContainerPort: 8082
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: !Join ['', ['/ecs/', !Ref 'AWS::StackName', '-', !FindInMap ['ServiceInfo', 'ServiceName', 'value']]]
awslogs-region: !Ref 'AWS::Region'
awslogs-stream-prefix: 'ecs'
MetadataServiceLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ['', ['/ecs/', !Ref 'AWS::StackName', '-', !FindInMap ['ServiceInfo', 'ServiceName', 'value']]]
ECSFargateService:
Type: AWS::ECS::Service
Properties:
ServiceName: !FindInMap ['ServiceInfo', 'ServiceName', 'value']
Cluster: !Ref 'ECSCluster'
LaunchType: FARGATE
DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 75
DesiredCount: !FindInMap ['ServiceInfo', 'DesiredCount', 'value']
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
SecurityGroups:
- !Ref 'FargateSecurityGroup'
Subnets:
- !Ref 'Subnet1'
- !Ref 'Subnet2'
TaskDefinition: !Ref 'TaskDefinition'
LoadBalancers:
- ContainerName: !FindInMap ['ServiceInfo', 'ServiceName', 'value']
ContainerPort: !FindInMap ['ServiceInfo', 'ContainerPort', 'value']
TargetGroupArn: !Ref 'NLBTargetGroup'
- ContainerName: !FindInMap ['ServiceInfo', 'ServiceName', 'value']
ContainerPort: 8082
TargetGroupArn: !Ref 'NLBTargetGroupDBMigrate'
LambdaECSExecuteRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: Cloudwatch
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: CreateLogGroup
Effect: Allow
Action: logs:CreateLogGroup
Resource: !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':*' ] ]
- Sid: LogEvents
Effect: Allow
Action:
- logs:PutLogEvents
- logs:CreateLogStream
Resource: !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/lambda/', !Ref 'AWS::StackName', '-migrate-db:*'] ]
- PolicyName: VPC
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: NetInts
Effect: Allow
Action:
- ec2:CreateNetworkInterface
- ec2:DescribeNetworkInterfaces
- ec2:DeleteNetworkInterface
Resource: '*'
ExecuteDBMigration:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.10
Role: !GetAtt LambdaECSExecuteRole.Arn
Handler: index.handler
Environment:
Variables:
MD_LB_ADDRESS: !Join ['', ['http://', !GetAtt 'NLB.DNSName', ':8082'] ]
Code:
ZipFile: |
import os, json
from urllib import request
def handler(event, context):
response = {}
status_endpoint = "{}/db_schema_status".format(os.environ.get('MD_LB_ADDRESS'))
upgrade_endpoint = "{}/upgrade".format(os.environ.get('MD_LB_ADDRESS'))
with request.urlopen(status_endpoint) as status:
response['init-status'] = json.loads(status.read())
upgrade_patch = request.Request(upgrade_endpoint, method='PATCH')
with request.urlopen(upgrade_patch) as upgrade:
response['upgrade-result'] = upgrade.read().decode()
with request.urlopen(status_endpoint) as status:
response['final-status'] = json.loads(status.read())
print(response)
return(response)
Description: Trigger DB Migration
FunctionName: !Join ['-', [!Ref 'AWS::StackName', 'migrate-db'] ]
Timeout: 900
VpcConfig:
SecurityGroupIds:
- !GetAtt VPC.DefaultSecurityGroup
SubnetIds:
- !Ref Subnet1
- !Ref Subnet2
NLBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckProtocol: TCP
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 2
TargetType: ip
Port: !FindInMap ['ServiceInfo', 'ContainerPort', 'value']
Protocol: TCP
UnhealthyThresholdCount: 2
VpcId: !Ref 'VPC'
NLBTargetGroupDBMigrate:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckProtocol: TCP
HealthCheckTimeoutSeconds: 10
HealthCheckPort: 8080
HealthyThresholdCount: 2
TargetType: ip
Port: 8082
Protocol: TCP
UnhealthyThresholdCount: 2
VpcId: !Ref 'VPC'
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: DBSubnetGroup for RDS instances
SubnetIds:
- Ref: Subnet1
- Ref: Subnet2
RDSSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security Group for RDS
VpcId: !Ref 'VPC'
PostgresIngressRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref 'RDSSecurityGroup'
SourceSecurityGroupId: !Ref 'FargateSecurityGroup'
IpProtocol: tcp
FromPort: 5432
ToPort: 5432
RDSMasterInstance:
Type: AWS::RDS::DBInstance
Properties:
DBName: 'metaflow'
AllocatedStorage: 20
DBInstanceClass: 'db.t3.small'
DeleteAutomatedBackups: 'true'
StorageType: 'gp2'
Engine: 'postgres'
EngineVersion: '16.1'
MasterUsername: !Join ['', ['{{resolve:secretsmanager:', !Ref MyRDSSecret, ':SecretString:username}}' ]]
MasterUserPassword: !Join ['', ['{{resolve:secretsmanager:', !Ref MyRDSSecret, ':SecretString:password}}' ]]
VPCSecurityGroups:
- !Ref 'RDSSecurityGroup'
DBSubnetGroupName: !Ref 'DBSubnetGroup'
MyRDSSecret:
Type: "AWS::SecretsManager::Secret"
Properties:
Description: "This is a Secrets Manager secret for an RDS DB instance"
GenerateSecretString:
SecretStringTemplate: '{"username": "master"}'
GenerateStringKey: "password"
PasswordLength: 16
ExcludeCharacters: '"@/\'
SecretRDSInstanceAttachment:
Type: "AWS::SecretsManager::SecretTargetAttachment"
Properties:
SecretId: !Ref MyRDSSecret
TargetId: !Ref RDSMasterInstance
TargetType: AWS::RDS::DBInstance
MetaflowS3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
AccessControl: Private
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
SagemakerSecurityGroup:
Condition: 'EnableSagemaker'
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: 'Security Group for Sagemaker'
VpcId: !Ref VPC
SecurityGroupIngress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "TCP"
FromPort: 8080
ToPort: 8080
SageMakerExecutionRole:
Condition: 'EnableSagemaker'
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
Service:
- sagemaker.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: IAM_PASS_ROLE
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: AllowPassRole
Effect: Allow
Action: iam:PassRole
Resource: '*'
Condition:
StringEquals:
iam:PassedToService: sagemaker.amazonaws.com
- PolicyName: MISC_PERMISSIONS
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: MiscPermissions
Effect: Allow
Action:
- cloudwatch:PutMetricData
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- ecr:GetAuthorizationToken
- ecr:BatchCheckLayerAvailability
Resource: '*'
- PolicyName: log_roles_policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: CreateLogStream
Effect: Allow
Action:
- logs:CreateLogStream
Resource:
- !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/batch/job:log-stream:*' ] ]
- !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/sagemaker/NotebookInstances:log-stream:*' ] ]
- Sid: LogEvents
Effect: Allow
Action:
- logs:PutLogEvents
- logs:GetLogEvents
Resource:
- !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/sagemaker/NotebookInstances:log-stream:', !Ref 'AWS::StackName', '-NotebookInstance-', '{{resolve:secretsmanager:', !Ref RandomString, ':SecretString:password}}', '/jupyter.log' ] ]
- !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/sagemaker/NotebookInstances:log-stream:', !Ref 'AWS::StackName', '-NotebookInstance-', '{{resolve:secretsmanager:', !Ref RandomString, ':SecretString:password}}', '/LifecycleConfigOnCreate' ] ]
- !Join [ "", [ !Sub 'arn:${IAMPartition}:logs:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':log-group:/aws/batch/job:log-stream:job-queue-' ] ]
- Sid: LogGroup
Effect: Allow
Action:
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:CreateLogGroup
Resource: '*'
- PolicyName: SageMakerPermissions
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: SageMakerNotebook
Effect: "Allow"
Action:
- "sagemaker:DescribeNotebook*"
- "sagemaker:StartNotebookInstance"
- "sagemaker:StopNotebookInstance"
- "sagemaker:UpdateNotebookInstance"
- "sagemaker:CreatePresignedNotebookInstanceUrl"
Resource:
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance/${AWS::StackName}*"
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance-lifecycle-config/basic*"
- PolicyName: CustomS3ListAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: BucketAccess
Effect: Allow
Action: s3:ListBucket
Resource: !GetAtt 'MetaflowS3Bucket.Arn'
- PolicyName: CustomS3Access
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: ObjectAccess
Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:DeleteObject
Resource: !Join ['', [ !GetAtt 'MetaflowS3Bucket.Arn', '/*' ]]
- PolicyName: DenyPresigned
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: DenyPresigned
Effect: Deny
Action: s3:*
Resource: '*'
Condition:
StringNotEquals:
s3:authType: REST-HEADER
MetaflowUserRole:
Condition: 'EnableRole'
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Effect: Allow
Principal:
AWS:
- !GetAtt 'MetadataSvcECSTaskRole.Arn'
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: "Metaflow-Policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "cloudformation:DescribeStacks"
- "cloudformation:*Stack"
- "cloudformation:*ChangeSet"
Resource:
- !Sub "arn:${IAMPartition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}-*"
- Effect: "Allow"
Action:
- "s3:*Object"
Resource:
- !GetAtt 'MetaflowS3Bucket.Arn'
- !Join ['', [ !GetAtt 'MetaflowS3Bucket.Arn', '/*' ]]
- Effect: "Allow"
Action:
- "sagemaker:DescribeNotebook*"
- "sagemaker:StartNotebookInstance"
- "sagemaker:StopNotebookInstance"
- "sagemaker:UpdateNotebookInstance"
- "sagemaker:CreatePresignedNotebookInstanceUrl"
Resource:
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance/${AWS::StackName}-*"
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance-lifecycle-config/basic*"
- Effect: "Allow"
Action:
- "iam:PassRole"
Resource:
- !Sub "arn:${IAMPartition}:iam::${AWS::AccountId}:role/${AWS::StackName}-*"
- Effect: "Allow"
Action:
- "kms:Decrypt"
- "kms:Encrypt"
Resource:
- !Sub "arn:${IAMPartition}:kms:${AWS::Region}:${AWS::AccountId}:key/"
- PolicyName: BatchPerms
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: JobsPermissions
Effect: Allow
Action:
- "batch:TerminateJob"
- "batch:DescribeJobs"
- "batch:DescribeJobDefinitions"
- "batch:DescribeJobQueues"
- "batch:RegisterJobDefinition"
- "batch:DescribeComputeEnvironments"
Resource: '*'
- Sid: DefinitionsPermissions
Effect: Allow
Action:
- "batch:SubmitJob"
Resource:
- !Ref "JobQueue"
- !Sub arn:${IAMPartition}:batch:${AWS::Region}:${AWS::AccountId}:job-definition/*:*
- PolicyName: CustomS3ListAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: BucketAccess
Effect: Allow
Action: s3:ListBucket
Resource: !GetAtt 'MetaflowS3Bucket.Arn'
- PolicyName: LogPerms
PolicyDocument:
Version: '2012-10-17'
Statement:
Sid: GetLogs
Effect: Allow
Action: logs:GetLogEvents
Resource: !Sub arn:${IAMPartition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:*:log-stream:*
- PolicyName: AllowSagemaker
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: AllowSagemakerCreate
Effect: Allow
Action: sagemaker:CreateTrainingJob
Resource: !Sub arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:training-job/*
- Sid: AllowSagemakerDescribe
Effect: Allow
Action: sagemaker:DescribeTrainingJob
Resource: !Sub arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:training-job/*
- PolicyName: AllowStepFunctions
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: TasksAndExecutionsGlobal
Effect: Allow
Action:
- "states:ListStateMachines"
Resource: '*'
- Sid: StateMachines
Effect: Allow
Action:
- "states:DescribeStateMachine"
- "states:UpdateStateMachine"
- "states:StartExecution"
- "states:CreateStateMachine"
- "states:ListExecutions"
- "states:StopExecution"
Resource: !Sub 'arn:${IAMPartition}:states:${AWS::Region}:${AWS::AccountId}:stateMachine:*'
- PolicyName: AllowEventBridge
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: RuleMaintenance
Effect: Allow
Action:
- "events:PutTargets"
- "events:DisableRule"
Resource: !Sub "arn:${IAMPartition}:events:${AWS::Region}:${AWS::AccountId}:rule/*"
- Sid: PutRule
Effect: Allow
Action:
- "events:PutRule"
Resource: !Sub "arn:${IAMPartition}:events:${AWS::Region}:${AWS::AccountId}:rule/*"
Condition:
"Null":
events:source: true
MetaflowUserPolicy:
Condition: 'EnableRole'
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "cloudformation:DescribeStacks"
- "cloudformation:*Stack"
- "cloudformation:*ChangeSet"
Resource:
- !Sub "arn:${IAMPartition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}-*"
- Effect: "Allow"
Action:
- "s3:*Object"
Resource:
- !GetAtt 'MetaflowS3Bucket.Arn'
- !Join ['', [ !GetAtt 'MetaflowS3Bucket.Arn', '/*' ]]
- Effect: "Allow"
Action:
- "sagemaker:DescribeNotebook*"
- "sagemaker:StartNotebookInstance"
- "sagemaker:StopNotebookInstance"
- "sagemaker:UpdateNotebookInstance"
- "sagemaker:CreatePresignedNotebookInstanceUrl"
Resource:
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance/${AWS::StackName}-*"
- !Sub "arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:notebook-instance-lifecycle-config/basic*"
- Effect: "Allow"
Action:
- "iam:PassRole"
Resource:
- !Sub "arn:${IAMPartition}:iam::${AWS::AccountId}:role/${AWS::StackName}-*"
- Effect: "Allow"
Action:
- "kms:Decrypt"
- "kms:Encrypt"
Resource:
- !Sub "arn:${IAMPartition}:kms:${AWS::Region}:${AWS::AccountId}:key/"
- Sid: JobsPermissions
Effect: Allow
Action:
- "batch:TerminateJob"
- "batch:DescribeJobs"
- "batch:DescribeJobDefinitions"
- "batch:DescribeJobQueues"
- "batch:RegisterJobDefinition"
- "batch:DescribeComputeEnvironments"
Resource: '*'
- Sid: DefinitionsPermissions
Effect: Allow
Action:
- "batch:SubmitJob"
Resource:
- !Ref "JobQueue"
- !Sub arn:${IAMPartition}:batch:${AWS::Region}:${AWS::AccountId}:job-definition/*:*
- Sid: BucketAccess
Effect: Allow
Action: s3:ListBucket
Resource: !GetAtt 'MetaflowS3Bucket.Arn'
- Sid: GetLogs
Effect: Allow
Action: logs:GetLogEvents
Resource: !Sub arn:${IAMPartition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:*:log-stream:*
- Sid: AllowSagemakerCreate
Effect: Allow
Action: sagemaker:CreateTrainingJob
Resource: !Sub arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:training-job/*
- Sid: AllowSagemakerDescribe
Effect: Allow
Action: sagemaker:DescribeTrainingJob
Resource: !Sub arn:${IAMPartition}:sagemaker:${AWS::Region}:${AWS::AccountId}:training-job/*
- Sid: TasksAndExecutionsGlobal
Effect: Allow
Action:
- "states:ListStateMachines"
Resource: '*'
- Sid: StateMachines
Effect: Allow
Action:
- "states:DescribeStateMachine"
- "states:UpdateStateMachine"
- "states:StartExecution"
- "states:CreateStateMachine"
- "states:ListExecutions"
- "states:StopExecution"
Resource: !Sub 'arn:${IAMPartition}:states:${AWS::Region}:${AWS::AccountId}:stateMachine:*'
- Sid: RuleMaintenance
Effect: Allow
Action:
- "events:PutTargets"
- "events:DisableRule"
Resource: !Sub "arn:${IAMPartition}:events:${AWS::Region}:${AWS::AccountId}:rule/*"
- Sid: PutRule
Effect: Allow
Action:
- "events:PutRule"
Resource: !Sub "arn:${IAMPartition}:events:${AWS::Region}:${AWS::AccountId}:rule/*"
Condition:
"Null":
events:source: true
EventBridgeRole: