-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
388 lines (376 loc) · 17.5 KB
/
serverless.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
# TemplateName: serverless.yaml
# TemplateType: Step Function with SNS, SQS, Lambda, S3 and CloudWatch Alarms
# Version: 1.0.0
# Owner: Subhamay Bhattacharyya
# ProjectName: Tarius
# Modification History:
# - 1.0.0 - Jun 16, 2023 -- Initial Version
# Resources:
# - One SNS Topic with KMS Encryption and Email Subscription.
# - One SQS Queue with KMS Encryption
# - One S3 Bucket with KMS Encryption
# - One Step Function with associated IAM execution role.
# - Two Lambda Functions with associated IAM Role
# - One Step function with associated IAM Role
# - Five CloudWatch Alarms for each of the Lambda functions.
service: bellflower
# app: 0038-bellflower-sls
# org: subhamayaws
package:
individually: true
frameworkVersion: "^3.25.1"
configValidationMode: off
plugins:
- serverless-step-functions
- serverless-aws-nested-stacks
params:
devl:
kms-key-alias: "SB-KMS"
kms-key-id: "arn:aws:kms:us-east-1:807724355529:key/e4c733c5-9fbe-4a90-bda1-6f0362bc9b89"
sns-subscription-email: subhamay.bellflower.sls@mailinator.com
delay-seconds: 0
maximum-message-size: 262144
message-retention-period: 345600
receive-message-waittime-seconds: 0
message-visibility-timeout: 300
s3-custom-resource-stack-name: "bellflower-s3-custom-resource-stack"
provider:
name: aws
stackName: ${self:service}-sls-stack
stage: devl
region: us-east-1
runtime: python3.9
architecture: x86_64
versionFunctions: false
memorySize: 256
timeout: 300
environment:
S3_BUCKET_NAME: ${self:custom.s3-bucket-name}
tags:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
deploymentBucket:
name: subhamay-serverless-deploy-us-east-1
serverSideEncryption: aws:kms
sseKMSKeyId: ${param:kms-key-id}
custom:
sf-iam-role: ${self:service}-sf-execution-role
sf-iam-policy: ${self:service}-sf-execution-policy
lambda-iam-role: ${self:service}-lambda-function-execution-role
lambda-iam-policy: ${self:service}-lambda-function-execution-policy
lambda-iam-role-arn: arn:aws:iam::${aws:accountId}:role/${self:custom.lambda-iam-role}
step-function-name: ${self:service}-state-machine-${self:provider.stage}-${self:provider.region}
sns-topic-name: ${self:service}-sns-topic-${self:provider.stage}-${self:provider.region}
sqs-queue-name: ${self:service}-sqs-queue-${self:provider.stage}-${self:provider.region}
s3-bucket-name: ${self:service}-integration-bucket-${aws:accountId}-${self:provider.stage}-${self:provider.region}
sqs-queue-arn: "arn:aws:sqs:${self:provider.region}:${aws:accountId}:${self:custom.sqs-queue-name}"
lambda-processing-name: ${self:service}-processing-lambda-${self:provider.stage}-${self:provider.region}
lambda-integration-name: ${self:service}-integration-lambda-${self:provider.stage}-${self:provider.region}
log-group-step-function-name: /aws/states/${self:service}-step-function-${self:provider.stage}-${self:provider.region}
nested-stacks:
location: "./nested-stacks"
stacks:
- id: BellflowerSQSQueue
template: sqs-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
SQSQueueName: ${self:custom.sqs-queue-name}
DelaySeconds: ${param:delay-seconds}
MaximumMessageSize: ${param:maximum-message-size}
MessageRetentionPeriod: ${param:message-retention-period}
ReceiveMessageWaitTimeSeconds: ${param:receive-message-waittime-seconds}
VisibilityTimeout: ${param:message-visibility-timeout}
KmsMasterKeyAlias: ${param:kms-key-alias}
- id: BellflowerSNSTopic
template: sns-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
SNSTopicName: ${self:custom.sns-topic-name}
SNSTopicDisplayName: "SNS Topic name for sending result notification"
SNSSubscriptionEmail: "subhamay.aws@mailinator.com"
KmsMasterKeyAlias: ${param:kms-key-alias}
- id: BellflowerIAMRole
template: iam-role-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
ProcessingLambdaFunctionName: ${self:custom.lambda-processing-name}
IntegrationLambdaFunctionName: ${self:custom.lambda-integration-name}
LambdaExecutionRoleName: ${self:custom.lambda-iam-role}
LambdaExecutionPolicyName: ${self:custom.lambda-iam-policy}
StepFunctionExecutionRoleName: ${self:custom.sf-iam-role}
StepFunctionExecutionPolicyName: ${self:custom.sf-iam-policy}
SQSQueueName: ${self:custom.sqs-queue-name}
SNSTopicName: ${self:custom.sns-topic-name}
S3BucketName: ${self:custom.s3-bucket-name}
KmsMasterKeyId: ${param:kms-key-id}
StepFunctionName: ${self:custom.step-function-name}
- id: BellflowerS3Bucket
template: s3-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
S3BucketName: ${self:custom.s3-bucket-name}
S3DataBucketFolder: "inbound,outbound"
S3BucketBlockPublicAcls: true
S3BucketBlockPublicPolicy: true
S3BucketIgnorePublicAcls: true
S3BucketRestrictPublicBuckets: true
S3CustomResourceStackName: ${param:s3-custom-resource-stack-name}
KmsMasterKeyAlias: ${param:kms-key-alias}
- id: BellflowerProcessingLambdaCloudWatchAlarms
template: cloudwatch-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
LambdaFunctionName: ${self:custom.lambda-processing-name}
SNSTopicName: ${self:custom.sns-topic-name}
AlarmNameLambdaInvocations: ${self:service}-processing-lambda-invocations-alarm
AlarmPeriodInSecondsLambdaInvocations: 900
DatapointsToAlarmLambdaInvocations: 1
EvaluationPeriodsForLambdaInvocations: 1
AlarmComparisonOperatorLambdaInvocations: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaErrors: ${self:service}-processing-lambda-errors-alarm
AlarmThresholdLambdaErrors: 2
AlarmPeriodInSecondsLambdaErrors: 900
DatapointsToAlarmLambdaErrors: 1
EvaluationPeriodsForLambdaErrors: 1
AlarmComparisonOperatorLambdaErrors: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaThrottles: ${self:service}-processing-lambda-throttles-alarm
AlarmThresholdLambdaThrottles: 3
AlarmPeriodInSecondsLambdaThrottles: 600
DatapointsToAlarmLambdaThrottles: 1
EvaluationPeriodsForLambdaThrottles: 1
AlarmComparisonOperatorLambdaThrottles: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaDuration: ${self:service}-processing-lambda-duration-alarm
AlarmThresholdLambdaDuration: 500
AlarmPeriodInSecondsLambdaDuration: 900
DatapointsToAlarmLambdaDuration: 1
EvaluationPeriodsForLambdaDuration: 1
AlarmComparisonOperatorLambdaDuration: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaConcurrentExecutions: ${self:service}-processing-lambda-conc-exec-alarm
AlarmThresholdLambdaConcurrentExecutions: 3
AlarmPeriodInSecondsLambdaConcurrentExecutions: 60
DatapointsToAlarmLambdaConcurrentExecutions: 1
EvaluationPeriodsForLambdaConcurrentExecutions: 1
AlarmComparisonOperatorLambdaConcurrentExecutions: "GreaterThanOrEqualToThreshold"
- id: BellflowerIntegrationLambdaCloudWatchAlarms
template: cloudwatch-stack.yaml
enabled: true
timeout: 300
parameters:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
LambdaFunctionName: ${self:custom.lambda-integration-name}
SNSTopicName: ${self:custom.sns-topic-name}
AlarmNameLambdaInvocations: ${self:service}-integration-lambda-invocations-alarm
AlarmPeriodInSecondsLambdaInvocations: 900
DatapointsToAlarmLambdaInvocations: 1
EvaluationPeriodsForLambdaInvocations: 1
AlarmComparisonOperatorLambdaInvocations: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaErrors: ${self:service}-integration-lambda-errors-alarm
AlarmThresholdLambdaErrors: 2
AlarmPeriodInSecondsLambdaErrors: 900
DatapointsToAlarmLambdaErrors: 1
EvaluationPeriodsForLambdaErrors: 1
AlarmComparisonOperatorLambdaErrors: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaThrottles: ${self:service}-integration-lambda-throttles-alarm
AlarmThresholdLambdaThrottles: 3
AlarmPeriodInSecondsLambdaThrottles: 600
DatapointsToAlarmLambdaThrottles: 1
EvaluationPeriodsForLambdaThrottles: 1
AlarmComparisonOperatorLambdaThrottles: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaDuration: ${self:service}-integration-lambda-duration-alarm
AlarmThresholdLambdaDuration: 500
AlarmPeriodInSecondsLambdaDuration: 900
DatapointsToAlarmLambdaDuration: 1
EvaluationPeriodsForLambdaDuration: 1
AlarmComparisonOperatorLambdaDuration: "GreaterThanOrEqualToThreshold"
AlarmNameLambdaConcurrentExecutions: ${self:service}-integration-lambda-conc-exec-alarm
AlarmThresholdLambdaConcurrentExecutions: 3
AlarmPeriodInSecondsLambdaConcurrentExecutions: 60
DatapointsToAlarmLambdaConcurrentExecutions: 1
EvaluationPeriodsForLambdaConcurrentExecutions: 1
AlarmComparisonOperatorLambdaConcurrentExecutions: "GreaterThanOrEqualToThreshold"
resources:
Description: >-
Project Bellflower: Root Stack Template to create a State Machine and associated services and IAM role to showcase callback pattern example (Amazon SQS, Amazon SNS, Lambda).
Resources:
#################################### Processing Lambda Log Group ###############################
BellflowerStateMachineLogGroup:
Type: AWS::Logs::LogGroup
Properties:
KmsKeyId: ${param:kms-key-id}
LogGroupName: ${self:custom.log-group-step-function-name}
RetentionInDays: 14
Tags:
- Key: Project
Value: ${self:service}
- Key: Environment
Value: ${self:provider.stage}
Outputs:
BellfloweSNSTopicArn:
Description: Arn of the SNS Topic
Value: !GetAtt BellflowerSNSTopic.Outputs.SNSTopicArn
BellflowerSNSSubscriptionArn:
Description: Arn of the SNS Subscription Email
Value: !GetAtt BellflowerSNSTopic.Outputs.SNSSubscriptionArn
BellflowerSQSQueueArn:
Description: Arn of the SQS Queue
Value: !GetAtt BellflowerSQSQueue.Outputs.SQSQueueArn
BellflowerSQSQueueURL:
Description: Arn of the SQS Queue
Value: !GetAtt BellflowerSQSQueue.Outputs.SQSQueueURL
BellflowerS3BucketArn:
Description: Arn of the S3 integration bucket.
Value: !Sub "arn:${AWS::Partition}:s3:::${self:custom.s3-bucket-name}"
BellflowerLambdaExecutionRoleArn:
Description: Arn of the Bellflower Lambda Execution Role
Value: !GetAtt BellflowerIAMRole.Outputs.LambdaExecutionRoleArn
BellflowerStepFunctionExecutionRoleArn:
Description: Arn of the Step Function Execution Role
Value: !GetAtt BellflowerIAMRole.Outputs.StepFunctionExecutionRoleArn
BellflowerStepFunctionArn:
Description: Arn of the Bellflower Step Function
Value: !Sub "arn:${AWS::Partition}:states:${AWS::Partition}:${AWS::AccountId}:stateMachine:${self:custom.step-function-name}"
BellflowerStateMachineLogGroupArn:
Description: Arn of the Step Function Log Group
Value: !GetAtt BellflowerStateMachineLogGroup.Arn
BellflowerProcessingLambdaInvocationsAlarmArn:
Description: Bellflower Processing Lambda Invocations Alarm Arn
Value: !GetAtt BellflowerProcessingLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaInvocationsArn
BellflowerProcessingLambdaErrorsAlarmArn:
Description: Bellflower Processing Lambda Errors Alarm Arn
Value: !GetAtt BellflowerProcessingLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaErrorsArn
BellflowerProcessingLambdaThrottlesAlarmArn:
Description: Bellflower Processing Lambda Throttles Alarm Arn
Value: !GetAtt BellflowerProcessingLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaThrottlesArn
BellflowerProcessingLambdaDurationAlarmArn:
Description: Bellflower LProcessing ambda Duration Alarm Arn
Value: !GetAtt BellflowerProcessingLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaDurationArn
BellflowerProcessingLambdaConcurrentExecutionsAlarmArn:
Description: Bellflower Processing Lambda Concurrent Executions Alarm Arn
Value: !GetAtt BellflowerProcessingLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaConurrentExecutionsArn
BellflowerIntegrationLambdaInvocationsAlarmArn:
Description: Bellflower Integration Lambda Invocations Alarm Arn
Value: !GetAtt BellflowerIntegrationLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaInvocationsArn
BellflowerIntegrationLambdaErrorsAlarmArn:
Description: Bellflower Integration Lambda Errors Alarm Arn
Value: !GetAtt BellflowerIntegrationLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaErrorsArn
BellflowerIntegrationLambdaThrottlesAlarmArn:
Description: Bellflower Integration Lambda Throttles Alarm Arn
Value: !GetAtt BellflowerIntegrationLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaThrottlesArn
BellflowerIntegrationLambdaDurationAlarmArn:
Description: Bellflower LIntegration ambda Duration Alarm Arn
Value: !GetAtt BellflowerIntegrationLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaDurationArn
BellflowerIntegrationLambdaConcurrentExecutionsAlarmArn:
Description: Bellflower Integration Lambda Concurrent Executions Alarm Arn
Value: !GetAtt BellflowerIntegrationLambdaCloudWatchAlarms.Outputs.CloudWatchAlarmLambdaConurrentExecutionsArn
functions:
bellflower-processing-lambda:
handler: processing_lambda.lambda_handler
name: ${self:custom.lambda-processing-name}
description: "The Lambda Function to generate a list of random strings."
reservedConcurrency: 1
package:
artifact: s3://subhamay-projects-repository-us-east-1/0038-bellflower/code/python/processing_lambda.zip
role: ${self:custom.lambda-iam-role-arn}
dependsOn: BellflowerIAMRole
bellflower-integration-lambda:
handler: integration_lambda.lambda_handler
name: ${self:custom.lambda-integration-name}
description: "The Lambda Function to be used for callback functionality"
reservedConcurrency: 1
package:
artifact: s3://subhamay-projects-repository-us-east-1/0038-bellflower/code/python/integration_lambda.zip
role: ${self:custom.lambda-iam-role-arn}
events:
- sqs:
arn: ${self:custom.sqs-queue-arn}
batchSize: 10
- s3:
bucket: ${self:custom.s3-bucket-name}
existing: true
event: s3:ObjectCreated:*
rules:
- prefix: inbound/
- suffix: .json
dependsOn: BellflowerIAMRole
stepFunctions:
stateMachines:
BellflowerStateMachine:
name: ${self:custom.step-function-name}
role: !GetAtt BellflowerIAMRole.Outputs.StepFunctionExecutionRoleArn
loggingConfig:
level: ALL
includeExecutionData: true
destinations:
- !GetAtt BellflowerStateMachineLogGroup.Arn
definition:
Comment: >-
Bellflower State Machine - Callback Pattern Example (Amazon SQS, Amazon SNS,
Lambda)
StartAt: Processing Lambda
TimeoutSeconds: 300
States:
Processing Lambda:
Type: Task
Resource: arn:aws:states:::lambda:invoke
OutputPath: $.Payload
Parameters:
Payload.$: $
FunctionName: arn:aws:lambda:${self:provider.region}:${aws:accountId}:function:${self:custom.lambda-processing-name}:$LATEST
Retry:
- ErrorEquals:
- States.ALL
IntervalSeconds: 5
MaxAttempts: 2
BackoffRate: 2
Comment: Automatic Retry 3 Times
InputPath: $
Next: Wait For Response
Catch:
- ErrorEquals:
- States.ALL
Next: SNS Publish (Failure)
SNS Publish (Failure):
Type: Task
Resource: arn:aws:states:::sns:publish
Parameters:
Message.$: $
TopicArn: !GetAtt BellflowerSNSTopic.Outputs.SNSTopicArn
Next: Fail
Fail:
Type: Fail
Wait For Response:
Type: Task
Resource: arn:aws:states:::sqs:sendMessage.waitForTaskToken
Parameters:
QueueUrl: !GetAtt BellflowerSQSQueue.Outputs.SQSQueueURL
MessageBody:
Input.$: $
TaskToken.$: $$.Task.Token
ResultPath: null
Next: SNS Publish (Success)
SNS Publish (Success):
Type: Task
Resource: arn:aws:states:::sns:publish
Parameters:
Message.$: $
TopicArn: !GetAtt BellflowerSNSTopic.Outputs.SNSTopicArn
Next: Success
Success:
Type: Succeed
dependsOn: BellflowerIAMRole