-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.yml
654 lines (621 loc) · 21.3 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
---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: >-
Step Functions and lambdas to interact with Stax to CUD (create-update-delete)
workloads and monitor task operations in your AWS Account(s).
Parameters:
PythonLoggingLevel:
Type: String
Description: Python logging level for Lambda functions
Default: INFO
AllowedValues:
- INFO
- DEBUG
- WARNING
- ERROR
- CRITICAL
DeployWorkloadStateMachine:
Type: String
Description: >-
Deploy workload state machine to CUD (create-update-delete) workloads.
Default: "false"
AllowedValues:
- "true"
- "false"
DeployWorkloadCloudwatchDashboard:
Type: String
Description: >-
Deploy workload cloudwatch dashboard to view stax orchestrator
step function metrics and logs.
Default: "false"
AllowedValues:
- "true"
- "false"
DeployTaskWatcherCloudwatchDashboard:
Type: String
Description: >-
Deploy workload cloudwatch dashboard to view stax orchestrator
step function metrics and logs.
Default: "false"
AllowedValues:
- "true"
- "false"
LambdaLogGroupRetentionInDays:
Type: Number
Description: >-
Number of days to retain lambda function logs;
applies to all lambda functions in this template
AllowedValues:
- 1
- 3
- 5
- 7
- 14
- 30
- 60
- 90
- 120
- 150
- 180
- 365
- 400
- 545
- 731
- 1827
- 2192
- 2557
- 2922
- 3288
- 3653
Default: 60
EnableStateMachineTracing:
Type: String
Description: Enable X-Ray tracing for state machines
Default: "false"
AllowedValues:
- "true"
- "false"
EnableLambdaTracing:
Type: String
Description: Enable X-Ray tracing for lambda functions
Default: "false"
AllowedValues:
- "true"
- "false"
EnableAlerting:
Type: String
Description: >-
Get alerts for failed step function executions; alerts on
failed workload deployments. Provide https endpoint or email
address to send alerts to.
Default: "true"
AllowedValues:
- "true"
- "false"
AlertsHttpsEndpoint:
Type: String
Description: >-
HTTPS endpoint to send alerts to.
Default: ""
AlertsEmail:
Type: String
Description: >-
Email address to send alerts to.
Default: ""
Metadata:
AWS::ServerlessRepo::Application:
Name: stax-orchestrator
Description: >-
Stax Workloads Orchestrator -
Interact with Stax to CUD (create-update-delete)
workloads in your AWS Account(s).
Author: Versent
SpdxLicenseId: Apache-2.0
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels: ['AWS', 'Stax', 'Serverless', 'Lambda', 'StepFunction']
HomePageUrl: https://github.com/Versent/stax-orchestrator.git
SourceCodeUrl: https://github.com/Versent/stax-orchestrator.git
Conditions:
WorkloadStateMachineEnabled: !Equals [!Ref DeployWorkloadStateMachine, "true"]
WorkloadCloudwatchDashboardEnabled: !And
- !Equals [!Ref DeployWorkloadCloudwatchDashboard, "true"]
- Condition: WorkloadStateMachineEnabled
TaskWatcherCloudwatchDashboardEnabled: !Equals
- !Ref DeployTaskWatcherCloudwatchDashboard
- "true"
StateMachineTracingEnabled: !Equals [!Ref EnableStateMachineTracing, "true"]
LambdaTracingEnabled: !Equals [!Ref EnableLambdaTracing, "true"]
AlertHttpsEndpointProvided: !Not [!Equals [!Ref AlertsHttpsEndpoint, ""]]
EnableAlerting: !Equals [!Ref EnableAlerting, "true"]
SubscribeHttpsEndpoint: !And
- Condition: EnableAlerting
- Condition: WorkloadStateMachineEnabled
- Condition: AlertHttpsEndpointProvided
AlertEmailProvided: !Not [!Equals [!Ref AlertsEmail, ""]]
SubscribeEmail: !And
- Condition: EnableAlerting
- Condition: WorkloadStateMachineEnabled
- Condition: AlertEmailProvided
Globals:
Function:
Runtime: python3.11
MemorySize: 128
Timeout: 300
Environment:
Variables:
LOG_LEVEL: !Ref PythonLoggingLevel
Layers:
- !Ref StaxLibLayer
Architectures:
- arm64
Resources:
WorkloadStateMachine:
Condition: WorkloadStateMachineEnabled
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: statemachines/workload.asl.json
Tracing:
Enabled: !If [StateMachineTracingEnabled, true, false]
DefinitionSubstitutions:
ValidateInputLambdaArn: !GetAtt ValidateInputLambda.Arn
CreateWorkloadLambdaArn: !GetAtt CreateWorkloadLambda.Arn
UpdateWorkloadLambdaArn: !GetAtt UpdateWorkloadLambda.Arn
DeleteWorkloadLambdaArn: !GetAtt DeleteWorkloadLambda.Arn
TaskFactoryArn: !GetAtt TaskWatcherStateMachine.Arn
Role: !GetAtt WorkloadStateMachineRole.Arn
WorkloadStateMachineRole:
Condition: WorkloadStateMachineEnabled
Type: AWS::IAM::Role
Properties:
Description: >-
Permissions for Stax Orchestrator Workload State Machine to invoke
lambdas and start step function executions
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: !Sub states.${AWS::Region}.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref StaxOrchestratorSfnPolicy
Policies:
- PolicyName: WorkloadStateMachinePolicy
PolicyDocument:
Statement:
- Sid: InvokeLambdaPolicy
Effect: Allow
Action: lambda:InvokeFunction
Resource:
- !GetAtt ValidateInputLambda.Arn
- !GetAtt CreateWorkloadLambda.Arn
- !GetAtt UpdateWorkloadLambda.Arn
- !GetAtt DeleteWorkloadLambda.Arn
- Sid: StartStepFunctionExecutionPolicy
Effect: Allow
Action: states:StartExecution
Resource:
- !Ref TaskWatcherStateMachine
TaskWatcherStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: statemachines/task_watcher.asl.json
Tracing:
Enabled: !If [StateMachineTracingEnabled, true, false]
DefinitionSubstitutions:
GetTaskStatusLambdaArn: !GetAtt GetTaskStatusLambda.Arn
Role: !GetAtt TaskWatcherStateMachineRole.Arn
TaskWatcherStateMachineRole:
Type: AWS::IAM::Role
Properties:
Description: >-
Permissions for Stax Orchestrator Task Watcher State Machine
to assume role and invoke GetTaskStatusLambda
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: !Sub states.${AWS::Region}.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref StaxOrchestratorSfnPolicy
Policies:
- PolicyName: TaskWatcherStateMachinePolicy
PolicyDocument:
Statement:
- Sid: InvokeGetTaskStatusLambdaPolicy
Effect: Allow
Action: lambda:InvokeFunction
Resource:
- !GetAtt GetTaskStatusLambda.Arn
ValidateInputLambda:
Condition: WorkloadStateMachineEnabled
Type: AWS::Serverless::Function
Properties:
Description: >-
Validate user input required to create/update/delete workloads
CodeUri: functions/validate_input/
Handler: app.lambda_handler
Tracing: !If [LambdaTracingEnabled, Active, !Ref AWS::NoValue]
Policies:
- !Ref StaxOrchestratorLambdaPolicy
- Fn::If:
- LambdaTracingEnabled
- arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess
- !Ref AWS::NoValue
CreateWorkloadLambda:
Condition: WorkloadStateMachineEnabled
Type: AWS::Serverless::Function
Properties:
Description: Invoke Stax API to create a workload
CodeUri: functions/create_workload/
Handler: app.lambda_handler
Tracing: !If [LambdaTracingEnabled, Active, !Ref AWS::NoValue]
Policies:
- !Ref StaxOrchestratorLambdaPolicy
- Fn::If:
- LambdaTracingEnabled
- arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess
- !Ref AWS::NoValue
UpdateWorkloadLambda:
Condition: WorkloadStateMachineEnabled
Type: AWS::Serverless::Function
Properties:
Description: Invoke Stax API to update a workload
CodeUri: functions/update_workload/
Handler: app.lambda_handler
Tracing: !If [LambdaTracingEnabled, Active, !Ref AWS::NoValue]
Policies:
- !Ref StaxOrchestratorLambdaPolicy
- Fn::If:
- LambdaTracingEnabled
- arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess
- !Ref AWS::NoValue
DeleteWorkloadLambda:
Condition: WorkloadStateMachineEnabled
Type: AWS::Serverless::Function
Properties:
Description: Invoke Stax API to delete a workload
CodeUri: functions/delete_workload/
Handler: app.lambda_handler
Tracing: !If [LambdaTracingEnabled, Active, !Ref AWS::NoValue]
Policies:
- !Ref StaxOrchestratorLambdaPolicy
- Fn::If:
- LambdaTracingEnabled
- arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess
- !Ref AWS::NoValue
GetTaskStatusLambda:
Type: AWS::Serverless::Function
Properties:
Description: Get status of a workload task
CodeUri: functions/get_task_status/
Handler: app.lambda_handler
Tracing: !If [LambdaTracingEnabled, Active, !Ref AWS::NoValue]
Policies:
- !Ref StaxOrchestratorLambdaPolicy
- Fn::If:
- LambdaTracingEnabled
- arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess
- !Ref AWS::NoValue
StaxLibLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: orchestrator-stax-lib-lambda-layer
Description: >-
Lambda layer containing dependencies for stax
orchestrator lambda functions
LicenseInfo: Available under the Apache license.
ContentUri: .
CompatibleArchitectures:
- arm64
CompatibleRuntimes:
- python3.11
RetentionPolicy: Delete
Metadata:
BuildMethod: makefile
BuildArchitecture: arm64
StaxLibLayerPermission:
Type: AWS::Lambda::LayerVersionPermission
Properties:
Action: lambda:GetLayerVersion
LayerVersionArn: !Ref StaxLibLayer
Principal: !Ref AWS::AccountId
StaxOrchestratorLambdaPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: >-
Stax Orchestrator Managed Policy for
lambda functions to interact with AWS Resources.
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowGetStaxAccessKeyAndSecretPolicy
Effect: Allow
Action:
- ssm:GetParameter
Resource:
# yamllint disable rule:line-length
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/orchestrator/stax/access/key
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/orchestrator/stax/access/key/secret
# yamllint enable rule:line-length
StaxOrchestratorSfnPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: >-
Stax Orchestrator Managed Policy for
step functions to interact with AWS Resources.
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowEventsPutAndDescribePolicy
Effect: Allow
Action:
- events:PutTargets
- events:PutRule
- events:DescribeRule
Resource:
# yamllint disable rule:line-length
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule
# yamllint enable rule:line-length
- !If
- StateMachineTracingEnabled
- Sid: AllowXrayTracingPolicy
Effect: Allow
Action:
- xray:PutTraceSegments
- xray:PutTelemetryRecords
- xray:GetSamplingRules
- xray:GetSamplingTargets
- xray:GetSamplingStatisticSummaries
Resource:
- '*'
- Ref: AWS::NoValue
ValidateInputLambdaLogGroup:
Condition: WorkloadStateMachineEnabled
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${ValidateInputLambda}
RetentionInDays: !Ref LambdaLogGroupRetentionInDays
CreateWorkloadLambdaLogGroup:
Condition: WorkloadStateMachineEnabled
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${CreateWorkloadLambda}
RetentionInDays: !Ref LambdaLogGroupRetentionInDays
UpdateWorkloadLambdaLogGroup:
Condition: WorkloadStateMachineEnabled
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${UpdateWorkloadLambda}
RetentionInDays: !Ref LambdaLogGroupRetentionInDays
DeleteWorkloadLambdaLogGroup:
Condition: WorkloadStateMachineEnabled
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${DeleteWorkloadLambda}
RetentionInDays: !Ref LambdaLogGroupRetentionInDays
GetTaskStatusLambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${GetTaskStatusLambda}
RetentionInDays: !Ref LambdaLogGroupRetentionInDays
StaxOrchestratorWorkloadDashboard:
Condition: WorkloadCloudwatchDashboardEnabled
Type: AWS::CloudWatch::Dashboard
Properties:
# yamllint disable rule:line-length
DashboardBody: !Sub |
{
"widgets": [
{
"type": "log",
"x": 10,
"y": 6,
"width": 14,
"height": 3,
"properties": {
"query": "SOURCE '/aws/lambda/${CreateWorkloadLambda}' | fields @message\r\n| filter @message like /ERROR/",
"region": "${AWS::Region}",
"stacked": false,
"title": "Create Workload Error Logs",
"view": "table"
}
},
{
"type": "log",
"x": 10,
"y": 6,
"width": 14,
"height": 3,
"properties": {
"query": "SOURCE '/aws/lambda/${UpdateWorkloadLambda}' | fields @message\r\n| filter @message like /ERROR/",
"region": "${AWS::Region}",
"stacked": false,
"title": "Update Workload Error Logs",
"view": "table"
}
},
{
"type": "log",
"x": 10,
"y": 6,
"width": 14,
"height": 3,
"properties": {
"query": "SOURCE '/aws/lambda/${DeleteWorkloadLambda}' | fields @message\r\n| filter @message like /ERROR/",
"region": "${AWS::Region}",
"stacked": false,
"title": "Delete Workload Error Logs",
"view": "table"
}
},
{
"type": "metric",
"x": 0,
"y": 0,
"width": 10,
"height": 3,
"properties": {
"view": "singleValue",
"metrics": [
[ "AWS/States", "ExecutionsFailed", "StateMachineArn", "${WorkloadStateMachine}", {"color": "#ff0000"} ],
[ ".", "ExecutionsSucceeded", ".", ".", {"color": "#2ca02c"} ],
[ ".", "ExecutionsAborted", ".", ".", {"color": "#ffa500"} ]
],
"stat": "Sum",
"setPeriodToTimeRange": true,
"region": "${AWS::Region}",
"title": "WorkloadStateMachine Executions",
"period": 300
}
},
{
"type": "metric",
"x": 0,
"y": 9,
"width": 10,
"height": 4,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/States", "ExecutionsSucceeded", "StateMachineArn", "${WorkloadStateMachine}", {"color": "#2ca02c"} ],
[ ".", "ExecutionsFailed", ".", ".", {"color": "#ff0000"} ]
],
"region": "${AWS::Region}",
"title": "Workload Operations Success Vs Failures"
}
}
]
}
# yamllint enable rule:line-length
StaxOrchestratorTaskDashboard:
Condition: TaskWatcherCloudwatchDashboardEnabled
Type: AWS::CloudWatch::Dashboard
Properties:
# yamllint disable rule:line-length
DashboardBody: !Sub |
{
"widgets": [
{
"type": "log",
"x": 10,
"y": 6,
"width": 14,
"height": 3,
"properties": {
"query": "SOURCE '/aws/lambda/${GetTaskStatusLambda}' | fields @message\r\n| filter @message like /ERROR/",
"region": "${AWS::Region}",
"stacked": false,
"title": "Get Task Status Error Logs",
"view": "table"
}
},
{
"type": "metric",
"x": 0,
"y": 0,
"width": 10,
"height": 3,
"properties": {
"view": "singleValue",
"metrics": [
[ "AWS/States", "ExecutionsFailed", "StateMachineArn", "${TaskWatcherStateMachine}", {"color": "#ff0000"} ],
[ ".", "ExecutionsSucceeded", ".", ".", {"color": "#2ca02c"} ],
[ ".", "ExecutionsAborted", ".", ".", {"color": "#ffa500"} ]
],
"stat": "Sum",
"setPeriodToTimeRange": true,
"region": "${AWS::Region}",
"title": "TaskWatcherStateMachine Executions",
"period": 300
}
},
{
"type": "metric",
"x": 0,
"y": 9,
"width": 10,
"height": 4,
"properties": {
"view": "timeSeries",
"stacked": false,
"metrics": [
[ "AWS/States", "ExecutionsSucceeded", "StateMachineArn", "${TaskWatcherStateMachine}", {"color": "#2ca02c"} ],
[ ".", "ExecutionsFailed", ".", ".", {"color": "#ff0000"} ]
],
"region": "${AWS::Region}",
"title": "TaskWatcher Operations Success Vs Failures"
}
}
]
}
# yamllint enable rule:line-length
FailedWorkloadStateMachineEventsRule:
Condition: WorkloadStateMachineEnabled
Type: AWS::Events::Rule
Properties:
Description: Trigger SNS topic when WorkloadStateMachine fails
Targets:
- Arn: !Ref AlertsTopic
Id: FailedWorkloadStateMachineEventsRule
EventPattern:
source:
- aws.states
detail-type:
- Step Functions Execution Status Change
detail:
status:
- FAILED
stateMachineArn:
- !Ref WorkloadStateMachine
AlertsTopicPolicy:
Condition: WorkloadStateMachineEnabled
Type: AWS::SNS::TopicPolicy
Properties:
PolicyDocument:
Id: AlertsTopicPolicy
Version: 2012-10-17
Statement:
- Sid: AllowEventsServiceToPublishToTopic
Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sns:Publish
Resource: !Ref AlertsTopic
Topics:
- !Ref AlertsTopic
AlertsTopic:
Condition: WorkloadStateMachineEnabled
Type: AWS::SNS::Topic
HttpEndpointSubscription:
Condition: SubscribeHttpsEndpoint
Type: AWS::SNS::Subscription
Properties:
Protocol: https
TopicArn: !Ref AlertsTopic
Endpoint: !Ref AlertsHttpsEndpoint
EmailSubscription:
Condition: SubscribeEmail
Type: AWS::SNS::Subscription
Properties:
Protocol: email
TopicArn: !Ref AlertsTopic
Endpoint: !Ref AlertsEmail
Outputs:
WorkloadStateMachineArn:
Condition: WorkloadStateMachineEnabled
Description: Stax orchestrator workload deployment step function arn
Value: !Ref WorkloadStateMachine
TaskWatcherStateMachineArn:
Description: Stax orchestrator task watcher step function arn
Value: !Ref TaskWatcherStateMachine
AlertsTopicArn:
Condition: WorkloadStateMachineEnabled
Description: >-
Stax orchestrator SNS topic arn to subscribe to for alert notifications
Value: !Ref AlertsTopic