Created
November 9, 2020 14:50
-
-
Save dgomesbr/a015c36f0242e36afd28fd0373697149 to your computer and use it in GitHub Desktop.
Sample write event to dynamoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
// SPDX-License-Identifier: MIT-0 | |
// default imports | |
const AWSXRay = require('aws-xray-sdk-core') | |
const AWS = AWSXRay.captureAWS(require('aws-sdk')) | |
const { metricScope, Unit } = require("aws-embedded-metrics") | |
const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }) | |
// environment variables | |
const { TABLE_NAME, REGION } = process.env | |
const options = { region: REGION } | |
AWS.config.update({ region: REGION }) | |
// create the DynamoDB DocumentClient with default options | |
const docClient = new AWS.DynamoDB.DocumentClient(options) | |
// HELPER: Creates a return with the body, plus CORS headers | |
const response = (statusCode, body, additionalHeaders) => ({ | |
statusCode, | |
body: JSON.stringify(body), | |
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', ...additionalHeaders }, | |
}) | |
// Validation of the request, if the body isn't null | |
function isValidRequest(context, event) { | |
return (event.body !== null) | |
} | |
// This is the function that adds the record to the DynamoDB, it also | |
// creates creation_date and lastupdate_date fields with Today's date | |
function addRecord(event) { | |
// auto generated date fields | |
let d = new Date() | |
let dISO = d.toISOString() | |
let auto_fields = { | |
"creation_date": dISO, | |
"lastupdate_date": dISO | |
} | |
//merge the json objects | |
let item_body = {...auto_fields, ...JSON.parse(event.body) } | |
console.log(item_body); | |
//final params to DynamoDB | |
const params = { | |
TableName: TABLE_NAME, | |
Item: item_body | |
} | |
return docClient.put(params) | |
} | |
// Lambda Handler, this is what's called during lambda execution | |
exports.addToDoItem = | |
metricScope(metrics => | |
async (event, context, callback) => { | |
metrics.setNamespace('MySampleApp') | |
metrics.putDimensions({ Service: "StoreOnDynamoDB" }) | |
metrics.setProperty("RequestId", context.requestId) | |
// validate the request | |
if (!isValidRequest(context, event)) { | |
metrics.putMetric("Error", 1, Unit.Count) | |
return response(400, { message: "Error: Invalid request" }) | |
} | |
// store on DynamoDB or return if fail, it also put metrics to enable us a better | |
// monitoring of our sample application | |
try { | |
let data = await addRecord(event).promise() | |
metrics.putMetric("Success", 1, Unit.Count) | |
return response(200, data) | |
} catch (err) { | |
metrics.putMetric("Error", 1, Unit.Count) | |
return response(400, { message: err.message }) | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment