forked from jcjorel/clonesquad-ec2-pet-autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
778 lines (724 loc) · 28.9 KB
/
template.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CloneSquad deployment
Globals:
Function:
Timeout: !Ref OperationTimeout
Parameters:
GroupName:
Type: String
ConstraintDescription: "[A-Za-z0-9][-A-Za-z0-9]+"
MaxLength: 40
MinLength: 2
Description: "CloneSquad group name"
CustomizationZipParameters:
Type: String
Default: ""
Description: "(Optional) Path to a ZIP file located in S3 expressed with special format '<S3_bucket_name>:<S3_key_path>'"
ConfigurationURL:
Type: String
ConstraintDescription: "[?-/:A-Za-z0-9]*"
Default: ""
Description: "(Optional) URL where to read configuration"
LambdaMemorySize:
Type: Number
Default: 512
Description: "(Optional) Memory size to allocate to Main and Interact Lambda functions"
LogRetentionDuration:
Type: Number
Default: 14
Description: "(Optional) Cloudwatch Logs retention duration"
UserNotificationArns:
Type: String
ConstraintDescription: "[-/:A-Za-z0-9]*"
Default: ""
Description: "(Optional) Comma seperated list of user provided ARNs (Lambda, SQS, SNS) to receive real-time notifications (TargetGroup Register/Deregister events, instances start/stop...)"
LoggingS3Path:
Type: String
Default: ""
Description: "(Optional) S3 Path for detailed logging (s3://<bucket_name>/<path>)"
OperationTimeout:
Type: String
ConstraintDescription: "[0-9]+"
Default: 300
Description: "(Optional) General timeout for operations"
VariantNumber:
Type: String
ConstraintDescription: "[0-9]*"
Default: ""
Description: "(Debug) Used to avoid CloudFormation clashes when updating some resources (ex: DynamoDB Tables)"
XRayDiagnosis:
Type: String
Default: 1
ConstraintDescription: "[0|1]"
Description: "(Debug) Trace CloneSquad to get visibility into execution time and associated costs"
Conditions:
HasCustomizationZipParameters: !Not [ !Equals [ !Ref CustomizationZipParameters, ""]]
Resources:
MainQueue:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 120
QueueName: !Sub "CloneSquad-Main-${GroupName}"
ReceiveMessageWaitTimeSeconds: 10
VisibilityTimeout: !Ref OperationTimeout
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
MainSQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref MainQueue
PolicyDocument:
Statement:
- Sid: "CloneSquad-Service-SendMessage-SQS-policy"
Action:
- "sqs:SendMessage"
Effect: "Allow"
Resource: !GetAtt MainQueue.Arn
Principal:
Service:
- events.amazonaws.com
- ec2.amazonaws.com
- Sid: "CloneSquad-SendMessage-SQS-policy"
Action:
- "sqs:SendMessage"
Effect: "Allow"
Resource: !GetAtt MainQueue.Arn
Condition:
ArnLike:
aws:SourceArn:
- !GetAtt MainFunction.Arn
- !GetAtt SNSMgrFunction.Arn
- Sid: "CloneSquad-Main-SQS-policy"
Action:
- "sqs:*"
Effect: "Allow"
Resource: !GetAtt MainQueue.Arn
Condition:
ArnLike:
aws:SourceArn:
- !GetAtt MainFunction.Arn
ConfigTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-Configuration"
AttributeDefinitions:
-
AttributeName: "Key"
AttributeType: "S"
KeySchema:
-
AttributeName: "Key"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
StreamSpecification:
StreamViewType: NEW_IMAGE
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
AlarmStateEC2Table:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-AlarmState-EC2"
AttributeDefinitions:
-
AttributeName: "AlarmName"
AttributeType: "S"
KeySchema:
-
AttributeName: "AlarmName"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
StateTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-State"
AttributeDefinitions:
-
AttributeName: "Key"
AttributeType: "S"
KeySchema:
-
AttributeName: "Key"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
EventTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-EventLog"
AttributeDefinitions:
-
AttributeName: "EventDate"
AttributeType: "S"
KeySchema:
-
AttributeName: "EventDate"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
LongTermEventTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-EventLog-LongTerm"
AttributeDefinitions:
-
AttributeName: "EventDate"
AttributeType: "S"
KeySchema:
-
AttributeName: "EventDate"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
SchedulerTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "CloneSquad-${GroupName}${VariantNumber}-Scheduler"
AttributeDefinitions:
-
AttributeName: "Key"
AttributeType: "S"
KeySchema:
-
AttributeName: "Key"
KeyType: "HASH"
TimeToLiveSpecification:
AttributeName: ExpirationTime
Enabled: True
BillingMode: "PAY_PER_REQUEST"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
EC2EventRule:
Type: 'AWS::Events::Rule'
Properties:
Name: !Sub "CloneSquad-EC2-Notif-${GroupName}"
Description: >-
CloudWatch Event Rule that detects changes to EC2 Instances and
publishes change events to the SQS queue for notification.
State: ENABLED
Targets:
- Arn: !GetAtt MainQueue.Arn
Id: !GetAtt MainQueue.QueueName
EventPattern:
detail-type:
- AWS API Call via CloudTrail
detail:
eventSource:
- ec2.amazonaws.com
eventName:
- RunInstances
- RebootInstances
- StartInstances
- StopInstances
- TerminateInstances
EC2SpotEventRule:
Type: AWS::Events::Rule
Properties:
Description: Events rule for Spot Instance Interruption Notices
Name: !Sub "CloneSquad-EC2Spot-Notif-${GroupName}"
Targets:
- Arn: !GetAtt MainQueue.Arn
Id: !GetAtt MainQueue.QueueName
EventPattern:
detail-type:
- EC2 Spot Instance Interruption Warning
source:
- aws.ec2
State: ENABLED
CloudWatchEventRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub "CloneSquad-${GroupName}-CWRole"
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action:
- 'sts:AssumeRole'
ERRORMetricFilter:
Type: AWS::Logs::MetricFilter
DependsOn: MainFunctionLogGroup
Properties:
FilterPattern: '"[ERROR]"'
LogGroupName: !Sub "/aws/lambda/CloneSquad-Main-${GroupName}"
MetricTransformations:
- DefaultValue: 0
MetricValue: 1
MetricName: !Sub "InternalErrors.Main.${GroupName}"
MetricNamespace: CloneSquad
ERRORInteractMetricFilter:
Type: AWS::Logs::MetricFilter
DependsOn: InteractFunctionLogGroup
Properties:
FilterPattern: '"[ERROR]"'
LogGroupName: !Sub "/aws/lambda/CloneSquad-Interact-${GroupName}"
MetricTransformations:
- DefaultValue: 0
MetricValue: 1
MetricName: !Sub "InternalErrors.Interact.${GroupName}"
MetricNamespace: CloneSquad
WARNINGMetricFilter:
Type: AWS::Logs::MetricFilter
DependsOn: MainFunctionLogGroup
Properties:
FilterPattern: '"[WARNING]"'
LogGroupName: !Sub "/aws/lambda/CloneSquad-Main-${GroupName}"
MetricTransformations:
- DefaultValue: 0
MetricValue: 1
MetricName: !Sub "InternalWarnings.Main.${GroupName}"
MetricNamespace: CloneSquad
WARNINGInteractMetricFilter:
Type: AWS::Logs::MetricFilter
DependsOn: InteractFunctionLogGroup
Properties:
FilterPattern: '"[WARNING]"'
LogGroupName: !Sub "/aws/lambda/CloneSquad-Interact-${GroupName}"
MetricTransformations:
- DefaultValue: 0
MetricValue: 1
MetricName: !Sub "InternalWarnings.Interact.${GroupName}"
MetricNamespace: CloneSquad
InternalERRORMainAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: "Alarm to catch CloudSquad Internal errors (ex: Misconfiguration, bugs...) - MAIN function"
AlarmName: !Sub "CloneSquad-${GroupName}-InternalERRORs.Main"
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 1
EvaluationPeriods: 2
MetricName: !Sub "InternalErrors.Main.${GroupName}"
Namespace: CloneSquad
Period: 60
Statistic: Sum
Threshold: 0
InternalWARNINGMainAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: "Alarm to catch CloudSquad Internal warnings (ex: Misconfiguration, bugs...) - MAIN function"
AlarmName: !Sub "CloneSquad-${GroupName}-InternalWARNINGs.Main"
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 1
EvaluationPeriods: 2
MetricName: !Sub "InternalWarnings.Main.${GroupName}"
Namespace: CloneSquad
Period: 60
Statistic: Sum
Threshold: 0
InternalERRORInteractAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: "Alarm to catch CloudSquad Internal errors (ex: Misconfiguration, bugs...) - INTERACT function"
AlarmName: !Sub "CloneSquad-${GroupName}-InternalERRORs.Interact"
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 1
EvaluationPeriods: 2
MetricName: !Sub "InternalErrors.Interact.${GroupName}"
Namespace: CloneSquad
Period: 60
Statistic: Sum
Threshold: 0
InternalWARNINGInteractAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: "Alarm to catch CloudSquad Internal warnings (ex: Misconfiguration, bugs...) - INTERACT function"
AlarmName: !Sub "CloneSquad-${GroupName}-InternalWARNINGs.Interact"
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 1
EvaluationPeriods: 2
MetricName: !Sub "InternalWarnings.Interact.${GroupName}"
Namespace: CloneSquad
Period: 60
Statistic: Sum
Threshold: 0
LackOfCPUCreditAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ActionsEnabled: True
AlarmDescription: "Alarm to catch burstable instance that exhausted their CPU credits"
AlarmName: !Sub "CloneSquad-${GroupName}-LackOfCPUCreditAlarm"
ComparisonOperator: GreaterThanThreshold
DatapointsToAlarm: 2
EvaluationPeriods: 2
MetricName: "NbOfCPUCreditExhaustedInstances"
Dimensions:
- Name: GroupName
Value: !Sub "${GroupName}"
Namespace: CloneSquad
Period: 60
Statistic: Average
Threshold: 0
SNSScaleUp:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub "CloneSquad-CloudWatchAlarm-ScaleUp-${GroupName}"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
SNSOk:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub "CloneSquad-CloudWatchAlarm-Ok-${GroupName}"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
SNSInsufficientData:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub "CloneSquad-CloudWatchAlarm-InsufficientData-${GroupName}"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
CloneSquadDependencyLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub "CloneSquad-Dependencies-Layer-${GroupName}"
Description: "Binary dependency artifact for 'Main' CloneSquad layer"
ContentUri: .venv/lib/python3.7/
CompatibleRuntimes:
- python3.7
CloneSquadLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub "CloneSquad-Layer-${GroupName}"
Description: "Binary artifact for CloneSquad functions"
ContentUri: src/
CompatibleRuntimes:
- python3.7
LicenseInfo: 'MIT'
MainFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
DependsOn: MainFunction
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref "MainFunction"]]
RetentionInDays: !Ref LogRetentionDuration
InteractFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
DependsOn: InteractFunction
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref "InteractFunction"]]
RetentionInDays: !Ref LogRetentionDuration
SNSMgrFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
DependsOn: SNSMgrFunction
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref "SNSMgrFunction"]]
RetentionInDays: !Ref LogRetentionDuration
SNSDiscoveryFunctionLogGroup:
Type: "AWS::Logs::LogGroup"
DependsOn: DiscoveryFunction
Properties:
LogGroupName: !Join ["", ["/aws/lambda/", !Ref "DiscoveryFunction"]]
RetentionInDays: !Ref LogRetentionDuration
InteractApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
Name: !Sub "CloneSquad-${GroupName}"
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
MainFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Handler: app.main_handler
Runtime: python3.7
CodeUri: custo/
Layers:
- !Ref CloneSquadLayer
- !Ref CloneSquadDependencyLayer
FunctionName: !Sub "CloneSquad-Main-${GroupName}"
MemorySize: !Ref LambdaMemorySize
Timeout: !Ref OperationTimeout
ReservedConcurrentExecutions: 1
Tracing: Active
Policies:
- AdministratorAccess
Environment:
Variables:
PYTHONPATH: "/opt:/opt/site-packages"
ApplicationName: "CloneSquad"
MainSQSQueue: !Ref MainQueue
UserNotificationArns: !Ref UserNotificationArns
InteractLambdaArn: !GetAtt InteractFunction.Arn
InteractSQSUrl: !Ref InteractQueue
GroupName: !Ref GroupName
GenericOkActions_SNSTopicArn: !Ref SNSOk
ScaleUp_SNSTopicArn: !Ref SNSScaleUp
GenericInsufficientDataActions_SNSTopicArn: !Ref SNSInsufficientData
CloudWatchEventRoleArn: !GetAtt CloudWatchEventRole.Arn
ConfigurationTable: !Ref ConfigTable
AlarmStateEC2Table: !Ref AlarmStateEC2Table
StateTable: !Ref StateTable
EventTable: !Ref EventTable
SchedulerTable: !Ref SchedulerTable
LongTermEventTable: !Ref LongTermEventTable
VariantNumber: !Ref VariantNumber
ConfigurationURL: !Ref ConfigurationURL
LoggingS3Path: !Ref LoggingS3Path
AWS_XRAY_SDK_ENABLED: !Ref XRayDiagnosis
Events:
Schedule:
Type: Schedule
Properties:
Description: "Schedule event for CloneSquad HeartBeat"
Name: !Sub "CS-hb-rule-${GroupName}"
Enabled: True
Schedule: "rate(1 minute)"
SQS:
Type: SQS
Properties:
Queue: !GetAtt MainQueue.Arn
BatchSize: 10
PermissionForSchedulerEvents:
Type: AWS::Lambda::Permission
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref MainFunction
Principal: "events.amazonaws.com"
SourceArn: !Sub "arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:rule/CS-hb-rule-${GroupName}-*"
SNSMgrFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Handler: app.sns_handler
Runtime: python3.7
CodeUri: custo/
Layers:
- !Ref CloneSquadLayer
- !Ref CloneSquadDependencyLayer
FunctionName: !Sub "CloneSquad-SNSMgr-${GroupName}"
MemorySize: 256
Timeout: 60
Policies:
- AdministratorAccess
Environment:
Variables:
PYTHONPATH: "/opt:/opt/site-packages"
ApplicationName: "CloneSquad"
MainSQSQueue: !Ref MainQueue
GroupName: !Ref GroupName
GenericOkActions_SNSTopicArn: !Ref SNSOk
ScaleUp_SNSTopicArn: !Ref SNSScaleUp
GenericInsufficientDataActions_SNSTopicArn: !Ref SNSInsufficientData
ConfigurationTable: !Ref ConfigTable
AlarmStateEC2Table: !Ref AlarmStateEC2Table
StateTable: !Ref StateTable
EventTable: !Ref EventTable
VariantNumber: !Ref VariantNumber
ConfigurationURL: !Ref ConfigurationURL
Events:
ScaleUpTopic:
Type: SNS
Properties:
Topic: !Ref SNSScaleUp
OkTopic:
Type: SNS
Properties:
Topic: !Ref SNSOk
InsufficientDataTopic:
Type: SNS
Properties:
Topic: !Ref SNSInsufficientData
ConfigStream:
Type: DynamoDB
Properties:
Stream: !GetAtt ConfigTable.StreamArn
BatchSize: 100
StartingPosition: TRIM_HORIZON
DiscoveryFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.discovery_handler
Runtime: python3.7
CodeUri: custo/
Layers:
- !Ref CloneSquadLayer
- !Ref CloneSquadDependencyLayer
FunctionName: !Sub "CloneSquad-Discovery-${GroupName}"
MemorySize: 128
Environment:
Variables:
PYTHONPATH: "/opt:/opt/site-packages"
ApplicationName: "CloneSquad"
GroupName: !Ref GroupName
GenericOkActions_SNSTopicArn: !Ref SNSOk
ScaleUp_SNSTopicArn: !Ref SNSScaleUp
GenericInsufficientDataActions_SNSTopicArn: !Ref SNSInsufficientData
ConfigurationTable: !Ref ConfigTable
AlarmStateEC2Table: !Ref AlarmStateEC2Table
StateTable: !Ref StateTable
EventTable: !Ref EventTable
LongTermEventTable: !Ref LongTermEventTable
SchedulerTable: !Ref SchedulerTable
VariantNumber: !Ref VariantNumber
ConfigurationURL: !Ref ConfigurationURL
LoggingS3Path: !Ref LoggingS3Path
InteractQueue: !Ref InteractQueue
InteractAPIGWUrl: !Sub "https://${InteractApi}.execute-api.${AWS::Region}.amazonaws.com/v1/"
MainFunctionArn: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:CloneSquad-Main-${GroupName}"
InteractFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Handler: app.interact_handler
Runtime: python3.7
CodeUri: custo/
Layers:
- !Ref CloneSquadLayer
- !Ref CloneSquadDependencyLayer
FunctionName: !Sub "CloneSquad-Interact-${GroupName}"
MemorySize: !Ref LambdaMemorySize
Timeout: 900
Policies:
- AdministratorAccess
Environment:
Variables:
PYTHONPATH: "/opt:/opt/site-packages"
ApplicationName: "CloneSquad"
GroupName: !Ref GroupName
GenericOkActions_SNSTopicArn: !Ref SNSOk
ScaleUp_SNSTopicArn: !Ref SNSScaleUp
GenericInsufficientDataActions_SNSTopicArn: !Ref SNSInsufficientData
ConfigurationTable: !Ref ConfigTable
AlarmStateEC2Table: !Ref AlarmStateEC2Table
StateTable: !Ref StateTable
EventTable: !Ref EventTable
LongTermEventTable: !Ref LongTermEventTable
SchedulerTable: !Ref SchedulerTable
VariantNumber: !Ref VariantNumber
ConfigurationURL: !Ref ConfigurationURL
LoggingS3Path: !Ref LoggingS3Path
InteractQueue: !Ref InteractQueue
MainFunctionArn: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:CloneSquad-Main-${GroupName}"
Events:
SQS:
Type: SQS
Properties:
Queue: !GetAtt InteractQueue.Arn
BatchSize: 10
ApiEventCommand:
Type: Api
Properties:
RestApiId: !Ref InteractApi
Path: "{proxy+}"
Method: get
InteractQueue:
Type: AWS::SQS::Queue
Properties:
MessageRetentionPeriod: 3600
QueueName: !Sub "CloneSquad-Interact-${GroupName}"
ReceiveMessageWaitTimeSeconds: 20
VisibilityTimeout: 900
Tags:
- Key: "clonesquad:group-name"
Value: !Ref GroupName
InteractQueueSQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref InteractQueue
PolicyDocument:
Statement:
- Sid: "CloneSquad-User-Interact-SQS-policy"
Action:
- "SQS:SendMessage"
Effect: "Allow"
Resource: !GetAtt InteractQueue.Arn
Condition:
ArnLike:
aws:SourceArn: !Split [",", !Sub "${UserNotificationArns}" ]
- Sid: "CloneSquad-Interact-SQS-policy"
Action:
- "sqs:*"
Effect: "Allow"
Resource: !GetAtt InteractQueue.Arn
Condition:
ArnLike:
aws:SourceArn: !GetAtt InteractFunction.Arn
PermissionForSchedulerEvents:
Type: AWS::Lambda::Permission
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref InteractFunction
Principal: "events.amazonaws.com"
SourceArn: !Sub "arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:rule/CS-Cron-${GroupName}-*"
Outputs:
MainFunction:
Description: "MainFunction Lambda Function ARN"
Value: !GetAtt MainFunction.Arn
InteractFunction:
Description: "InteractFunction Lambda Function ARN"
Value: !GetAtt InteractFunction.Arn
CloneSquadLayer:
Description: "CloneSquad Lambda layer (available for customization)"
Value: !Ref CloneSquadLayer
CloneSquadDependencyLayer:
Description: "CloneSquad Lambda layer (dependencies)"
Value: !Ref CloneSquadDependencyLayer
MainFunctionIamRole:
Description: "Implicit IAM Role created for MainRole function"
Value: !GetAtt MainFunctionRole.Arn
InternalERRORMainAlarm:
Description: "Alarm that triggers when an ERROR is encountered by Main Lambda function"
Value: !GetAtt InternalERRORMainAlarm.Arn
InternalWARNINGMainAlarm:
Description: "Alarm that triggers when a WARNING is encountered by Main Lambda function"
Value: !GetAtt InternalWARNINGMainAlarm.Arn
InternalERRORInteractAlarm:
Description: "Alarm that triggers when an ERROR is encountered by Interact Lambda function"
Value: !GetAtt InternalERRORInteractAlarm.Arn
InternalWARNINGInteractAlarm:
Description: "Alarm that triggers when a WARNING is encountered by Interact Lambda function"
Value: !GetAtt InternalWARNINGInteractAlarm.Arn
LackOfCPUCreditAlarm:
Description: "Alarm that triggers when at least one burstable instance has exhausted all its CPU credits"
Value: !GetAtt LackOfCPUCreditAlarm.Arn
ConfigTable:
Description: "Configuration table"
Value: !GetAtt ConfigTable.Arn
LongTermEventTable:
Description: "Logging table of noticeable events (requiring analysis)"
Value: !GetAtt LongTermEventTable.Arn
InteractQueue:
Description: "Queue for asynchronous calls with Interact Lambda function"
Value: !Ref InteractQueue
GroupName:
Description: "Group name for this CloneSquad deployment"
Value: !Ref GroupName
InteractAPIUrl:
Description: "Interact API Gateway URL"
Value: !Sub "https://${InteractApi}.execute-api.${AWS::Region}.amazonaws.com/v1/"