-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtemplate.yaml
245 lines (227 loc) · 7.19 KB
/
template.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Example project demoing the Batch Processing Utility in Powertools for AWS Lambda (.NET)
Globals:
Function:
Timeout: 20
Runtime: dotnet8
MemorySize: 1024
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: powertools-dotnet-sample-batch
POWERTOOLS_LOG_LEVEL: Debug
POWERTOOLS_LOGGER_CASE: PascalCase
POWERTOOLS_BATCH_ERROR_HANDLING_POLICY: DeriveFromEvent
POWERTOOLS_BATCH_MAX_DEGREE_OF_PARALLELISM: 1
POWERTOOLS_BATCH_PARALLEL_ENABLED : false
POWERTOOLS_BATCH_THROW_ON_FULL_BATCH_FAILURE: true
Resources:
# --------------
# KMS key for encrypted messages / records
CustomerKey:
Type: AWS::KMS::Key
Properties:
Description: KMS key for encrypted queues
Enabled: true
KeyPolicy:
Version: "2012-10-17"
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
Action: "kms:*"
Resource: "*"
- Sid: Allow AWS Lambda to use the key
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- kms:Decrypt
- kms:GenerateDataKey
Resource: "*"
CustomerKeyAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub alias/${AWS::StackName}-kms-key
TargetKeyId: !Ref CustomerKey
# --------------
# Batch Processing for SQS Queue
SqsDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
KmsMasterKeyId: !Ref CustomerKey
SqsQueue:
Type: AWS::SQS::Queue
Properties:
RedrivePolicy:
deadLetterTargetArn: !GetAtt SqsDeadLetterQueue.Arn
maxReceiveCount: 2
KmsMasterKeyId: !Ref CustomerKey
SqsBatchProcessorFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/HelloWorld/
Handler: HelloWorld::HelloWorld.Function::SqsHandlerUsingAttribute
Policies:
- Statement:
- Sid: DlqPermissions
Effect: Allow
Action:
- sqs:SendMessage
- sqs:SendMessageBatch
Resource: !GetAtt SqsDeadLetterQueue.Arn
- Sid: KmsKeyPermissions
Effect: Allow
Action:
- kms:Decrypt
- kms:GenerateDataKey
Resource: !GetAtt CustomerKey.Arn
Events:
SqsBatch:
Type: SQS
Properties:
BatchSize: 5
Enabled: true
FunctionResponseTypes:
- ReportBatchItemFailures
Queue: !GetAtt SqsQueue.Arn
SqsBatchProcessorFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${SqsBatchProcessorFunction}"
RetentionInDays: 7
# --------------
# Batch Processing for DynamoDb (DDB) Stream
DdbStreamDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
KmsMasterKeyId: !Ref CustomerKey
DdbTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
DdbStreamBatchProcessorFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/HelloWorld/
Handler: HelloWorld::HelloWorld.Function::DynamoDbStreamHandlerUsingAttribute
Policies:
- AWSLambdaDynamoDBExecutionRole
- Statement:
- Sid: DlqPermissions
Effect: Allow
Action:
- sqs:SendMessage
- sqs:SendMessageBatch
Resource: !GetAtt DdbStreamDeadLetterQueue.Arn
- Sid: KmsKeyPermissions
Effect: Allow
Action:
- kms:GenerateDataKey
Resource: !GetAtt CustomerKey.Arn
Events:
Stream:
Type: DynamoDB
Properties:
BatchSize: 5
BisectBatchOnFunctionError: true
DestinationConfig:
OnFailure:
Destination: !GetAtt DdbStreamDeadLetterQueue.Arn
Enabled: true
FunctionResponseTypes:
- ReportBatchItemFailures
MaximumRetryAttempts: 2
ParallelizationFactor: 1
StartingPosition: LATEST
Stream: !GetAtt DdbTable.StreamArn
DdbStreamBatchProcessorFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${DdbStreamBatchProcessorFunction}"
RetentionInDays: 7
# --------------
# Batch Processing for Kinesis Data Stream
KinesisStreamDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
KmsMasterKeyId: !Ref CustomerKey
KinesisStream:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1
StreamEncryption:
EncryptionType: KMS
KeyId: !Ref CustomerKey
KinesisStreamConsumer:
Type: AWS::Kinesis::StreamConsumer
Properties:
ConsumerName: powertools-dotnet-sample-batch-kds-consumer
StreamARN: !GetAtt KinesisStream.Arn
KinesisBatchProcessorFunction:
Type: AWS::Serverless::Function
Properties:
Policies:
- Statement:
- Sid: KinesisStreamConsumerPermissions
Effect: Allow
Action:
- kinesis:DescribeStreamConsumer
Resource:
- !GetAtt KinesisStreamConsumer.ConsumerARN
- Sid: DlqPermissions
Effect: Allow
Action:
- sqs:SendMessage
- sqs:SendMessageBatch
Resource: !GetAtt KinesisStreamDeadLetterQueue.Arn
- Sid: KmsKeyPermissions
Effect: Allow
Action:
- kms:Decrypt
- kms:GenerateDataKey
Resource: !GetAtt CustomerKey.Arn
CodeUri: ./src/HelloWorld/
Handler: HelloWorld::HelloWorld.Function::KinesisEventHandlerUsingAttribute
Events:
Kinesis:
Type: Kinesis
Properties:
BatchSize: 5
BisectBatchOnFunctionError: true
DestinationConfig:
OnFailure:
Destination: !GetAtt KinesisStreamDeadLetterQueue.Arn
Enabled: true
FunctionResponseTypes:
- ReportBatchItemFailures
MaximumRetryAttempts: 2
ParallelizationFactor: 1
StartingPosition: LATEST
Stream: !GetAtt KinesisStreamConsumer.ConsumerARN
KinesisBatchProcessorFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${KinesisBatchProcessorFunction}"
RetentionInDays: 7
Outputs:
SqsQueueUrl:
Description: "SQS Queue URL"
Value: !Ref SqsQueue
DdbTableName:
Description: "DynamoDB Table Name"
Value: !Ref DdbTable
KinesisStreamArn:
Description: "Kinesis Stream ARN"
Value: !GetAtt KinesisStream.Arn