-
Notifications
You must be signed in to change notification settings - Fork 102
/
app.ts
39 lines (34 loc) · 1.11 KB
/
app.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
//Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//SPDX-License-Identifier: MIT-0
/**
* Lambda Handler for the typescript pattern lambda-handler-dynamodb
* This handler put a new item in the DynamoDB table using AWS SDK.
* The DynamoDB Table used is passed as an environment variable "DatabaseTable"
*/
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { DynamoDBStreamEvent } from "aws-lambda/trigger/dynamodb-stream";
import moment = require('moment');
const dynamodb = new DynamoDBClient({});
const ddb = DynamoDBDocumentClient.from(dynamodb);
export const lambdaHandler = async ( event: DynamoDBStreamEvent ) => {
try {
const params = {
TableName : process.env.DatabaseTable,
Item: {
ID: event.Records[0].eventID,
created: moment().format('YYYYMMDD-hhmmss'),
metadata: JSON.stringify(event),
}
}
await ddb.send(new PutCommand(params));
}
catch (err) {
console.log(err);
return err;
}
return {
statusCode: 200,
body: 'OK!',
};
}