-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.lambda.ts
72 lines (68 loc) · 2.21 KB
/
create.lambda.ts
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
/* eslint-disable import/no-extraneous-dependencies */
import { LambdaInterface } from '@aws-lambda-powertools/commons';
import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';
import {
ConditionalCheckFailedException,
DynamoDBClient,
PutItemCommand,
} from '@aws-sdk/client-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import {
APIGatewayProxyEventV2,
APIGatewayProxyResult,
Context,
} from 'aws-lambda';
const metrics = new Metrics({
defaultDimensions: {
environment: process.env.ENV ?? 'dev',
},
});
const tracer = new Tracer();
const logger = new Logger();
const client = tracer.captureAWSv3Client(new DynamoDBClient({}));
class LambdaHandler implements LambdaInterface {
@tracer.captureLambdaHandler()
@logger.injectLambdaContext()
@metrics.logMetrics({ captureColdStartMetric: true })
public async handler(
event: APIGatewayProxyEventV2,
_context: Context,
): Promise<APIGatewayProxyResult> {
tracer.getSegment();
const item = marshall({
...JSON.parse(event.body!),
createdAt: new Date().toISOString(),
});
const tableName = process.env.TABLE_NAME;
if (!tableName) {
throw new Error('TABLE_NAME env var not provided!');
}
const putItem = new PutItemCommand({
TableName: tableName,
ConditionExpression: 'attribute_not_exists(pk)',
Item: item,
});
try {
await client.send(putItem);
metrics.addMetric('createPostSuccess', MetricUnits.Count, 1);
tracer.putAnnotation('createPostSuccess', true);
} catch (e) {
if (!(e instanceof ConditionalCheckFailedException)) {
tracer.putAnnotation('createPostFailure', false);
metrics.addMetric('createPostFailure', MetricUnits.Count, 1);
throw new Error(`failed creating post: ${e}`);
} else {
metrics.addMetric('createPostSuccess', MetricUnits.Count, 1);
tracer.putAnnotation('createPostSuccess', true);
}
}
return {
statusCode: 200,
body: 'OK',
};
}
}
const handlerClass = new LambdaHandler();
export const handler = handlerClass.handler.bind(handlerClass);