-
Notifications
You must be signed in to change notification settings - Fork 2
/
template-without-kinesis-with-RDS-old.yaml
475 lines (420 loc) · 13.6 KB
/
template-without-kinesis-with-RDS-old.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
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:
Tracing: Active
CodeUri: target/amazon-devops-guru-workshop-source-code-1.0.0-SNAPSHOT.jar
Runtime: java21
Timeout: 30
MemorySize: 512
Environment:
Variables:
JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
PRODUCT_TABLE_NAME: !Ref ProductsTable
INVOCATION_QUEUE_URL: !Ref SimpleQueue
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
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ProductsTable
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
GetProductByIdRDSFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: GetProductByIdRDS
Handler: software.amazonaws.example.product.handler.GetProductByIdRDSHandler::handleRequest
Environment:
Variables:
JAVA_TOOL_OPTIONS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
#DB_ENDPOINT: !GetAtt MyDB.Endpoint.Address
Policies:
- VPCAccessPolicy: {}
VpcConfig:
SecurityGroupIds:
- Fn::GetAtt: LambdaSecurityGroup.GroupId
SubnetIds: !Ref Subnets
Events:
GetRequestById:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /productsFromRDS/{id}
Method: get
GetProductByIdRDSFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${GetProductByIdRDSFunction}"
RetentionInDays: 7
GetProductsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: GetProducts
Handler: software.amazonaws.example.product.handler.GetAllProductsHandler::handleRequest
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 SimpleQueue
INVOCATION_TOPIC_URL: !Ref TargetTopic
STATE_MACHINE_ARN: !Ref CreatedProductEmailNotificationSM
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref ProductsTable
- SQSSendMessagePolicy:
QueueName: !GetAtt SimpleQueue.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/{id}
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
SimpleQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: "new-product-created"
TargetTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: devops-guru-workshop-target-topic
Subscription:
- Endpoint: "tipopix306@v2ssr.com"
Protocol: email
ProductCreatedEventFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: CreatedProduct
Handler: software.amazonaws.example.sqs.CreatedProductFunction::handleRequest
Timeout: 20
Events:
SQSQueueEvent:
Type: SQS
Properties:
Queue: !GetAtt SimpleQueue.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": 30,
"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: 100
#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
StartingPosition: LATEST
BatchSize: 50
MaximumRetryAttempts: 100
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
MyDBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: Subnets available for the RDS DB Instance
SubnetIds:
Ref: Subnets
myVPCSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for RDS DB Instance.
VpcId:
Ref: VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '5432'
ToPort: '5432'
SourceSecurityGroupId:
Ref: LambdaSecurityGroup
LambdaSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: SecurityGroup for Serverless Functions
VpcId:
Ref: VpcId
MyDB:
Type: AWS::RDS::DBInstance
Properties:
DBName: DevOpsGuruWorkshop
AllocatedStorage: 5
DBInstanceClass: db.t3.micro
Engine: postgres
EngineVersion: '14.6'
MasterUsername: postgres
MasterUserPassword: postgres
DBSubnetGroupName:
Ref: MyDBSubnetGroup
VPCSecurityGroups:
- Ref: myVPCSecurityGroup
#StaticAssetsBucket:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: !Sub ${AWS::AccountId}-devops-guru-workshop
# AccessControl: PublicRead
#PublicBucketPolicy:
# Type: AWS::S3::BucketPolicy
# Properties:
# Bucket: !Ref StaticAssetsBucket
# PolicyDocument:
# Version: "2012-10-17"
# Statement:
# - Effect: Allow
# Action:
# - "s3:GetObject"
# Resource:
# - !Sub "${StaticAssetsBucket.Arn}/*"
#Principal: "*"
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}