-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathtemplate.yaml
284 lines (266 loc) · 10.2 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html
# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
An example application with Powertools for AWS Lambda (TypeScript) instrumented. Its purpose is to demonstrate how to use the Powertools for AWS Lambda (TypeScript) with AWS SAM. The application an API with contains 3 endpoints (get all items, get an item by ids, put an item). Each Lambda function utilises Logger, Metrics, and Tracers.
# 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
Parameters:
apiGatewayName:
Type: String
Default: uuid-api
apiGatewayStageName:
Type: String
AllowedPattern: '[a-z0-9]+'
Default: dev
apiGatewayHTTPMethod:
Type: String
Default: GET
lambdaFunctionName:
Type: String
AllowedPattern: '[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+'
Default: uuid-lambda
# 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: nodejs18.x
Architectures:
- x86_64
MemorySize: 128
Timeout: 100
# 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:
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# This is a Lambda function config associated with the source code: get-all-items.js
getAllItemsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/get-all-items.handler
Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable
- DynamoDBReadPolicy:
TableName: !Ref SampleTable
- SSMParameterWithSlashPrefixReadPolicy:
ParameterName: !Ref uuidApiUrlParameter
Tracing: Active
Environment:
Variables:
# Make table name accessible as environment variable from function code during execution
NODE_OPTIONS: '--enable-source-maps' # see https://docs.aws.amazon.com/lambda/latest/dg/typescript-exceptions.html
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1 # see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-reusing-connections.html
SAMPLE_TABLE: !Ref SampleTable
POWERTOOLS_SERVICE_NAME: items-store
POWERTOOLS_METRICS_NAMESPACE: PowertoolsSAMExample
LOG_LEVEL: Debug
Events:
Api:
Type: Api
Properties:
Path: /
Method: GET
Metadata:
# Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: 'ES2020'
Sourcemap: true
EntryPoints:
- src/get-all-items.ts
# This is a Lambda function config associated with the source code: get-by-id.js
getByIdFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/get-by-id.handler
Description: A simple example includes a HTTP get method to get one item by id from a DynamoDB table.
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable
- DynamoDBReadPolicy:
TableName:
!Ref SampleTable
# add ssm:getParameter permission to the function
- SSMParameterWithSlashPrefixReadPolicy:
ParameterName: !Ref uuidApiUrlParameter
Tracing: Active
Environment:
Variables:
# Make table name accessible as environment variable from function code during execution
NODE_OPTIONS: '--enable-source-maps' # see https://docs.aws.amazon.com/lambda/latest/dg/typescript-exceptions.html
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1 # see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-reusing-connections.html
SAMPLE_TABLE: !Ref SampleTable
POWERTOOLS_SERVICE_NAME: items-store
POWERTOOLS_METRICS_NAMESPACE: PowertoolsSAMExample
LOG_LEVEL: Debug
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
Metadata:
# Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: 'ES2020'
Sourcemap: true
EntryPoints:
- src/get-by-id.ts
# This is a Lambda function config associated with the source code: get-uuid.js
getUuidFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/get-uuid.handler
Description: A simple example includes a HTTP get method to get a UUID.
Tracing: Active
FunctionName: !Ref lambdaFunctionName
Role: !GetAtt lambdaIAMRole.Arn
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: items-store
POWERTOOLS_METRICS_NAMESPACE: PowertoolsSAMExample
LOG_LEVEL: Debug
# add ssm:getParameter permission to the function
Policies:
- SSMParameterWithSlashPrefixReadPolicy:
ParameterName: !Ref uuidApiUrlParameter
Events:
Api:
Type: Api
Properties:
Path: /
Method: GET
RestApiId: !Ref apiGateway
Metadata:
# Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: 'ES2020'
Sourcemap: true
EntryPoints:
- src/get-uuid.ts
# create ssm parameter with the value of the uuid api url
uuidApiUrlParameter:
Type: AWS::SSM::Parameter
Properties:
Name: /app/uuid-api-url
Type: String
Description: 'Example parameter for UUID API URL'
Value: !Sub 'https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/${apiGatewayStageName}/'
# This is a Lambda function config associated with the source code: put-item.js
putItemFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/put-item.handler
Description: A simple example includes a HTTP post method to add one item to a DynamoDB table.
Policies:
# Give Create/Read/Update/Delete Permissions to the SampleTable
- DynamoDBCrudPolicy:
TableName: !Ref SampleTable
- SSMParameterWithSlashPrefixReadPolicy:
ParameterName: !Ref uuidApiUrlParameter
Tracing: Active
Environment:
Variables:
# Make table name accessible as environment variable from function code during execution
NODE_OPTIONS: '--enable-source-maps' # see https://docs.aws.amazon.com/lambda/latest/dg/typescript-exceptions.html
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1 # see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-reusing-connections.html
SAMPLE_TABLE: !Ref SampleTable
POWERTOOLS_SERVICE_NAME: items-store
POWERTOOLS_METRICS_NAMESPACE: PowertoolsSAMExample
LOG_LEVEL: Debug
Events:
Api:
Type: Api
Properties:
Path: /
Method: POST
Metadata:
# Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
Target: 'ES2020'
Sourcemap: true
EntryPoints:
- src/put-item.ts
# DynamoDB table to store item: {id: <ID>, name: <NAME>}
SampleTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
apiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub ${AWS::StackName}-uuid-api
Description: 'Example API for UUID generation'
apiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- apiGatewayRootMethod
Properties:
RestApiId: !Ref apiGateway
StageName: !Ref apiGatewayStageName
apiGatewayRootMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE # NOSONAR
HttpMethod: !Ref apiGatewayHTTPMethod
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt getUuidFunction.Arn
ResourceId: !GetAtt apiGateway.RootResourceId
RestApiId: !Ref apiGateway
lambdaApiGatewayInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt getUuidFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiGateway}/${apiGatewayStageName}/*/
lambdaIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*
PolicyName: lambda
lambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${lambdaFunctionName}
RetentionInDays: 1
Outputs:
UuidWebEndpoint:
Description: 'API Gateway endpoint URL for UUID endpoint'
Value: !Sub 'https://${apiGateway}.execute-api.${AWS::Region}.amazonaws.com/dev/'