'Add a string to my Lambda function variable
I am trying to implement a DynamoDB single table design. I have a Lambda that's triggered by a new Cognito user with a Post Confirmation Trigger which sends the new User to my DynamoDB Table.
In the 'Item' I would like to add a string that goes before the User Sub ID which is saved to 'PK' but can't work out how to append this to the beginning.
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({apiVersion: '2012-10-08'});
exports.handler = async (event, context) => {
console.log(event);
let date = new Date();
const tableName = 'Kaleido';
const region = 'ap-southeast-2';
console.log("table=" + tableName + " -- region=" + region);
aws.config.update({region: region});
// If the required parameters are present, proceed
if (event.request.userAttributes.sub) {
// -- Write data to DDB
let ddbParams = {
Item: {
//Add 'User#' below
"PK": {S: event.request.userAttributes.sub},
"SK": {S: event.userName},
"Type": "User",
"createdAt": {S: date.toISOString()},
},
TableName: 'Kaleido'
};
// Call DynamoDB
try {
await ddb.putItem(ddbParams).promise();
console.log("Success");
} catch (err) {
console.log("Error", err);
context.done(null, event);
}
console.log("Success: Everything executed correctly");
context.done(null, event);
} else {
// Nothing to do, the user's email ID is unknown
console.log("Error: Nothing was written to DDB or SQS");
context.done(null, event);
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
