-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
432 lines (421 loc) · 16.9 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
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
service: celosia
# app: 0069-celosia-sls
# org: subhamayaws
frameworkVersion: "^3.25.1"
params:
devl:
kms-key-alias: alias/SB-KMS
kms-key-id: arn:aws:kms:us-east-1:807724355529:key/e4c733c5-9fbe-4a90-bda1-6f0362bc9b89
sns-subscription-email: subhamay.aws@mailinator.com
delay-seconds: 0
maximum-message-size: 262144
message-retention-period: 60
receive-message-waittime-seconds: 0
message-visibility-timeout: 0
lambda-invocations-alarm-name: ${self:service}-invocations-alarm-${self:provider.stage}-${self:provider.region}
lambda-errors-alarm-name: ${self:service}-errors-alarm-${self:provider.stage}-${self:provider.region}
lambda-throttles-alarm-name: ${self:service}-throttles-alarm-${self:provider.stage}-${self:provider.region}
lambda-duration-alarm-name: ${self:service}-duration-alarm-${self:provider.stage}-${self:provider.region}
lambda-conc-execution-alarm-name: ${self:service}-concurrent-executions-alarm-${self:provider.stage}-${self:provider.region}
comparison-operator-invocations-alarm: GreaterThanOrEqualToThreshold
comparison-operator-errors-alarm: GreaterThanOrEqualToThreshold
comparison-operator-throttles-alarm: GreaterThanOrEqualToThreshold
comparison-operator-duration-alarm: GreaterThanOrEqualToThreshold
comparison-operator-conc-execution-alarm: GreaterThanOrEqualToThreshold
threshold-lambda-invocations-alarm: 2
threshold-lambda-errors-alarm: 2
threshold-lambda-throttles-alarm: 2
threshold-lambda-duration-alarm: 2
threshold-lambda-conc-execution-alarm: 2
datapoints-to-alarm-invocations-lambda: 1
datapoints-to-alarm-errors-lambda: 1
datapoints-to-alarm-throttles-lambda: 1
datapoints-to-alarm-duration-lambda: 1
datapoints-to-alarm-conc-execution-lambda: 1
evaluation-period-invocations-lambda: 1
evaluation-period-errors-lambda: 1
evaluation-period-throttles-lambda: 1
evaluation-period-duration-lambda: 1
evaluation-period-conc-execution-lambda: 1
alarm-period-in-seconds-invocations-lambda: 60
alarm-period-in-seconds-errors-lambda: 60
alarm-period-in-seconds-throttles-lambda: 60
alarm-period-in-seconds-duration-lambda: 60
alarm-period-in-seconds-conc-execution-lambda: 60
provider:
name: aws
stackName: celosia-sls-stack
deploymentBucket:
name: subhamay-serverless-deploy-us-east-1
serverSideEncryption: AES256
runtime: python3.9
stage: devl
region: us-east-1
environment:
DYNAMODB_TABLE_NAME: ${self:custom.dynamodb-table}
iam:
role:
statements:
- Sid: AllowDynamoDBPutAndGetItem
Effect: 'Allow'
Action:
- dynamodb:GetItem
- dynamodb:PutItem
Resource: !GetAtt CelosiaQuotesDynamoDbTable.Arn
- Sid: AllowSNSPublishMessage
Effect: Allow
Action:
- sns:Publish
Resource: !Ref CelosiaSNSTopic
- Sid: AllowKMSDecrypt
Effect: Allow
Action:
- kms:Decrypt
- kms:Encrypt
- kms:GenerateDataKey
- kms:GenerateDataKeyPair
Resource: ${param:kms-key-id}
- Sid: AllowSQSReadWriteDelete
Effect: Allow
Action:
- sqs:ReceiveMessage
- sqs:SendMessage
- sqs:DeleteMessage
Resource: !GetAtt CelosiaSQSQueue.Arn
name: ${self:custom.lambda-iam-role}
tags:
ProjectName: ${self:service}
Environment: ${self:provider.stage}
custom:
dynamodb-table: ${self:service}-table-${self:provider.stage}-${self:provider.region}
lambda-function: ${self:service}-insert-data-${self:provider.stage}-${self:provider.region}
lambda-iam-role: ${self:service}-lambda-execution-role
sf-iam-role: ${self:service}-sf-execution-role
sqs-queue: ${self:service}-queue-${self:provider.stage}-${self:provider.region}
sns-topic: ${self:service}-sns-topic-${self:provider.stage}-${self:provider.region}
step-function: ${self:service}-step-function-${self:provider.stage}-${self:provider.region}
plugins:
- serverless-step-functions
resources:
Resources:
CelosiaQuotesDynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: MessageId
AttributeType: S
KeySchema:
- AttributeName: MessageId
KeyType: HASH
BillingMode: PAY_PER_REQUEST
SSESpecification:
KMSMasterKeyId: ${param:kms-key-alias}
SSEEnabled: True
SSEType: KMS
TableName: ${self:custom.dynamodb-table}
CelosiaSNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "SNS Topic Name For CloudWatchAlert Notification"
TopicName: ${self:custom.sns-topic}
KmsMasterKeyId: ${param:kms-key-alias}
Tags:
- Key: Project
Value: ${self:service}
- Key: Environment
Value: ${self:provider.stage}
CelosiaSNSSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: email
Endpoint: ${param:sns-subscription-email}
TopicArn: !Ref CelosiaSNSTopic
CelosiaSQSQueue:
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Type: AWS::SQS::Queue
Properties:
DelaySeconds: ${param:delay-seconds}
KmsMasterKeyId: ${param:kms-key-alias}
MaximumMessageSize: ${param:maximum-message-size}
MessageRetentionPeriod: ${param:message-retention-period}
QueueName: ${self:custom.sqs-queue}
ReceiveMessageWaitTimeSeconds: ${param:receive-message-waittime-seconds}
VisibilityTimeout: ${param:message-visibility-timeout}
Tags:
- Key: Project
Value: ${self:service}
- Key: Environment
Value: ${self:provider.stage}
CelosiaSQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref CelosiaSQSQueue
PolicyDocument:
Version: "2012-10-17"
Id: AllowIncomingAccess
Statement:
- Sid: __owner_statement
Effect: Allow
Principal:
AWS:
- !Ref AWS::AccountId
Action:
- 'SQS:*'
Resource:
- !GetAtt CelosiaSQSQueue.Arn
###################################### 1. Lambda Invocations CW Alarm ############################
CloudWatchAlarmInvocations:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: true
AlarmName: ${param:lambda-invocations-alarm-name}
AlarmDescription: "${self:service} Alarm for number of Lambda function invocations"
AlarmActions:
- !Ref CelosiaSNSTopic
ComparisonOperator: ${param:comparison-operator-invocations-alarm}
MetricName: "Invocations"
Namespace: AWS/Lambda
Statistic: "Sum"
Threshold: ${param:threshold-lambda-invocations-alarm}
DatapointsToAlarm: ${param:datapoints-to-alarm-invocations-lambda}
EvaluationPeriods: ${param:evaluation-period-invocations-lambda}
Dimensions:
- Name: "FunctionName"
Value: ${self:custom.lambda-function}
Period: ${param:alarm-period-in-seconds-invocations-lambda}
TreatMissingData: notBreaching
###################################### 2. Lambda Error CW Alarm ##################################
CloudWatchAlarmErrors:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: true
AlarmName: ${param:lambda-errors-alarm-name}
AlarmDescription: !Sub '${self:service} - Alarm for number Lambda function errors'
AlarmActions:
- !Ref CelosiaSNSTopic
ComparisonOperator: ${param:comparison-operator-errors-alarm}
MetricName: "Errors"
Namespace: AWS/Lambda
Statistic: "Sum"
Threshold: ${param:threshold-lambda-errors-alarm}
DatapointsToAlarm: ${param:datapoints-to-alarm-errors-lambda}
EvaluationPeriods: ${param:evaluation-period-errors-lambda}
Dimensions:
- Name: "FunctionName"
Value: ${self:custom.lambda-function}
Period: ${param:alarm-period-in-seconds-errors-lambda}
TreatMissingData: notBreaching
###################################### 3. Lambda Throttles CW Alarm ##############################
CloudWatchAlarmThrottles:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: true
AlarmName: ${param:lambda-throttles-alarm-name}
AlarmDescription: !Sub '${self:service} - Alarm for Lambda function throttles'
AlarmActions:
- !Ref CelosiaSNSTopic
ComparisonOperator: ${param:comparison-operator-throttles-alarm}
MetricName: "Throttles"
Namespace: AWS/Lambda
Statistic: "Sum"
Threshold: ${param:threshold-lambda-throttles-alarm}
DatapointsToAlarm: ${param:datapoints-to-alarm-throttles-lambda}
EvaluationPeriods: ${param:evaluation-period-throttles-lambda}
Dimensions:
- Name: "FunctionName"
Value: ${self:custom.lambda-function}
Period: ${param:alarm-period-in-seconds-throttles-lambda}
TreatMissingData: notBreaching
###################################### 4. Lambda Duration CW Alarm ###############################
CloudWatchAlarmDuration:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: true
AlarmName: ${param:lambda-duration-alarm-name}
AlarmDescription: !Sub '${self:service} - Alarm for Lambda function duration'
AlarmActions:
- !Ref CelosiaSNSTopic
ComparisonOperator: ${param:comparison-operator-duration-alarm}
MetricName: "Duration"
Namespace: AWS/Lambda
Statistic: "Average"
Threshold: ${param:threshold-lambda-duration-alarm}
DatapointsToAlarm: ${param:datapoints-to-alarm-duration-lambda}
EvaluationPeriods: ${param:evaluation-period-duration-lambda}
Dimensions:
- Name: "FunctionName"
Value: ${self:custom.lambda-function}
Period: ${param:alarm-period-in-seconds-duration-lambda}
TreatMissingData: notBreaching
###################################### 5. Lambda Concurrent Executions CW Alarm ##################
CloudWatchAlarmConcurrentExecutions:
Type: 'AWS::CloudWatch::Alarm'
Properties:
ActionsEnabled: true
AlarmName: ${param:lambda-conc-execution-alarm-name}
AlarmDescription: !Sub '${self:service} - Alarm for Lambda concurrent executions'
AlarmActions:
- !Ref CelosiaSNSTopic
ComparisonOperator: ${param:comparison-operator-conc-execution-alarm}
MetricName: "ConcurrentExecutions"
Namespace: AWS/Lambda
Statistic: "Sum"
Threshold: ${param:threshold-lambda-conc-execution-alarm}
DatapointsToAlarm: ${param:datapoints-to-alarm-conc-execution-lambda}
EvaluationPeriods: ${param:evaluation-period-conc-execution-lambda}
Dimensions:
- Name: "FunctionName"
Value: ${self:custom.lambda-function}
Period: ${param:alarm-period-in-seconds-conc-execution-lambda}
TreatMissingData: notBreaching
################################ State Machine Execution Role #################################
StepFunctionExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: ${self:custom.sf-iam-role}
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- states.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaRole"
Policies:
- PolicyName: step-function-policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: AllowKMSEncryptDecrypt
Effect: Allow
Action:
- kms:Decrypt
- kms:Encrypt
- kms:GenerateDataKey
- kms:GenerateDataKeyPair
Resource: "*"
- Sid: AllowKSQSSendMessage
Effect: Allow
Action:
- sqs:SendMessage
- sqs:PurgeQueue
Resource: !GetAtt CelosiaSQSQueue.Arn
- Sid: AllowDynamoGetItem
Effect: Allow
Action:
- dynamodb:GetItem
Resource: !GetAtt CelosiaQuotesDynamoDbTable.Arn
Tags:
- Key: Project
Value: ${self:service}
- Key: Environment
Value: ${self:provider.stage}
Outputs:
CelosiaLambdaFunctionArn:
Description: Arn of the Lambda function
Value: !Sub 'arn:aws:lambda:${self:provider.region}:${AWS::AccountId}:function:${self:custom.lambda-function}'
CelosiaQuotesDynamoDbTableArn:
Description: Arn of the Celosia DynamoDB Table
Value: !GetAtt CelosiaQuotesDynamoDbTable.Arn
CelosiaSQSQueueArn:
Description: Arn of the Celosia SQS Queue
Value: !GetAtt CelosiaSQSQueue.Arn
CelosiaSQSQueueURL:
Description: URL of the Celosia SQS Queue
Value: !GetAtt CelosiaSQSQueue.QueueUrl
CelosiaSNSTopicArn:
Description: Arn of the Celosia SNS Topic
Value: !Ref CelosiaSNSTopic
CelosiaSNSSubscriptionArn:
Description: Arn of the Celosia SNS Subscription
Value: !Ref CelosiaSNSSubscription
CelosiaLambdaInvocationsAlarmArn:
Description: Arn of the Lambda Invocations Alarm
Value: !GetAtt CloudWatchAlarmInvocations.Arn
CelosiaLambdaErrorsAlarmArn:
Description: Arn of the Lambda Errors Alarm
Value: !GetAtt CloudWatchAlarmErrors.Arn
CloudWatchAlarmThrottlesArn:
Description: Arn of the Lambda Throttles Alarm
Value: !GetAtt CloudWatchAlarmThrottles.Arn
CloudWatchAlarmDurationArn:
Description: Arn of the Lambda Duration Alarm
Value: !GetAtt CloudWatchAlarmDuration.Arn
CloudWatchAlarmConcurrentExecutionsArn:
Description: Arn of the Lambda Concurrent Executions Alarm
Value: !GetAtt CloudWatchAlarmConcurrentExecutions.Arn
CelosiaStepFunctionRoleArn:
Description: Arn of the Celosia Step Function Exceution Role
Value: !GetAtt StepFunctionExecutionRole.Arn
functions:
insert-data-lambda:
handler: celosia_code.lambda_handler
name: ${self:custom.lambda-function}
description: "Celosia Lambda To Generate Some Random Data"
architecture: x86_64
# Reserve a maximum number of concurrent instances (default: account limit)
reservedConcurrency: 2
tags: # Function specific tags
ProjectName: ${self:service}
Environment: ${self:provider.stage}
stepFunctions:
stateMachines:
celosiaStateMachine:
name: ${self:custom.lambda-function}
role: !GetAtt StepFunctionExecutionRole.Arn
definition:
Comment: An example of the Amazon States Language using a choice state.
StartAt: Seed the DynamoDB Table
States:
Seed the DynamoDB Table:
Comment: Read the famous quotes from the input event and load them into a DynamoDB table.
Type: Task
Resource: !Sub "arn:aws:lambda:${self:provider.region}:${AWS::AccountId}:function:${self:custom.lambda-function}:$LATEST"
ResultPath: "$.List"
Next: For Loop Condition
For Loop Condition:
Type: Choice
Comment: Check if the element is not equal to DONE
Choices:
- Not:
Variable: $.List[0]
StringEquals: "DONE"
Next: Read Next Message from DynamoDB
Default: Succeed
Read Next Message from DynamoDB:
Type: Task
Comment: Read the item from DynamoDB table
Resource: arn:aws:states:::dynamodb:getItem
Parameters:
TableName: ${self:custom.dynamodb-table}
Key:
MessageId:
S.$: "$.List[0]"
ResultPath: "$.DynamoDB"
Next: Send Message to SQS
Send Message to SQS:
Type: Task
Comment: Send the message to the SQS Queue
Resource: "arn:aws:states:::sqs:sendMessage"
Parameters:
MessageBody.$: "$.DynamoDB.Item.Message.S"
QueueUrl: !GetAtt CelosiaSQSQueue.QueueUrl
ResultPath: "$.SQS"
Next: Pop Element from List
Pop Element from List:
Type: Pass
Comment: Pop the first element from the list
Parameters:
List.$: "$.List[1:]"
Next: For Loop Condition
Succeed:
Type: Succeed
dependsOn: StepFunctionExecutionRole
tags:
ProjectName: ${self:service}
Environment: ${self:provider.stage}