forked from nrdio/sns-lambda-dynamodb-cloudformation-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (26 loc) · 844 Bytes
/
index.js
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
const AWS = require("aws-sdk");
const uuid = require("uuid");
AWS.config.update({region: "us-west-2"});
// Create DynamoDB document client
const dynamoDb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
try {
// Extract message
const message = JSON.parse(event.Records[0].Sns.Message);
// Prepare account item
const accountItem = {
TableName: "Account",
Item: {
id: uuid.v1(),
name: message.name,
iban: message.iban
}
};
// insert account in DynamoDB
await dynamoDb.put(accountItem).promise();
return "Registered new account with id " + accountItem.Item.id;
} catch (err) {
console.log(err);
throw err;
}
}