-
Notifications
You must be signed in to change notification settings - Fork 2
/
template-with-kinesis.yaml
466 lines (413 loc) · 13.6 KB
/
template-with-kinesis.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
DevOpsGuruDemoProductsAPi
Sample SAM Template for DevOpsGuruDemoProductsAPi
Parameters:
Stage:
Type: String
Default: prod
KinesisStreamName:
Type: String
Default: orderedProductDataStream
Description: name for the Kinesis Data Stream.
VpcId:
Type: String
Default: vpc-950cd6fd
Description: VpcId of your existing Virtual Private Cloud (VPC)
Subnets:
Type: CommaDelimitedList
Default: subnet-0787be4d, subnet-88dc46e0
Description: The list of SubnetIds, for at least two Availability Zones in the
region in your Virtual Private Cloud (VPC)
Globals:
Function:
CodeUri: target/amazon-devops-guru-workshop-source-code-1.0.0-SNAPSHOT.jar
Runtime: java21
Timeout: 30
MemorySize: 1024
Environment:
Variables:
JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
PRODUCT_TABLE_NAME: !Ref ProductsTable
INVOCATION_QUEUE_URL: !Ref ProductCreatedQueue
#INVOCATION_TOPIC_URL: !Ref TargetTopic
#STATE_MACHINE_ARN: !Ref CreatedProductEmailNotificationSM
#DB_ENDPOINT: !GetAtt MyDB.Endpoint.Address
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref Stage
Name: DevOpsGuruDemoProductsAPI
Description: Api for DevOpsGuruDemoProductsAPI
Auth:
ApiKeyRequired: true # sets for all methods
MyApiUsagePlan:
Type: AWS::ApiGateway::UsagePlan
DependsOn:
- MyApiStage
Properties:
ApiStages:
- ApiId: !Ref MyApi
Stage: !Ref Stage
UsagePlanName: DevOpsGuruDemoProductsAPIUsagePlan
Description: Usage plan for DevOpsGuruDemoProductsAPI
Quota:
Limit: 2000000
Period: DAY
Throttle:
RateLimit: 50000
BurstLimit: 50000
MyApiKey:
Type: AWS::ApiGateway::ApiKey
DependsOn:
- MyApiStage
Properties:
Name: "DevOpsGuruDemoProductsApiKey"
Description: "DevOpsGuruDemoProducts API Key"
Enabled: true
GenerateDistinctId: false
Value: a6ZbcDefQW12BN56WEN7
StageKeys:
- RestApiId: !Ref MyApi
StageName: !Ref Stage
MyApiUsagePlanKey:
Type: "AWS::ApiGateway::UsagePlanKey"
DependsOn:
- MyApi
Properties:
KeyId: !Ref MyApiKey
KeyType: API_KEY
UsagePlanId: !Ref MyApiUsagePlan
GetProductByIdFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: GetProductById
Handler: software.amazonaws.example.product.handler.GetProductByIdHandler::handleRequest
Environment:
Variables:
INVOCATION_TOPIC_URL: !Ref TargetTopic
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
- SNSPublishMessagePolicy:
TopicName: !GetAtt TargetTopic.TopicName
Events:
GetRequestById:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products/{id}
Method: get
GetProductByIdFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductByIdFunction}"
RetentionInDays: 7
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: software.amazonaws.example.product.handler.GetAllProductsHandler::handleRequest
FunctionName: GetProducts
Runtime: java11
MemorySize: 128
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
Events:
GetRequest:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products
Method: get
GetProductsFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductsFunction}"
RetentionInDays: 7
PutProductFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: PutProduct
Handler: software.amazonaws.example.product.handler.CreateProductHandler::handleRequest
Environment:
Variables:
JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
PRODUCT_TABLE_NAME: !Ref ProductsTable
INVOCATION_QUEUE_URL: !Ref ProductCreatedQueue
#INVOCATION_TOPIC_URL: !Ref TargetTopic
STATE_MACHINE_ARN: !Ref CreatedProductEmailNotificationSM
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ProductsTable
- SQSSendMessagePolicy:
QueueName: !GetAtt ProductCreatedQueue.QueueName
- StepFunctionsExecutionPolicy:
StateMachineName: !GetAtt CreatedProductEmailNotificationSM.Name
- KinesisCrudPolicy:
StreamName: !Ref KinesisStreamName
#- SNSPublishMessagePolicy:
#TopicName: !GetAtt TargetTopic.TopicName
Events:
PutRequest:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products
Method: put
PutProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${PutProductFunction}"
RetentionInDays: 7
DeleteProductFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: DeleteProduct
Handler: software.amazonaws.example.product.handler.DeleteProductByIdHandler::handleRequest
MemorySize: 512
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ProductsTable
Events:
PutRequest:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /products/{id}
Method: delete
DeleteProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${DeleteProductFunction}"
RetentionInDays: 7
OnFailureQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "on-failure-queue"
MessageRetentionPeriod : 7200
ProductCreatedQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "new-product-created-queue"
TargetTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: devops-guru-workshop-target-topic
TargetTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref TargetTopic
#Protocol: lambda
#Endpoint: !GetAtt CreatedProductSNSNotificationFunction.Arn
#RawMessageDelivery: true
RedrivePolicy: !Sub '{"deadLetterTargetArn": "${OnFailureQueue.Arn}"}'
Endpoint: !GetAtt ProductCreatedQueue.Arn
Protocol: sqs
# Provide permission for SNS to invoke the Lambda function
#TopicConsumerInvokeFunctionPermission:
#Type: 'AWS::Lambda::Permission'
#Properties:
#Action: 'lambda:InvokeFunction'
#FunctionName: !Ref CreatedProductSNSNotificationFunction
#Principal: sns.amazonaws.com
# Allow Lambda to send messages to SQS
#LambdaToQueuePolicy:
#Type: AWS::SQS::QueuePolicy
#Properties:
#PolicyDocument:
#Statement:
#- Effect: Allow
#Principal:
#Service: lambda.amazonaws.com
#Action: SQS:SendMessage
#Resource: !GetAtt OnFailureQueue.Arn
#Queues:
#- !Ref OnFailureQueue
# Policy allows SNS to publish to this SQS queue
#SnsToSqsPolicy:
#Type: AWS::SQS::QueuePolicy
#Properties:
#PolicyDocument:
#Version: "2012-10-17"
#Statement:
#- Sid: "Allow SNS publish to SQS"
#Effect: Allow
#Principal:
#Service: "sns.amazonaws.com"
#Resource: !GetAtt ProductCreatedQueue.Arn
#Action: SQS:SendMessage
#Condition:
#ArnEquals:
#aws:SourceArn: !Ref TargetTopic
#Queues:
#- Ref: ProductCreatedQueue
#More info about SQS Queue Policy Resource: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-policy.html
OnFailureQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref OnFailureQueue
PolicyDocument:
Statement:
Effect: Allow
Principal: '*'
Action: sqs:SendMessage
Resource: '*'
Condition:
ArnEquals:
aws:SourceArn: !Ref TargetTopic
ProductCreatedEventFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: CreatedProduct
Handler: software.amazonaws.example.sqs.CreatedProductFunction::handleRequest
Timeout: 20
Events:
SQSQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt ProductCreatedQueue.Arn
CreatedProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${ProductCreatedEventFunction}"
RetentionInDays: 7
CreatedProductSNSNotificationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: CreatedProductNotification
Handler: software.amazonaws.example.sns.CreatedProductNotificationFunction::handleRequest
Events:
SNSEvent:
Type: SNS
Properties:
Topic: !Ref TargetTopic
CreatedProductSNSNotificationFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${CreatedProductSNSNotificationFunction}"
RetentionInDays: 7
CreatedProductSFnEmailNotificationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: CreatedProductSFnEmailNotification
Handler: software.amazonaws.example.stepfunction.CreatedProductSFnEmailNotificationFunction::handleRequest
CreatedProductSFnEmailNotificationFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${CreatedProductSFnEmailNotificationFunction}"
RetentionInDays: 7
CreatedProductEmailNotificationSM:
Type: AWS::Serverless::StateMachine
Properties:
Definition:
StartAt: CreatedProductTask
States:
CreatedProductTask:
Type: Task
Resource: !GetAtt CreatedProductSFnEmailNotificationFunction.Arn
"Retry": [
{
"ErrorEquals": [States.ALL],
"IntervalSeconds": 10,
"MaxAttempts": 50,
"BackoffRate": 1
}
]
End: true
Policies:
- LambdaInvokePolicy:
FunctionName:
!Ref CreatedProductSFnEmailNotificationFunction
# Define an AWS Kinesis Data Stream
KinesisStream:
Type: AWS::Kinesis::Stream
Properties:
Name: !Ref KinesisStreamName
#ShardCount: 1
StreamModeDetails:
StreamMode: ON_DEMAND
# Define a Lambda function as an application consumer
OrderedProductFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: OrderedProduct
Handler: software.amazonaws.example.kinesis.OrderedProductFunction::handleRequest
Timeout: 20
Policies:
- KinesisStreamReadPolicy:
StreamName: !Ref KinesisStreamName
Events:
Stream:
Type: Kinesis
Properties:
Stream: !GetAtt KinesisStream.Arn
StartingPosition: LATEST
BatchSize: 50
MaximumRetryAttempts: 30
OrderedProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${OrderedProductFunction}"
RetentionInDays: 7
# Define a Lambda function as an application consumer
UpdatedProductFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: UpdatedProduct
Handler: software.amazonaws.example.dynamodb.streams.UpdatedProductFunction::handleRequest
Timeout: 20
Events:
DynamoStream:
Type: DynamoDB
Properties:
Stream: !GetAtt ProductsTable.StreamArn
DestinationConfig:
OnFailure:
Type: SQS
Destination: !GetAtt OnFailureQueue.Arn
StartingPosition: LATEST
BatchSize: 50
MaximumRetryAttempts: 5
MaximumRecordAgeInSeconds: 3600
UpdatedProductFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${UpdatedProductFunction}"
RetentionInDays: 7
ProductsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: "ProductsTable"
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
AttributeDefinitions:
- AttributeName: 'PK'
AttributeType: 'S'
KeySchema:
- AttributeName: 'PK'
KeyType: 'HASH'
#BillingMode: PAY_PER_REQUEST
ProvisionedThroughput:
ReadCapacityUnits: "3"
WriteCapacityUnits: "1"
StreamSpecification:
#StreamViewType: NEW_IMAGE
StreamViewType: NEW_AND_OLD_IMAGES
DevOpsGuruResource:
Type: AWS::DevOpsGuru::ResourceCollection
Properties:
ResourceCollectionFilter:
CloudFormation:
StackNames:
- !Sub ${AWS::StackName}
Outputs:
DevOpsGuruWorkshopApiEndpoint:
Description: "API Gateway endpoint URL for Prod stage for DevOpsGuru Workshop"
Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/prod/products"
StackName:
Description: "Created AWS CloudFormation Stack Name"
Value: !Sub ${AWS::StackName}