-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathtemplate.yaml
264 lines (250 loc) · 8.7 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
AWSTemplateFormatVersion: 2010-09-09
Description: >-
An example application with Powertools for AWS Lambda (TypeScript).
# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform: AWS::Serverless-2016-10-31
# Global configuration that all Functions inherit
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html
Globals:
Function:
Runtime: nodejs22.x
Tracing: Active
Architectures:
- arm64
MemorySize: 128
Timeout: 30
Environment:
Variables:
NODE_OPTIONS: '--enable-source-maps' # see https://docs.aws.amazon.com/lambda/latest/dg/typescript-exceptions.html
# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
# Items table
itemsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: powertools-example-items
TableClass: STANDARD_INFREQUENT_ACCESS
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
StreamSpecification:
StreamViewType: NEW_IMAGE # we use the stream to trigger the processItemsStreamFn function
# Idempotency table
idempotencyTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: powertools-example-idempotency
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
TimeToLiveSpecification:
AttributeName: expiration
Enabled: true
# We store the idempotency table name in a SSM Parameter to simulate a potential cross-stack reference.
# This is not strictly necessary in this example, but it's a good way of showing how to use SSM parameters
# and retrieve them using Powertools.
idempotencyTableNameParam:
Type: AWS::SSM::Parameter
Properties:
Name: /items-store/idempotency-table-name
Type: String
Value: !Ref idempotencyTable
# In this example, we use ESM and bundle all the dependencies including the AWS SDK.
# Because we are using ESM and tree shaking, we create an optimized bundle.
putItemFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: powertools-example-put-item
Handler: functions/put-item.handler
Policies:
# Allow the function to read/write to the items table
- DynamoDBCrudPolicy:
TableName: !Ref itemsTable
# Allow the function to read/write to the idempotency table
- DynamoDBCrudPolicy:
TableName: !Ref idempotencyTable
# Allow the function to read the idempotency table name from SSM
- SSMParameterWithSlashPrefixReadPolicy:
ParameterName: !Ref idempotencyTableNameParam
Environment:
Variables:
TABLE_NAME: !Ref itemsTable
SSM_PARAMETER_NAME: !Ref idempotencyTableNameParam
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
LoggingConfig:
LogGroupName: !Sub "/aws/lambda/${putItemFunction}"
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: ES2022
Sourcemap: true
KeepNames: true
Format: esm
SourcesContent: true
MainFields: module,main
TreeShaking: true
Banner:
- js=import { createRequire } from "module";const require = createRequire(import.meta.url);
EntryPoints:
- functions/put-item.ts
# Log group for the putItemFunction with a retention of 1 day
putItemLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${putItemFunction}"
RetentionInDays: 1
# In this example, we use ESM with the Powertools Lambda Layer which includes Powertools
# as well as the AWS SDK, this is a convenient way to use Powertools in a centralized way across
# multiple functions.
getAllItemsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: functions/get-all-items.handler
Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
Policies:
# Allow the function to read to the items table
- DynamoDBReadPolicy:
TableName: !Ref itemsTable
Layers:
- !Sub arn:aws:lambda:${AWS::Region}:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:3
Environment:
Variables:
TABLE_NAME: !Ref itemsTable
Events:
Api:
Type: Api
Properties:
Path: /
Method: GET
LoggingConfig:
LogGroupName: !Sub "/aws/lambda/${getAllItemsFunction}"
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: ES2022
Sourcemap: true
KeepNames: true
Format: esm
SourcesContent: true
MainFields: module,main
TreeShaking: true
External:
- '@aws-sdk/*'
- '@aws-lambda-powertools/*'
Banner:
- js=import { createRequire } from "module";const require = createRequire(import.meta.url);
EntryPoints:
- functions/get-all-items.ts
# Log group for the getAllItemsFunction with a retention of 1 day
getAllItemsLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${getAllItemsFunction}"
RetentionInDays: 1
# In this example we emit a CommonJS (CJS) bundle and include all the dependencies in it.
getByIdFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: powertools-example-get-by-id
Handler: functions/get-by-id.handler
Policies:
# Allow the function to read to the items table
- DynamoDBReadPolicy:
TableName: !Ref itemsTable
Environment:
Variables:
TABLE_NAME: !Ref itemsTable
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
LoggingConfig:
LogGroupName: !Sub "/aws/lambda/${getByIdFunction}"
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: ES2022
Sourcemap: true
KeepNames: true
Format: cjs
SourcesContent: true
MainFields: main
EntryPoints:
- functions/get-by-id.ts
# Log group for the getByIdFunction with a retention of 1 day
getByIdLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${getByIdFunction}"
RetentionInDays: 1
# In this example we use the AWS Lambda Powertools Layer to include Powertools but
# we use the CommonJS (CJS) format for the function code.
processItemsStreamFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: powertools-example-get-by-id
Handler: functions/process-items-stream.handler
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt itemsTable.StreamArn
StartingPosition: LATEST
BatchSize: 100
FilterCriteria:
Filters:
- Pattern: "{\"eventName\":[\"INSERT\"],\"dynamodb\":{\"NewImage\":{\"id\":{\"S\":[{\"exists\":true}]},\"name\":{\"S\":[{\"exists\":true}]}}}}"
MaximumRetryAttempts: 3
FunctionResponseTypes:
- ReportBatchItemFailures
DestinationConfig:
OnFailure:
Destination: !GetAtt processItemsDeadLetterQueue.Arn
LoggingConfig:
LogGroupName: !Sub "/aws/lambda/${processItemsStreamFunction}"
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: ES2022
Sourcemap: true
KeepNames: true
Format: cjs
SourcesContent: true
MainFields: main
External:
- '@aws-sdk/*'
- '@aws-lambda-powertools/*'
EntryPoints:
- functions/process-items-stream.ts
# Log group for the getByIdFunction with a retention of 1 day
processItemsStreamLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${processItemsStreamFunction}"
RetentionInDays: 1
# Dead letter queue for items that failed to be processed by the processItemsStreamFunction
processItemsDeadLetterQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: powertools-example-dead-letter-queue