-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
1222 lines (1188 loc) · 40.5 KB
/
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"
Parameters:
ProjectPrefix:
Type: String
Description: Project prefix for naming resources
Default: de-c4w4a2
AWSRegion:
Type: String
Description: Default AWS Region
Default: us-east-1
VPCCIDR:
Type: String
Description: CIDR of VPC. IPv4 address range in CIDR notation.
Default: 10.0.0.0/16
PublicSubnetCIDR:
Type: String
Description: CIDR of a public subnet. IPv4 address range in CIDR notation.
Default: 10.0.1.0/24
PrivateSubnetACIDR:
Type: String
Description: CIDR of a private subnet A. IPv4 address range in CIDR notation.
Default: 10.0.3.0/24
PrivateSubnetBCIDR:
Type: String
Description: CIDR of a private subnet B. IPv4 address range in CIDR notation.
Default: 10.0.4.0/24
PublicBucketName:
Type: String
Description: Public bucket name for assets.
Default: dlai-data-engineering
PublickBucketDBTDockerfile:
Type: String
Description: Public bucket key for sql file.
Default: labs/cfn_dependencies/c4w4a2/docker/dbt/Dockerfile
PublickBucketDBTProjectFolder:
Type: String
Description: Public bucket key for sql file.
Default: labs/cfn_dependencies/c4w4a2/docker/dbt/dbt_project
PublickBucketAirflowFolder:
Type: String
Description: Public bucket key for sql file.
Default: labs/cfn_dependencies/c4w4a2/docker/airflow
PublickBucketAirflowDags:
Type: String
Description: Public bucket key for sql file.
Default: labs/cfn_dependencies/c4w4a2/dags
PublicBucketPostgresDDLObjectKey:
Type: String
Description: Public bucket key for sql file.
Default: labs/cfn_dependencies/c4w4a2/songs_ddl.sql
PublicBucketLayerKey:
Type: String
Description: Public bucket key for dependencies file.
Default: labs/cfn_dependencies/c4w4a2/lambda_layer_dependencies_p312.zip
PublicBucketPostgresLayerKey:
Type: String
Description: Public bucket key for dependencies file.
Default: labs/cfn_dependencies/c4w4a2/lambda_layer_dependencies_p38.zip
PublicBucketPostgresLambdaKey:
Type: String
Description: Public bucket key for postgres load zip file.
Default: labs/cfn_dependencies/c4w4a2/postgres_load.py.zip
PublicVsCodeDockerRepo:
Description: ECR repo with the vscode docker image
Type: String
Default: public.ecr.aws/deeplearning-ai/dlai-de-code-server:latest
PublicBucketVSCodeServiceKey:
Type: String
Description: Public bucket key for postgres load zip file.
Default: labs/cfn_dependencies/c4w4a2/docker_vscode_service.service
PublicBucketAPIServiceKey:
Type: String
Description: Public bucket key for postgres load zip file.
Default: labs/cfn_dependencies/c4w4a2/docker_api_service.service
PublicBucketSupersetServiceKey:
Type: String
Description: Public bucket key for postgres load zip file.
Default: labs/cfn_dependencies/c4w4a2/docker_superset_service.service
PublicBucketLambdaKey:
Type: String
Description: Public bucket key for lab clone lambda zip file.
Default: labs/cfn_dependencies/c4w4a2/policy_clone.zip
PublicBucketPolicyPath:
Type: String
Description: Public bucket key for lab policy file.
Default: labs/cfn_dependencies/c4w4a2/lab.policy
PublicBucketLabContentKey:
Type: String
Description: Public bucket key for lab_content folder.
Default: labs/c4w4a2-343017
PostgresLambdaRuntime:
Type: String
Description: Postgres Lambda function Runtime
Default: python3.8
Timeout:
Type: Number
Description: Lambda function Timeout
Default: 900
VSCodeRoleName:
Type: String
Description: Role name for the VSCode Instance
Default: VSCodeInstanceRole
LatestAmiId:
Description: The latest Amazon Linux 2023 AMI from the Parameter Store
Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>"
Default: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
VSCodeInstanceType:
Description: The EC2 instance type
Type: String
Default: t3.medium
AllowedValues:
- t3.medium
- t3.large
InstanceType:
Description: The EC2 instance type
Type: String
Default: t3.xlarge
AllowedValues:
- t3.xlarge
# AirflowInstanceType:
# Description: The EC2 instance type
# Type: String
# Default: t3.large
# AllowedValues:
# - t3.large
PostgresMasterUsername:
Type: String
Description: A user name for RDS database instance.
MinLength: 1
MaxLength: 16
AllowedPattern: "[a-zA-Z][a-zA-Z0-9]*"
Default: postgresuser
ConstraintDescription: must begin with a letter and contain only alphanumeric characters.
PostgresMasterUserPassword:
Type: String
Description: >-
A user password for RDS database instance default password Awsadmin123. (8
characters minimum, 41 characters maximum.)
NoEcho: true
MinLength: 8
MaxLength: 41
AllowedPattern: "[a-zA-Z0-9]*"
Default: adminpwrd
ConstraintDescription: must contain only alphanumeric characters.
PostgresAllocatedStorage:
Default: "20"
Description: The size of the database (GiB)
Type: Number
MinValue: "20"
MaxValue: "65536"
ConstraintDescription: must be between 20 and 65536 GiB.
PostgresDBName:
Type: String
Default: "postgres"
KeyName:
Type: String
Default: KeyName
ProducerDockerRepo:
Description: ECR repo with the producer docker image
Type: String
Default: public.ecr.aws/deeplearning-ai/dlai-de-c4w4a2-api:latest
RedshiftDatabaseName:
Description: The name of the database to be created
Type: String
Default: dev
AllowedPattern: "([a-z]|[0-9])+"
RedshiftMasterUsername:
Description:
The user name that is associated with the master user account for
the cluster that is being created
Type: String
Default: defaultuser
AllowedPattern: "([a-z])([a-z]|[0-9])*"
RedshiftMasterUserPassword:
Description:
The password that is associated with the master user account for
the cluster that is being created.
Type: String
NoEcho: "true"
Default: Defaultuserpwrd1234+
RedshiftPortNumber:
Description: The port number on which the cluster accepts incoming connections.
Type: Number
Default: 5439
APIPort:
Type: Number
Description: API port
Default: 8000
AirflowPort:
Type: Number
Description: Airflow port
Default: 8080
SupersetPort:
Type: Number
Description: Airflow port
Default: 8088
VsCodePort:
Type: Number
Description: VS Code port
Default: 8443
Runtime:
Type: String
Description: Lambda function Runtime
Default: python3.12
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VPCCIDR
EnableDnsHostnames: true
EnableDnsSupport: true
InstanceTenancy: default
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Application
Value: !Ref AWS::StackId
- Key: Name
Value: !Sub ${ProjectPrefix}-igw
InternetGatewayAttachment:
DependsOn:
- InternetGateway
- VPC
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet:
DependsOn: VPC
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- "0"
- !GetAZs ""
CidrBlock: !Ref PublicSubnetCIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-public-subnet
PrivateSubnetA:
DependsOn: VPC
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- "0"
- !GetAZs ""
CidrBlock: !Ref PrivateSubnetACIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-private-subnet-a
PrivateSubnetB:
DependsOn: VPC
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select
- "1"
- !GetAZs ""
CidrBlock: !Ref PrivateSubnetBCIDR
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-private-subnet-b
InternetGatewayRoute:
DependsOn:
- InternetGatewayAttachment
- PublicRouteTable
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref PublicRouteTable
PublicRouteTable:
DependsOn: VPC
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-public-routetable
VpcId: !Ref VPC
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnet
RouteTableId: !Ref PublicRouteTable
PrivateSubnetARouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetA
RouteTableId: !Ref PrivateRouteTable
PrivateSubnetBRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnetB
RouteTableId: !Ref PrivateRouteTable
NatGatewayRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NATGateway
PrivateRouteTable:
DependsOn: VPC
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-private-routetable
VpcId: !Ref VPC
NATGateway:
DependsOn:
- NATGatewayEIPA
- PublicSubnet
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NATGatewayEIPA.AllocationId
SubnetId: !Ref PublicSubnet
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-natgw
NATGatewayEIPA:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
DefaultVPCSecurityGroup:
DependsOn: VPC
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Default Security Group for the VPC.
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub ${ProjectPrefix}-sg
DefaultVPCSecurityGroupSelfRefIngress:
DependsOn: DefaultVPCSecurityGroup
Type: AWS::EC2::SecurityGroupIngress
Properties:
SourceSecurityGroupId: !Ref DefaultVPCSecurityGroup
IpProtocol: "-1"
GroupId: !Ref DefaultVPCSecurityGroup
DBIngressRule:
DependsOn: DefaultVPCSecurityGroup
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
FromPort: "3306"
ToPort: "3306"
GroupId: !Ref DefaultVPCSecurityGroup
IpProtocol: tcp
CidrIp: 0.0.0.0/0
PostgreSQLIngressRule:
DependsOn: DefaultVPCSecurityGroup
Type: "AWS::EC2::SecurityGroupIngress"
Properties:
FromPort: "5432"
ToPort: "5432"
GroupId: !Ref DefaultVPCSecurityGroup
IpProtocol: tcp
CidrIp: 0.0.0.0/0
PostgresSubnetGroup:
Type: "AWS::RDS::DBSubnetGroup"
Properties:
DBSubnetGroupDescription: Subnet available for the DB Instance
SubnetIds:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
DependsOn:
- PrivateSubnetA
- PrivateSubnetB
PostgresDB:
DependsOn:
- DefaultVPCSecurityGroup
- PostgresSubnetGroup
- PrivateSubnetA
Type: "AWS::RDS::DBInstance"
DeletionPolicy: Delete
Properties:
DBInstanceIdentifier: !Sub "${ProjectPrefix}-rds"
DBName: dev
DBInstanceClass: db.t3.small
AllocatedStorage: !Ref PostgresAllocatedStorage
AvailabilityZone: !GetAtt
- PrivateSubnetA
- AvailabilityZone
DBSubnetGroupName: !Ref PostgresSubnetGroup
Engine: postgres
EngineVersion: 15.5
MasterUsername: !Ref PostgresMasterUsername
MasterUserPassword: !Ref PostgresMasterUserPassword
MultiAZ: false
PubliclyAccessible: false
BackupRetentionPeriod: 0
VPCSecurityGroups:
- !Ref DefaultVPCSecurityGroup
AssociatedRoles:
- FeatureName: s3Import
RoleArn: !GetAtt LoadIAMRole.Arn
PostgresLambdaLayer:
Type: AWS::Lambda::LayerVersion
Properties:
CompatibleArchitectures:
- arm64
CompatibleRuntimes:
- !Ref PostgresLambdaRuntime
Content:
S3Bucket: !Ref PublicBucketName
S3Key: !Ref PublicBucketPostgresLayerKey
Description: Lambda layer with dependencies to insert data into postgres DBs
LayerName: !Sub ${ProjectPrefix}-lambda-layer-postgres
PostgresLoadFunction:
DependsOn:
- PostgresLambdaLayer
- PostgresDB
Type: "AWS::Lambda::Function"
Properties:
Environment:
Variables:
DBHOST: !GetAtt
- PostgresDB
- Endpoint.Address
DBPORT: !GetAtt
- PostgresDB
- Endpoint.Port
DBNAME: !Ref PostgresDBName
DBUSER: !Ref PostgresMasterUsername
DBPASSWORD: !Ref PostgresMasterUserPassword
BUCKET_NAME: !Ref PublicBucketName
OBJECT_KEY: !Ref PublicBucketPostgresDDLObjectKey
IAM_ROLE_ARN: !GetAtt LoadIAMRole.Arn
Code:
S3Bucket: !Ref PublicBucketName
S3Key: !Ref PublicBucketPostgresLambdaKey
FunctionName: !Sub "${ProjectPrefix}-postgres-lambda-function"
Handler: postgres_load.lambda_handler
Layers:
- !Ref PostgresLambdaLayer
Runtime: !Ref PostgresLambdaRuntime
Role: !GetAtt FunctionRole.Arn
Timeout: !Ref Timeout
VpcConfig:
SecurityGroupIds:
- !Ref DefaultVPCSecurityGroup
SubnetIds:
- !Ref PrivateSubnetA
FunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service:
- lambda.amazonaws.com
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
CFPostgresLambdaExecution:
DependsOn:
- PostgresLoadFunction
Type: "Custom::CFPostgresLambdaExecution"
Properties:
ServiceToken: !GetAtt PostgresLoadFunction.Arn
DeletionPolicy: Delete
IacS3Bucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub "${ProjectPrefix}-${AWS::AccountId}-${AWS::Region}-terraform-state"
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
DataLakeS3Bucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub "${ProjectPrefix}-${AWS::AccountId}-${AWS::Region}-data-lake"
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LoadIAMRole:
Type: "AWS::IAM::Role"
DependsOn:
- FunctionRole
- DataLakeS3Bucket
Properties:
RoleName: !Sub ${ProjectPrefix}-load-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "redshift.amazonaws.com"
- "rds.amazonaws.com"
- "glue.amazonaws.com"
Action:
- "sts:AssumeRole"
- Effect: Allow
Principal:
AWS: !GetAtt FunctionRole.Arn
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Spectrum-Glue-Access-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- s3:GetBucketLocation
- s3:GetObject
- s3:ListMultipartUploadParts
- s3:ListBucket
- s3:ListBucketMultipartUploads
Resource:
- !Sub "arn:aws:s3:::${PublicBucketName}"
- !Sub "arn:aws:s3:::${PublicBucketName}/*"
- !Sub "arn:aws:s3:::${ProjectPrefix}-${AWS::AccountId}-${AWS::Region}-data-lake"
- !Sub "arn:aws:s3:::${ProjectPrefix}-${AWS::AccountId}-${AWS::Region}-data-lake/*"
- Effect: Allow
Action:
- glue:CreateDatabase
- glue:DeleteDatabase
- glue:GetDatabase
- glue:GetDatabases
- glue:UpdateDatabase
- glue:CreateTable
- glue:DeleteTable
- glue:BatchDeleteTable
- glue:UpdateTable
- glue:GetTable
- glue:GetTables
- glue:BatchCreatePartition
- glue:CreatePartition
- glue:DeletePartition
- glue:BatchDeletePartition
- glue:UpdatePartition
- glue:GetPartition
- glue:GetPartitions
- glue:BatchGetPartition
- logs:*
Resource:
- "*"
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: EC2 Security Group
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${ProjectPrefix}-ec2-sg"
EC2SecurityGroupIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: 22
ToPort: 22
EC2SecurityGroupIngressAPI:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: !Ref APIPort
ToPort: !Ref APIPort
EC2SecurityGroupIngressAirflow:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: !Ref AirflowPort
ToPort: !Ref AirflowPort
EC2SecurityGroupIngressHTTPS:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: 443
ToPort: 443
EC2SecurityGroupIngressRedshift:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: 5439
ToPort: 5439
EC2SecurityGroupIngressSuperset:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: !Ref SupersetPort
ToPort: !Ref SupersetPort
EC2SecurityGroupIngressVsCode:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: !Ref VsCodePort
ToPort: !Ref VsCodePort
EC2SecurityGroupIngressJupyter:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: !Ref EC2SecurityGroup
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: 8888
ToPort: 8888
EC2KeyPair:
Type: AWS::EC2::KeyPair
Properties:
KeyName: !Sub "${ProjectPrefix}-ec2-keypair"
EC2IAMRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: !Sub "${ProjectPrefix}-ec2-role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: "sts:AssumeRole"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess
- arn:aws:iam::aws:policy/AmazonAthenaFullAccess
- arn:aws:iam::aws:policy/AmazonKinesisFullAccess
- arn:aws:iam::aws:policy/AWSGlueConsoleFullAccess
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
- arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilderECRContainerBuilds
Policies:
- PolicyName: S3AccessPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: "Statement1"
Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::de-c4w4*"
- Sid: "S3AccessDLAIBucket"
Action:
- "s3:List*"
- "s3:Get*"
Effect: "Allow"
Resource:
- "arn:aws:s3:::dlai-data-engineering"
- "arn:aws:s3:::dlai-data-engineering/*"
- Sid: "IAMAccess"
Action:
- "iam:Pass*"
- "iam:Get*"
- "iam:List*"
Effect: "Allow"
Resource:
- "*"
- Sid: "AssumeGlueRole"
Effect: "Allow"
Action: "sts:AssumeRole"
Resource: !Sub "arn:aws:iam::${AWS::AccountId}:role/*${ProjectPrefix}glue-role"
EC2InstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles:
- !Ref EC2IAMRole
RedshiftCluster:
Type: AWS::Redshift::Cluster
DependsOn: InternetGatewayAttachment
Properties:
ClusterIdentifier: !Sub "${ProjectPrefix}-redshift-cluster"
ClusterType: single-node
NumberOfNodes: 1
NodeType: dc2.large
DBName:
Ref: RedshiftDatabaseName
MasterUsername:
Ref: RedshiftMasterUsername
MasterUserPassword:
Ref: RedshiftMasterUserPassword
ClusterParameterGroupName:
Ref: RedshiftClusterParameterGroup
VpcSecurityGroupIds:
- Ref: RedshiftSecurityGroup
ClusterSubnetGroupName:
Ref: RedshiftClusterSubnetGroup
PubliclyAccessible: "true"
Port:
Ref: RedshiftPortNumber
IamRoles:
- !GetAtt LoadIAMRole.Arn
RedshiftClusterParameterGroup:
Type: AWS::Redshift::ClusterParameterGroup
Properties:
Description: Cluster parameter group
ParameterGroupFamily: redshift-1.0
Parameters:
- ParameterName: enable_user_activity_logging
ParameterValue: "true"
RedshiftClusterSubnetGroup:
Type: AWS::Redshift::ClusterSubnetGroup
Properties:
Description: Cluster subnet group
SubnetIds:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
DependsOn:
- PrivateSubnetA
- PrivateSubnetB
RedshiftSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort:
Ref: RedshiftPortNumber
ToPort:
Ref: RedshiftPortNumber
IpProtocol: tcp
VpcId:
Ref: VPC
DataArchitectureInstance:
DependsOn:
- EC2InstanceProfile
- DBTBucket
- DagsBucket
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
InstanceType: !Ref InstanceType
KeyName: !Ref EC2KeyPair # KeyName
IamInstanceProfile: !Ref EC2InstanceProfile
BlockDeviceMappings:
- DeviceName: "/dev/xvda"
Ebs:
VolumeSize: "30"
NetworkInterfaces:
- DeviceIndex: "0"
SubnetId: !Ref PublicSubnet
GroupSet:
- !Ref EC2SecurityGroup
Tags:
- Key: Name
Value: !Sub "${ProjectPrefix}-da-instance"
UserData:
{
"Fn::Base64":
{
"Fn::Join":
[
"",
[
"#!/bin/bash\n",
"set -ex\n",
"echo ----- Configuring API ----- \n",
"export PRODUCER_DOCKER_REPO=",
{ "Ref": "ProducerDockerRepo" },
"\n",
"echo $PRODUCER_DOCKER_REPO\n",
"sudo yum update -y\n",
"sudo yum install docker git -y\n",
"sudo service docker start\n",
"echo ------ Configuring Airflow ----- \n",
"export de_project=",
{ "Ref": "ProjectPrefix" },
"\n",
"export AWS_DEFAULT_REGION=",
{ "Ref": "AWSRegion" },
"\n",
"export redshift_host=$(aws redshift describe-clusters --cluster-identifier $de_project-redshift-cluster --query \"Clusters[0].Endpoint.Address\" --output text) \n",
"wget https://s3.amazonaws.com/mountpoint-s3-release/latest/x86_64/mount-s3.rpm\n",
"sudo yum install -y ./mount-s3.rpm\n",
"mkdir -p /docker_dbt/dbt_project\n",
"cd /docker_dbt\n",
"sudo aws s3 cp s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublickBucketDBTDockerfile" },
" ./Dockerfile \n",
"sudo chmod 777 -R ./dbt_project\n",
"sudo mount-s3 --dir-mode 0766 --file-mode 0766 --allow-overwrite --allow-delete --allow-other --prefix dbt_project/ ",
{ "Ref": "DBTBucket" },
" ./dbt_project\n",
"sudo aws s3 sync s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublickBucketDBTProjectFolder" },
" ./dbt_project_local \n",
"sudo sed -i \"s/<REDSHIFT_ENDPOINT>/$redshift_host/g\" ./dbt_project_local/.dbt/profiles.yml\n",
"sudo aws s3 cp ./dbt_project_local s3://",
{ "Ref": "DBTBucket" },
"/dbt_project/ --recursive \n",
"echo mount-s3 service ready for dbt_project folder\n",
"sudo aws s3 cp s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublickBucketAirflowDags" },
"/ ./airflow_dags/ --recursive \n",
"sudo aws s3 cp ./airflow_dags s3://",
{ "Ref": "DagsBucket" },
"/dags --recursive \n",
"mkdir -p /opt/airflow\n",
"cd /opt/airflow\n",
"sudo aws s3 sync s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublickBucketAirflowFolder" },
" . \n",
"mkdir -p ./dags ./logs ./plugins\n",
"sudo chmod 777 -R ./logs \n",
"sudo chmod 777 -R ./dags \n",
"sudo mount-s3 --dir-mode 0766 --file-mode 0766 --allow-overwrite --allow-delete --allow-other --prefix dags/ ",
{ "Ref": "DagsBucket" },
" ./dags",
"\n",
"echo mount-s3 service ready for dags folder\n",
"sudo yum install -y docker\n",
"sudo service docker start\n",
"sudo usermod -a -G docker ec2-user\n",
"mkdir -p /usr/local/lib/docker/cli-plugins\n",
"curl -SL https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose\n",
"chmod +x /usr/local/lib/docker/cli-plugins/docker-compose\n",
"echo docker compose has been installed\n",
"sudo chmod 666 /var/run/docker.sock\n",
"echo Airflow folders created\n",
"echo -e \"AIRFLOW_UID=$(id -u)\\nAIRFLOW_GID=0\" > .env\n",
"echo -e \"AIRFLOW__SCHEDULER__MIN_FILE_PROCESS_INTERVAL=5\\nAIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL=30\" >> .env\n",
"echo -e \"PYTHONDONTWRITEBYTECODE=''\\nPYTHONPATH=/opt/airflow/dags:${PYTHONPATH}\" >> .env\n",
"sudo docker compose up -d airflow-init\n",
"sudo docker compose up -d\n",
"echo Airflow service is running\n",
"echo ------ Configuring Superset ----- \n",
"sudo aws s3 cp s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublicBucketSupersetServiceKey" },
" /etc/systemd/system/docker_superset_service.service\n",
"cd / \n",
"git clone https://github.com/apache/superset.git\n",
"cd superset\n",
"echo \"sqlalchemy-redshift\" >> ./docker/requirements-local.txt\n",
"sudo chmod 644 /etc/systemd/system/docker_superset_service.service\n",
"sudo systemctl daemon-reload\n",
"sudo systemctl enable docker_superset_service.service\n",
"sudo systemctl start docker_superset_service.service\n",
"echo Superset service is running\n",
],
],
},
}
DataArchitectureInstanceEIP:
Type: "AWS::EC2::EIP"
Properties:
InstanceId: !Ref DataArchitectureInstance
VsCodeInstance:
DependsOn:
- VSCodeEC2InstanceProfile
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref LatestAmiId
InstanceType: !Ref VSCodeInstanceType
KeyName: !Ref EC2KeyPair # KeyName
IamInstanceProfile: !Ref VSCodeEC2InstanceProfile
NetworkInterfaces:
- DeviceIndex: "0"
SubnetId: !Ref PublicSubnet
GroupSet:
- !Ref EC2SecurityGroup
Tags:
- Key: Name
Value: !Sub "${ProjectPrefix}-vscode-instance"
UserData:
{
"Fn::Base64":
{
"Fn::Join":
[
"",
[
"#!/bin/bash\n",
"set -ex\n",
"sudo aws s3 cp s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublicBucketVSCodeServiceKey" },
" /etc/systemd/system/docker_vscode_service.service\n",
"sudo aws s3 cp s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublicBucketAPIServiceKey" },
" /etc/systemd/system/docker_api_service.service\n",
"export VSCODE_DOCKER_REPO=",
{ "Ref": "PublicVsCodeDockerRepo" },
"\n",
"echo $VSCODE_DOCKER_REPO\n",
"sudo sed -i 's|<VSCODE_DOCKER_REPO>|'\"$VSCODE_DOCKER_REPO\"'|g' /etc/systemd/system/docker_vscode_service.service \n",
"sudo mkdir /home/ec2-user/data\n",
"sudo aws s3 sync s3://",
{ "Ref": "PublicBucketName" },
"/",
{ "Ref": "PublicBucketLabContentKey" },
" /home/ec2-user/data\n",
"sudo chown -R 1000:1000 /home/ec2-user/data\n",
"sudo chmod -R 775 /home/ec2-user/data\n",
"export PRODUCER_DOCKER_REPO=",
{ "Ref": "ProducerDockerRepo" },
"\n",
"echo $PRODUCER_DOCKER_REPO\n",
"sudo sed -i 's|<PRODUCER_DOCKER_REPO>|'\"$PRODUCER_DOCKER_REPO\"'|g' /etc/systemd/system/docker_api_service.service \n",
"sudo yum update -y\n",
"sudo yum install docker -y\n",
"sudo service docker start\n",
"sudo usermod -a -G docker ec2-user\n",
"sudo mkdir -p /usr/local/lib/docker/cli-plugins\n",
"sudo curl -SL https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose\n",
"sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose\n",
"echo docker compose has been installed\n",
"sudo chmod 644 /etc/systemd/system/docker_vscode_service.service\n",
"sudo systemctl daemon-reload\n",
"sudo systemctl enable docker_vscode_service.service\n",
"sudo systemctl start docker_vscode_service.service\n",
"sudo docker pull $PRODUCER_DOCKER_REPO\n",
"echo 'Pulled Inference image from' $PRODUCER_DOCKER_REPO\n",
"sudo chmod 644 /etc/systemd/system/docker_api_service.service\n",
"sudo systemctl daemon-reload\n",
"sudo systemctl enable docker_api_service.service\n",
"sudo systemctl start docker_api_service.service\n",
"echo Producer ready!\n",
],
],
},
}
VsCodeInstanceEIP:
Type: "AWS::EC2::EIP"
Properties:
InstanceId: !Ref VsCodeInstance
LambdaLayer:
Type: AWS::Lambda::LayerVersion
Properties:
CompatibleArchitectures:
- arm64
CompatibleRuntimes:
- python3.12
Content:
S3Bucket: !Ref PublicBucketName
S3Key: !Ref PublicBucketLayerKey
Description: Lambda layer to setup Vscode instance profile
LayerName: !Sub ${ProjectPrefix}-lambda-layer
LambdaRoleVSCodeSetup:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub "${ProjectPrefix}-lambda-role-vscode-setup"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service:
- lambda.amazonaws.com
Policies:
- PolicyName: VSCodeInstanceRolePolicy