-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yml
797 lines (727 loc) · 23.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
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Serverless application with API Gateway, Lambda, and scaling alarms.
Parameters:
paramDomainName:
Type: String
Description: "The domain name for the ACM certificate (e.g., your-custom-domain-name.example.com)"
paramHostedZoneId:
Type: AWS::Route53::HostedZone::Id
Description: "The Route 53 hosted zone ID for the domain name (e.g., Z1ABCDEF123456)"
paramWithFargate:
Type: String
Description: "The Route 53 hosted zone ID for the domain name (e.g., Z1ABCDEF123456)"
Default: false
AllowedValues: [true, false]
Conditions:
condWithFargate: !Equals [!Ref paramWithFargate, "true"]
condWithoutFargate: !Equals [!Ref paramWithFargate, "false"]
Globals:
Function:
Timeout: 10
Resources:
MyLambdaFunction:
Type: "AWS::Serverless::Function"
Description: "Lambda function to scale up the concurrency limit (deploy trigger 1)"
Properties:
PackageType: Image
Policies:
- Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
- cloudformation:DescribeStacks
Resource: "*"
Environment:
Variables:
STACK_NAME: !Ref AWS::StackName
Events:
MyApi:
Type: Api
Properties:
Path: /
Method: ANY
RestApiId: !Ref MyServerlessApi
MyApiProxy:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
RestApiId: !Ref MyServerlessApi
Metadata:
Dockerfile: Dockerfile
DockerContext: ./services/pet-store/src
DockerTag: latest
#
HighConcurrencySNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "High Concurrency Alarm SNS Topic"
TopicName: "HighConcurrencySNSTopic"
LowConcurrencySNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "Low Concurrency Alarm SNS Topic"
TopicName: "LowConcurrencySNSTopic"
# HighConcurrencySNSSubscription:
# Condition: condWithoutFargate
# Type: AWS::SNS::Subscription
# Properties:
# Protocol: "lambda"
# TopicArn: !Ref HighConcurrencySNSTopic
# Endpoint: !GetAtt ScaleUpLambdaFunction.Arn
# LowConcurrencySNSSubscription:
# Condition: condWithFargate
# Type: AWS::SNS::Subscription
# Properties:
# Protocol: "lambda"
# TopicArn: !Ref LowConcurrencySNSTopic
# Endpoint: !GetAtt ScaleDownLambdaFunction.Arn
HighConcurrencyAlarm:
Condition: condWithoutFargate
Type: "AWS::CloudWatch::Alarm"
Properties:
# MetricName: ConcurrentExecutions
MetricName: Invocations
Namespace: "AWS/Lambda"
Statistic: "Sum"
Period: 300
EvaluationPeriods: 1
Threshold: 100
ComparisonOperator: "GreaterThanThreshold"
AlarmDescription: "Alarm when Lambda concurrency exceeds the threshold"
AlarmActions:
- !Ref HighConcurrencySNSTopic
Dimensions:
- Name: FunctionName
Value: !Ref MyLambdaFunction
TreatMissingData: "notBreaching"
ScaleUpLambdaFunction:
Type: "AWS::Serverless::Function"
Description: "Lambda function to scale up the concurrency limit (deploy trigger 2)"
Properties:
Timeout: 900
CodeUri: infra/src
Handler: scale.lambda_scale_up_handler
Runtime: python3.11
Environment:
Variables:
LAMBDA_FUNCTION_NAME: !GetAtt MyLambdaFunction.Arn
STACK_NAME: !Ref AWS::StackName
Policies:
- Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
# - cloudformation:CreateStack
# - cloudformation:DeleteStack
# - cloudformation:DescribeStacks
# - cloudformation:UpdateStack
# - ec2:CreateInternetGateway
# - ec2:CreateVpc
# - ec2:DescribeInternetGateways
# - ec2:DescribeVpcs
# - ec2:ModifyVpcAttribute
# - ecs:CreateCluster
# - ecs:DeleteCluster
# - ecs:DescribeClusters
# - ec2:DeleteInternetGateway
# - ec2:DeleteVpc
# - iam:CreateRole
# - iam:DeleteRole
# - iam:DeleteRolePolicy
# - iam:PutRolePolicy
# - lambda:GetAlias
# - lambda:GetFunction
# - lambda:ListVersionsByFunction
# - ec2:CreateSecurityGroup
# - ec2:DescribeAvailabilityZones
# - elasticloadbalancing:DescribeTargetGroup
- cloudformation:*
- ec2:*
- ecs:*
- iam:*
- lambda:*
- elasticloadbalancing:*
- route53:GetHostedZone
- route53:ChangeResourceRecordSets
- route53:GetChange
- route53:*
- cloudwatch:*
Resource: "*"
Events:
alarm:
Type: SNS
Properties:
Topic: !Ref HighConcurrencySNSTopic
# Events:
# HighConcurrencyTrigger:
# Type: CloudWatchEvent
# Properties:
# Pattern:
# source:
# - "aws.cloudwatch"
# detail-type:
# - "CloudWatch Alarm State Change"
# detail:
# state:
# value:
# - "ALARM"
# alarmName:
# - Ref: HighConcurrencyAlarm
LowConcurrencyAlarm:
Condition: condWithFargate
Type: "AWS::CloudWatch::Alarm"
Properties:
# MetricName: ConcurrentExecutions
MetricName: Invocations
Namespace: "AWS/Lambda"
# https://marbot.io/blog/reduce-cloudwatch-alarms-by-combining-metrics-to-reduce-costs-or-improve-auto-scaling.html
Statistic: "Sum"
Period: 7200
EvaluationPeriods: 1
Threshold: 20
ComparisonOperator: "LessThanThreshold"
AlarmDescription: "Alarm when Lambda concurrency drops below the threshold"
AlarmActions:
- !Ref LowConcurrencySNSTopic
Dimensions:
- Name: FunctionName
Value: !Ref MyLambdaFunction
TreatMissingData: "breaching"
ScaleDownLambdaFunction:
Type: "AWS::Serverless::Function"
Description: "Lambda function to scale down the concurrency limit (deploy trigger 2)"
Properties:
Timeout: 900
CodeUri: infra/src
Handler: scale.lambda_scale_down_handler
Runtime: python3.11
Environment:
Variables:
LAMBDA_FUNCTION_NAME: !GetAtt MyLambdaFunction.Arn
STACK_NAME: !Ref AWS::StackName
Policies:
- Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
# - cloudformation:CreateStack
# - cloudformation:DeleteStack
# - cloudformation:DescribeStacks
# - cloudformation:UpdateStack
# - ec2:CreateInternetGateway
# - ec2:CreateVpc
# - ec2:DescribeInternetGateways
# - ec2:DescribeVpcs
# - ec2:ModifyVpcAttribute
# - ecs:CreateCluster
# - ecs:DeleteCluster
# - ecs:DescribeClusters
# - ec2:DeleteInternetGateway
# - ec2:DeleteVpc
# - iam:CreateRole
# - iam:DeleteRole
# - iam:DeleteRolePolicy
# - iam:PutRolePolicy
# - lambda:GetAlias
# - lambda:GetFunction
# - lambda:ListVersionsByFunction
# - ec2:CreateSecurityGroup
# - ec2:DescribeAvailabilityZones
# - elasticloadbalancing:DescribeTargetGroup
- cloudformation:*
- ec2:*
- ecs:*
- iam:*
- lambda:*
- elasticloadbalancing:*
- route53:GetHostedZone
- route53:ChangeResourceRecordSets
- route53:GetChange
- route53:*
- apigateway:GET
- cloudwatch:*
Resource: "*"
Events:
alarm:
Type: SNS
Properties:
Topic: !Ref LowConcurrencySNSTopic
# Events:
# LowConcurrencyTrigger:
# Type: CloudWatchEvent
# Properties:
# Pattern:
# source:
# - "aws.cloudwatch"
# detail-type:
# - "CloudWatch Alarm State Change"
# detail:
# state:
# value:
# - "ALARM"
# alarmName:
# - Ref: LowConcurrencyAlarm
MyServerlessApi:
Type: "AWS::Serverless::Api"
Properties:
# Domain:
# DomainName: !Ref paramDomainName
# CertificateArn: !Ref AcmCertificate
# # Route53:
# # HostedZoneId: !Ref paramHostedZoneId
# # SetIdentifier: rd_01
EndpointConfiguration: REGIONAL
StageName: prod
AcmCertificate:
Type: "AWS::CertificateManager::Certificate"
Properties:
DomainName: !Ref paramDomainName
ValidationMethod: DNS
MyRoute53RecordSetApiGateway:
Condition: condWithoutFargate
Type: AWS::Route53::RecordSet
Properties:
Name: !Ref paramDomainName
HostedZoneId: !Ref paramHostedZoneId
Type: A
SetIdentifier: rd_01
Weight: 1
AliasTarget:
DNSName: !GetAtt DeliveryApiDomainName.RegionalDomainName
HostedZoneId: !GetAtt DeliveryApiDomainName.RegionalHostedZoneId
EvaluateTargetHealth: false
# # Use the fixed Hosted Zone ID for API Gateway regional endpoints provided by AWS for your specific region.
# # For example, this is the Hosted Zone ID for API Gateway in us-east-1 region.
# HostedZoneId: Z1UJRXOUMOOFQ8
# # The regional domain name attribute you need to use here is DistributionDomainName due to the API Gateway being created as an AWS::Serverless::Api.
# DNSName: !Sub ${MyServerlessApi.RootResourceId}.execute-api.${AWS::Region}.amazonaws.com
# DeliveryApi:
# Type: AWS::ApiGateway::RestApi
# Properties:
# EndpointConfiguration:
# Types:
# - REGIONAL
DeliveryApiDomainName:
Type: "AWS::ApiGateway::DomainName"
Properties:
RegionalCertificateArn: !Ref AcmCertificate
DomainName: !Ref paramDomainName
EndpointConfiguration:
Types:
- REGIONAL
DeliveryApiBasePathMapping:
Type: "AWS::ApiGateway::BasePathMapping"
Properties:
DomainName: !Ref DeliveryApiDomainName
RestApiId: !Ref MyServerlessApi
Stage: !Ref MyServerlessApi.Stage
# DeliveryApiRoute53RecordSetGroup:
# Type: AWS::Route53::RecordSetGroup
# Properties:
# HostedZoneId: !Ref paramHostedZoneId
# RecordSets:
# - Name: !Ref paramDomainName # Your chosen subdomain
# Type: A
# AliasTarget:
# HostedZoneId: !GetAtt DeliveryApiDomainName.DistributionHostedZoneId
# DNSName: !GetAtt DeliveryApiDomainName.DistributionDomainName
# - Name: !Ref paramDomainName
# Type: AAAA
# AliasTarget:
# HostedZoneId: !GetAtt DeliveryApiDomainName.DistributionHostedZoneId
# DNSName: !GetAtt DeliveryApiDomainName.DistributionDomainName
# Network resources
MyVPC:
Condition: condWithFargate
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: "10.0.0.0/16"
EnableDnsSupport: true
EnableDnsHostnames: true
# public section
MyInternetGateway:
Condition: condWithFargate
Type: "AWS::EC2::InternetGateway"
GatewayAttachment:
Condition: condWithFargate
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
VpcId: !Ref MyVPC
InternetGatewayId: !Ref MyInternetGateway
PublicSubnetOne:
Condition: condWithFargate
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref MyVPC
CidrBlock: "10.0.1.0/24"
AvailabilityZone: !Select [0, !GetAZs ""]
MapPublicIpOnLaunch: true
PublicSubnetTwo:
Condition: condWithFargate
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref MyVPC
CidrBlock: "10.0.2.0/24"
AvailabilityZone: !Select [1, !GetAZs ""]
MapPublicIpOnLaunch: true
PublicRouteTable:
Condition: condWithFargate
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref MyVPC
PublicRoute:
Condition: condWithFargate
Type: "AWS::EC2::Route"
DependsOn: GatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: "0.0.0.0/0"
GatewayId: !Ref MyInternetGateway
PublicSubnetOneRouteTableAssociation:
Condition: condWithFargate
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PublicSubnetOne
RouteTableId: !Ref PublicRouteTable
PublicSubnetTwoRouteTableAssociation:
Condition: condWithFargate
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
SubnetId: !Ref PublicSubnetTwo
RouteTableId: !Ref PublicRouteTable
# private section
# # NAT Gateway EIP
# NATGatewayEIP:
# Type: 'AWS::EC2::EIP'
# DependsOn: GatewayAttachment
# Properties:
# Domain: vpc
# # NAT Gateway
# NATGateway:
# Type: 'AWS::EC2::NatGateway'
# Properties:
# AllocationId: !GetAtt NATGatewayEIP.AllocationId
# SubnetId: !Ref PublicSubnetOne
# PrivateSubnetOne:
# Type: 'AWS::EC2::Subnet'
# Properties:
# VpcId: !Ref MyVPC
# CidrBlock: '10.0.3.0/24'
# AvailabilityZone: !Select [0, !GetAZs '']
# PrivateSubnetTwo:
# Type: 'AWS::EC2::Subnet'
# Properties:
# VpcId: !Ref MyVPC
# CidrBlock: '10.0.4.0/24'
# AvailabilityZone: !Select [1, !GetAZs '']
# PrivateRouteTable:
# Type: 'AWS::EC2::RouteTable'
# Properties:
# VpcId: !Ref MyVPC
# PrivateRoute:
# Type: 'AWS::EC2::Route'
# Properties:
# RouteTableId: !Ref PrivateRouteTable
# DestinationCidrBlock: '0.0.0.0/0'
# NatGatewayId: !Ref NATGateway
# PrivateSubnetOneRouteTableAssociation:
# Type: 'AWS::EC2::SubnetRouteTableAssociation'
# Properties:
# SubnetId: !Ref PrivateSubnetOne
# RouteTableId: !Ref PrivateRouteTable
# PrivateSubnetTwoRouteTableAssociation:
# Type: 'AWS::EC2::SubnetRouteTableAssociation'
# Properties:
# SubnetId: !Ref PrivateSubnetTwo
# RouteTableId: !Ref PrivateRouteTable
# ECS resources
ECSCluster:
Condition: condWithFargate
Type: "AWS::ECS::Cluster"
# IAM Roles for ECS
ECSExecutionRole:
Condition: condWithFargate
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: "ECSTaskExecutionPolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "ecr:GetAuthorizationToken"
- "ecr:BatchCheckLayerAvailability"
- "ecr:GetDownloadUrlForLayer"
- "ecr:BatchGetImage"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "*"
ECSTaskRole:
Condition: condWithFargate
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: "ECSTaskRolePolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:UpdateItem"
- "dynamodb:PutItem"
Resource:
"arn:aws:dynamodb:your-region:your-account-id:table/YourDynamoDBTable"
# Add any additional permissions needed by the tasks
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: LambdaExecutionPolicy1
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action: "logs:*"
Resource: "arn:aws:logs:*:*:*"
- PolicyName: LambdaExecutionPolicy2
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action: "lambda:*"
Resource: "*"
ImageUriExtractorLambda:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
import boto3
import cfnresponse
import json
def lambda_handler_inner(event, context):
try:
lambda_client = boto3.client('lambda')
function_name = event['ResourceProperties']['LambdaArn']
response = lambda_client.get_function(FunctionName=function_name)
image_uri = response['Code']['ImageUri']
responseData = {'ImageUri': image_uri}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
except Exception as e:
errorMessage = str(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {}, errorMessage)
def lambda_handler(event, context):
print("event: " + json.dumps(event))
print("context: " + str(context))
result = lambda_handler_inner(event, context)
print("result: " + str(result))
return result
Handler: "index.lambda_handler"
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: python3.11
Timeout: 30
LambdaImageUriCustomResource:
Type: "Custom::LambdaImageUriExtractor"
Properties:
ServiceToken: !GetAtt ImageUriExtractorLambda.Arn
LambdaArn: !Ref MyLambdaFunction
FargateTaskDefinition:
Condition: condWithFargate
Type: "AWS::ECS::TaskDefinition"
Properties:
Family: "fastapitask"
Cpu: "256"
Memory: "512"
NetworkMode: "awsvpc"
RequiresCompatibilities:
- "FARGATE"
ExecutionRoleArn: !GetAtt ECSExecutionRole.Arn
TaskRoleArn: !GetAtt ECSTaskRole.Arn
ContainerDefinitions:
- Name: "fastapic1"
Environment:
- Name: "AWS_STACK_NAME"
Value: !Ref AWS::StackName
Image: !GetAtt LambdaImageUriCustomResource.ImageUri
PortMappings:
-
ContainerPort: 8080
HostPort: 8080
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-group: /ecs/MyLogGroup
awslogs-region: us-east-1 # adjust the region if needed
awslogs-stream-prefix: MyContainer
# Security group for the Fargate tasks and ALB
FargateSecurityGroup:
Condition: condWithFargate
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Security group for Fargate tasks"
VpcId: !Ref MyVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
FargateService:
Condition: condWithFargate
Type: "AWS::ECS::Service"
Properties:
Cluster: !Ref ECSCluster
TaskDefinition: !Ref FargateTaskDefinition
LaunchType: "FARGATE"
DesiredCount: 1
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
- !Ref PublicSubnetOne
- !Ref PublicSubnetTwo
SecurityGroups:
- !Ref FargateSecurityGroup
LoadBalancers:
- ContainerName: "fastapic1"
ContainerPort: 8080
# LoadBalancerName: fastapi-alb
TargetGroupArn: !GetAtt ALBTargetGroup.TargetGroupArn
DependsOn:
- ALB
# - ALBListener80
- ALBListener443
# ALB section
# Security Group for the ALB
ALBSecurityGroup:
Condition: condWithFargate
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Access to the ALB"
VpcId: !Ref MyVPC
SecurityGroupIngress:
- CidrIp: "0.0.0.0/0"
IpProtocol: "-1"
# Load balancer resources
ALB:
Condition: condWithFargate
Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
Properties:
Name: anyscale0-alb
Scheme: "internet-facing"
Subnets:
- !Ref PublicSubnetOne
- !Ref PublicSubnetTwo
SecurityGroups:
- !Ref FargateSecurityGroup
Type: "application"
DependsOn:
- MyInternetGateway
# ALBListener80:
# Condition: condWithFargate
# Type: "AWS::ElasticLoadBalancingV2::Listener"
# Properties:
# DefaultActions:
# - Type: "forward"
# TargetGroupArn: !GetAtt ALBTargetGroup.TargetGroupArn
# LoadBalancerArn: !Ref ALB
# Port: "80"
# Protocol: "HTTP"
ALBListener443:
Condition: condWithFargate
Type: "AWS::ElasticLoadBalancingV2::Listener"
Properties:
Certificates:
- CertificateArn: !Ref AcmCertificate
DefaultActions:
- Type: "forward"
TargetGroupArn: !GetAtt ALBTargetGroup.TargetGroupArn
LoadBalancerArn: !Ref ALB
Port: "443"
Protocol: "HTTPS"
ALBTargetGroup:
Condition: condWithFargate
Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
Properties:
Port: 8080
Protocol: "HTTP"
VpcId: !Ref MyVPC
Matcher:
HttpCode: "200-499"
TargetType: ip
MyRoute53RecordSetFargate:
Condition: condWithFargate
Type: AWS::Route53::RecordSet
Properties:
Name: !Ref paramDomainName
HostedZoneId: !Ref paramHostedZoneId
Type: A
SetIdentifier: rd_02
Weight: 200
AliasTarget:
DNSName: !GetAtt ALB.DNSName
HostedZoneId: !GetAtt ALB.CanonicalHostedZoneID
EvaluateTargetHealth: true
HealthCheckId: !Ref MyHealthCheck
MyHealthCheck:
Condition: condWithFargate
Type: AWS::Route53::HealthCheck
Properties:
HealthCheckConfig:
Type: HTTPS # Use HTTP or HTTPS based on your application protocol
ResourcePath: / # Adjust the path as needed
FullyQualifiedDomainName: !GetAtt ALB.DNSName
Port: 443 # Adjust the port based on your application configuration
RequestInterval: 30 # Adjust the interval based on your application needs
FailureThreshold: 3 # Adjust the threshold based on your application needs
Outputs:
# WebEndpoint:
# Description: "API Gateway endpoint URL for Prod stage"
# Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
# MyLambdaFunctionConcurrentExecutionMetric:
# Description: "The CloudWatch metric for concurrent Lambda executions"
# Value: !GetAtt HighConcurrencyAlarm.MetricName
# add output of the route 53 record
domainName:
Description: "Route 53 record set for the API Gateway"
Value: !Ref paramDomainName