-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment10.yml
1089 lines (1085 loc) · 31.1 KB
/
assignment10.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: "Cloudformation Stack Template"
Parameters:
VpcCidrBlock:
Description: "CIDR Block for VPC"
Type: String
Default: "10.0.0.0/16"
VpcSubnetCidrBlocks:
Description: "List of CIDR blocks"
Type: CommaDelimitedList
Default: "10.0.10.0/24, 10.0.11.0/24, 10.0.12.0/24, 10.0.13.0/24, 10.0.14.0/24, 10.0.15.0/24"
VpcSubnetAvailabilityZones:
Description: "List of Availability Zones"
Type: CommaDelimitedList
Default: "us-east-1a, us-east-1b, us-east-1c"
PublicRouteDestinationCidrBlock:
Description: "CIDR Block for Public Route"
Type: String
Default: "0.0.0.0/0"
PublicSubnetNames:
Description: "Name Tags for Subnets"
Type: CommaDelimitedList
Default: "PublicSubnet1, PublicSubnet2, PublicSubnet3"
PrivateSubnetNames:
Description: "Name Tags for Subnets"
Type: CommaDelimitedList
Default: "PrivateSubnet1, PrivateSubnet2, PrivateSubnet3"
PublicRouteTableName:
Description: "Name Tag for Public Route Table"
Type: String
Default: "PublicRouteTable"
PrivateRouteTableName:
Description: "Name Tag for Private Route Table"
Type: String
Default: "PrivateRouteTable"
InternetGatewayName:
Description: "Name Tag for Internet Gateway"
Type: String
Default: "InternetGateway"
AppSecurityGroup:
Description: "Name Tag for Application Security Group"
Type: String
Default: "application"
DBSecurityGroupName:
Description: "Name Tag for DB Security Group"
Type: String
Default: "database"
AmiID:
Description: "AMI ID for Instance"
Type: String
EC2InstanceName:
Description: "Instance Name"
Type: String
Default: "NodeApp"
InstanceType:
Description: EC2 instance Type
Type: String
Default: t2.micro
S3BucketName:
Description: "S3 Bucket Name"
Type: String
Default: dev
Ipv4Cidr:
Description: "AMI ID for Instance"
Type: String
Default: "0.0.0.0/0"
Ipv6Cidr:
Description: "AMI ID for Instance"
Type: String
Default: "::/0"
ProfileName:
Default: demo
Type: String
Description: Dev/Demo(Prod)
myLaunchTemplateVersionNumber:
Type: String
Default: "1.0"
KeyName:
Type: String
Default: "key"
DNSName:
Type: String
Default: "demo.pramithijagdish.me."
emailaddress:
Description: email address
Type: String
Default: "[email protected]"
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidrBlock
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
InstanceTenancy: 'default'
Tags:
- Key: Name
Value: !Ref AWS::StackName
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Ref InternetGatewayName
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [0, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 0
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Select [0, !Ref PublicSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [1, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 1
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Select [1, !Ref PublicSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PublicSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [2, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 2
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Select [2, !Ref PublicSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PrivateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [3, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 0
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Select [0, !Ref PrivateSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PrivateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [4, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 1
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Select [1, !Ref PrivateSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PrivateSubnet3:
Type: AWS::EC2::Subnet
Properties:
VpcId:
Ref: VPC
CidrBlock: !Select [5, !Ref VpcSubnetCidrBlocks]
AvailabilityZone: !Select
- 2
- !GetAZs
Ref: 'AWS::Region'
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Select [2, !Ref PrivateSubnetNames]
- Key: VPC Name
Value: !Ref AWS::StackName
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Ref PublicRouteTableName
- Key: VPC Name
Value: !Ref AWS::StackName
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Name
Value: !Ref PrivateRouteTableName
- Key: VPC Name
Value: !Ref AWS::StackName
PublicRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId:
Ref: PublicRouteTable
DestinationCidrBlock: !Ref PublicRouteDestinationCidrBlock
GatewayId:
Ref: InternetGateway
PublicSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PublicSubnet1
RouteTableId:
Ref: PublicRouteTable
PublicSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PublicSubnet2
RouteTableId:
Ref: PublicRouteTable
PublicSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PublicSubnet3
RouteTableId:
Ref: PublicRouteTable
PrivateSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PrivateSubnet1
RouteTableId:
Ref: PrivateRouteTable
PrivateSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PrivateSubnet2
RouteTableId:
Ref: PrivateRouteTable
PrivateSubnet3RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PrivateSubnet3
RouteTableId:
Ref: PrivateRouteTable
ApplicationSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: application security group
GroupName: application
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3000
ToPort: 3000
SourceSecurityGroupId: !GetAtt LoadBalancerSecurityGroup.GroupId
- IpProtocol: tcp
FromPort: 3000
ToPort: 3000
SourceSecurityGroupId: !GetAtt LoadBalancerSecurityGroup.GroupId
- IpProtocol: tcp
Description: SSH
FromPort: 22
ToPort: 22
CidrIp:
Ref: Ipv4Cidr
- IpProtocol: tcp
Description: SSH
FromPort: 22
ToPort: 22
CidrIpv6:
Ref: Ipv6Cidr
Tags:
- Key: Name
Value: !Ref AppSecurityGroup
LoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: load balancer security group
GroupName: load balancer
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp:
Ref: Ipv4Cidr
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIpv6:
Ref: Ipv6Cidr
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp:
Ref: Ipv4Cidr
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIpv6:
Ref: Ipv6Cidr
DBSecurityGroup:
DependsOn:
- PrivateSubnet1
- PrivateSubnet2
- PrivateSubnet3
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: database
GroupName: database
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !GetAtt ApplicationSecurityGroup.GroupId
Tags:
- Key: Name
Value: !Ref DBSecurityGroupName
S3Bucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Delete
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
BucketName: !Join
- "."
- - !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split
- "/"
- !Ref "AWS::StackId"
- Ref: "ProfileName"
- "pramithijagdish.me"
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LifecycleConfiguration:
Rules:
- Id: Move to Standard IA
Prefix: profile
Status: Enabled
Transitions:
- TransitionInDays: 30
StorageClass: STANDARD_IA
Tags:
- Key: Name
Value: !Ref S3BucketName
DBSubnetGroup:
Type: 'AWS::RDS::DBSubnetGroup'
Properties:
DBSubnetGroupDescription: Private Subnet Group for MySql Database
DBSubnetGroupName: RDS Subnet Group
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2
- !Ref PrivateSubnet3
RDSDBParameterGroup:
Type: 'AWS::RDS::DBParameterGroup'
Properties:
Description: MySQL8
Family: mysql8.0
RDS:
Type: 'AWS::RDS::DBInstance'
DeletionPolicy: Delete
Properties:
MultiAZ: false
DBInstanceIdentifier: csye6225
DBName: csye6225
Port: 3306
DBInstanceClass: db.t3.micro
AllocatedStorage: 20
Engine: MySQL
EngineVersion: 8.0.27
MasterUsername: csye6225
MasterUserPassword: csye6225Spring2022
PubliclyAccessible: false
VPCSecurityGroups:
- !Ref DBSecurityGroup
DBSubnetGroupName:
Ref: DBSubnetGroup
DBParameterGroupName:
Ref: RDSDBParameterGroup
StorageEncrypted: true
KmsKeyId: !GetAtt RdsKmsKey.Arn
WebAppS3Policy:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: WebAppS3
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 's3:GetObject'
- 's3:PutObject'
- 's3:DeleteObject'
Resource:
- !Sub arn:aws:s3:::${S3Bucket}
- !Sub arn:aws:s3:::${S3Bucket}/*
Roles:
- !Ref IAMRole
IAMRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: EC2-CSYE6225
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
IAMInstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- Ref: IAMRole
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: LaunchTemplate
LaunchTemplateData:
IamInstanceProfile:
Arn: !GetAtt
- IAMInstanceProfile
- Arn
ImageId: !Ref AmiID
InstanceType: !Ref InstanceType
KeyName: !Ref KeyName
SecurityGroupIds:
- !Ref ApplicationSecurityGroup
BlockDeviceMappings:
- DeviceName: "/dev/sdm"
Ebs:
VolumeSize: 20
VolumeType: "gp2"
DeleteOnTermination: true
Encrypted: true
KmsKeyId: !GetAtt EBSKmsKey.Arn
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
echo export DB_HOST="${RDS.Endpoint.Address}" >> /etc/environment
echo export DB_USER="csye6225" >> /etc/environment
echo export DB_PASSWORD="csye6225Spring2022" >> /etc/environment
echo export DB_NAME="csye6225" >> /etc/environment
echo export AWS_BUCKET_NAME="${S3Bucket}" >> /etc/environment
echo export AWS_REGION="us-east-1" >> /etc/environment
echo export DB="csye6225" >> /etc/environment
sleep 20
source /etc/environment
rm -rf node_modules
sudo npm i
sudo pm2 restart all --update-env
AutoscalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: '1'
MaxSize: '3'
DesiredCapacity: '1'
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumber
VPCZoneIdentifier:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
- !Ref PublicSubnet3
TargetGroupARNs:
- !Ref TargetGroup
Cooldown: 60
Tags:
- Key: Name
Value: NodeAppASG
PropagateAtLaunch: "true"
LoadBalancer:
DependsOn: InternetGatewayAttachment
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: ipv4
Name: AppLoadBalancer
Scheme: internet-facing
Type: application
Subnets:
- !Ref PublicSubnet1
- !Ref PublicSubnet2
- !Ref PublicSubnet3
SecurityGroups:
- !Ref LoadBalancerSecurityGroup
CPUAlarmHigh:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: '1'
Statistic: Average
Threshold: '4'
AlarmDescription: Alarm if CPU utilization more than 4%
Period: '300'
AlarmActions:
- !Ref ScaleOutPolicy
Namespace: AWS/EC2
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref AutoscalingGroup
ComparisonOperator: GreaterThanThreshold
MetricName: CPUUtilization
CPUAlarmLow:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: '1'
Statistic: Average
Threshold: '2'
AlarmDescription: Alarm if CPU utilization is less than 2%
Period: '300'
AlarmActions:
- !Ref ScaleInPolicy
Namespace: AWS/EC2
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref AutoscalingGroup
ComparisonOperator: LessThanThreshold
MetricName: CPUUtilization
ScaleOutPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref AutoscalingGroup
Cooldown: 60
ScalingAdjustment: 1
ScaleInPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref AutoscalingGroup
Cooldown: 60
ScalingAdjustment: -1
TargetGroup:
DependsOn: LoadBalancer
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
HealthCheckPath: /healthz
HealthyThresholdCount: 3
UnhealthyThresholdCount: 5
HealthCheckPort: 3000
HealthCheckProtocol: HTTP
Name: AppLBTargetGroup
Port: 3000
Protocol: HTTP
TargetType: instance
VpcId: !Ref VPC
LoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref TargetGroup
LoadBalancerArn: !Ref LoadBalancer
Port: 443
Protocol: HTTPS
Certificates:
- CertificateArn: arn:aws:acm:us-east-1:485384960986:certificate/7f85a476-51fa-431d-960a-25e31bdc8c6d
DNSRecord:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: !Ref DNSName
Comment: Zone apex alias targeted to LoadBalancer.
RecordSets:
- Name: !Ref DNSName
Type: A
AliasTarget:
HostedZoneId: !GetAtt 'LoadBalancer.CanonicalHostedZoneID'
DNSName: !GetAtt 'LoadBalancer.DNSName'
CodeDeployEC2S3:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: CodeDeploy-EC2-S3
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 's3:PutObject'
- 's3:Get*'
- 's3:GetObjectVersion'
- 's3:DeleteObject'
- 's3:List*'
Resource:
- arn:aws:s3:::assignment7.demo
- arn:aws:s3:::assignment7.demo/*
Roles:
- !Ref IAMRole
GHUploadToS3:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: GH-Upload-To-S3
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 's3:PutObject'
- 's3:Put*'
- 's3:Get*'
- 's3:List*'
- 's3:Delete*'
- 's3:GetObjectVersion'
Resource:
- arn:aws:s3:::assignment7.demo
- arn:aws:s3:::assignment7.demo/*
Users:
- ghactions-app
GHCodeDeploy:
Type: AWS::IAM::Policy
Properties:
PolicyName: GHCodeDeploy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'codedeploy:RegisterApplicationRevision'
- 'codedeploy:GetApplicationRevision'
Resource:
- arn:aws:codedeploy:us-east-1:485384960986:application:csye6225webapp
- Effect: Allow
Action:
- 'codedeploy:CreateDeployment'
- 'codedeploy:GetDeployment'
Resource:
- '*'
- Effect: Allow
Action:
- 'codedeploy:GetDeploymentConfig'
Resource:
- arn:aws:codedeploy:us-east-1:485384960986:deploymentconfig:CodeDeployDefault.OneAtATime
- arn:aws:codedeploy:us-east-1:485384960986:deploymentconfig:CodeDeployDefault.HalfAtATime
- arn:aws:codedeploy:us-east-1:485384960986:deploymentconfig:CodeDeployDefault.AllAtOnce
Users:
- ghactions-app
GHEC2AMI:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: gh-ec2-ami
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ec2:AttachVolume
- ec2:AuthorizeSecurityGroupIngress
- ec2:CopyImage
- ec2:CreateImage
- ec2:CreateKeypair
- ec2:CreateSecurityGroup
- ec2:CreateSnapshot
- ec2:CreateTags
- ec2:CreateVolume
- ec2:DeleteKeyPair
- ec2:DeleteSecurityGroup
- ec2:DeleteSnapshot
- ec2:DeleteVolume
- ec2:DeregisterImage
- ec2:DescribeImageAttribute
- ec2:DescribeImages
- ec2:DescribeInstances
- ec2:DescribeInstanceStatus
- ec2:DescribeRegions
- ec2:DescribeSecurityGroups
- ec2:DescribeSnapshots
- ec2:DescribeSubnets
- ec2:DescribeTags
- ec2:DescribeVolumes
- ec2:DetachVolume
- ec2:GetPasswordData
- ec2:ModifyImageAttribute
- ec2:ModifyInstanceAttribute
- ec2:ModifySnapshotAttribute
- ec2:RegisterImage
- ec2:RunInstances
- ec2:StopInstances
- ec2:TerminateInstances
Resource:
- '*'
Users:
- ghactions-app
CodeDeployServiceRole:
Type: AWS::IAM::Role
Properties:
RoleName: CodeDeployServiceRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- codedeploy.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole
Path: "/"
CodeDeployApplication:
Type: AWS::CodeDeploy::Application
Properties:
ComputePlatform: Server
ApplicationName: csye6225webapp
CodeDeployDeploymentGroup:
Type: AWS::CodeDeploy::DeploymentGroup
DependsOn: CodeDeployApplication
Properties:
DeploymentGroupName: csye6225-webapp-deployment
ApplicationName: !Ref CodeDeployApplication
ServiceRoleArn: !GetAtt CodeDeployServiceRole.Arn
DeploymentStyle:
DeploymentType: IN_PLACE
DeploymentOption: WITHOUT_TRAFFIC_CONTROL
DeploymentConfigName: CodeDeployDefault.AllAtOnce
AutoRollbackConfiguration:
Enabled: 'true'
Events:
- DEPLOYMENT_FAILURE
Ec2TagFilters:
- Key: Name
Value: NodeAppASG
Type: KEY_AND_VALUE
CloudWatchAgentPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: "EC2CloudWatchPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'cloudwatch:PutMetricData'
- 'ec2:DescribeTags'
- 'ec2:DescribeVolumes'
- 'logs:PutLogEvents'
- 'logs:DescribeLogStreams'
- 'logs:DescribeLogGroups'
- 'logs:CreateLogStream'
- 'logs:CreateLogGroup'
Resource: "*"
- Effect: Allow
Action:
- 'ssm:GetParameter'
- 'ssm:PutParameter'
Resource:
- "arn:aws:ssm:*:*:parameter/AmazonCloudWatch-*"
- "arn:aws:iam::aws:policy/CloudWatchAgent*"
Roles:
- Ref: IAMRole
SNSservice:
Type: AWS::SNS::Topic
Properties:
DisplayName: verify_email
Subscription:
- Endpoint: !GetAtt LambdaFunction.Arn
Protocol: lambda
TopicName: verify_email
SNStopicpolicy:
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Version: '2008-10-17'
Id: __default_policy_ID
Statement:
- Sid: __default_statement_ID
Effect: Allow
Principal:
AWS: "*"
Action:
- SNS:Publish
- SNS:DeleteTopic
- SNS:ListSubscriptionsByTopic
- SNS:GetTopicAttributes
- SNS:Receive
- SNS:AddPermission
- SNS:Subscribe
Resource:
Ref: SNSservice
Condition:
StringEquals:
AWS:SourceOwner: !Ref AWS::AccountId
- Sid: __console_pub_0
Effect: Allow
Principal:
AWS: "*"
Action: SNS:Publish
Resource:
Ref: SNSservice
- Sid: __console_sub_0
Effect: Allow
Principal:
AWS: "*"
Action:
- SNS:Subscribe
- SNS:Receive
Resource:
Ref: SNSservice
Topics:
- Ref: SNSservice
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref LambdaFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn:
Ref: SNSservice
LambdaFunction:
Type: AWS::Lambda::Function
Description: Store token in dynamodb and send email to SNS
Properties:
FunctionName: emailVerify
Role:
Fn::Join:
- ''
- - 'arn:aws:iam::'
- !Ref AWS::AccountId
- ":role/"
- !Ref LambdaRole
Runtime: nodejs12.x
Handler: index.handler
Code:
ZipFile: |
console.log('In Lambda function');
exports.handler=function(event,context,callback){
console.log('Message from SNS');
var message = event.Records[0].Sns.Message;
callback(null,"Success");
};
DynamodbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
-
AttributeName: "Email"
AttributeType: "S"
-
AttributeName: "TokenName"
AttributeType: "S"
KeySchema:
-
AttributeName: "Email"
KeyType: "HASH"
-
AttributeName: "TokenName"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: 10
WriteCapacityUnits: 5
TableName: csye6225
TimeToLiveSpecification:
AttributeName: TimeToLive
Enabled: true
Tags:
- Key: key
Value: value
LambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: "LambdaFunctionRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSESFullAccess
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
DynamoDBPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: DynamoPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'dynamodb:List*'
- 'dynamodb:DescribeReservedCapacity*'
- 'dynamodb:DescribeLimits'
- 'dynamodb:DescribeTimeToLive'
Resource:
- '*'
- Effect: Allow
Action:
- 'dynamodb:PutItem'
- 'dynamodb:DescribeTable'
- 'dynamodb:Get*'
- 'dynamodb:Query'
- 'dynamodb:Scan'
- 'dynamodb:Delete*'
- 'dynamodb:CreateTable'
- 'dynamodb:Update*'
- 'dynamodb:BatchWrite*'
- 'dynamodb:DescribeStream'
- 'dynamodb:BatchGet*'
Resource:
- !GetAtt DynamodbTable.Arn
Roles:
- !Ref IAMRole
EBSKmsKey:
Type: AWS::KMS::Key
Properties:
Description: 'KMS Key for EBS'
KeyPolicy:
Id: "kms-key-for-ebs"
Version: '2012-10-17'
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS:
- !Join ["",["arn:aws:iam::",!Ref "AWS::AccountId",":root"],]
- !Join ["",["arn:aws:iam::",!Ref "AWS::AccountId",":role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",],]
Action: kms:*
Resource: '*'
- Sid: Allow access for Key Administrators
Effect: Allow
Principal:
AWS:
- !Join ["",["arn:aws:iam::",!Ref "AWS::AccountId",":role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling",],]
Action:
- kms:Create*
- kms:Describe*
- kms:Enable*
- kms:List*
- kms:Put*
- kms:Update*
- kms:Revoke*
- kms:Disable*
- kms:Get*
- kms:Delete*
- kms:TagResource
- kms:UntagResource
- kms:ScheduleKeyDeletion
- kms:CancelKeyDeletion
Resource: '*'
- Sid: Allow use of the key
Effect: Allow
Principal:
AWS: