My task is to add New Cognito Users to DynamoDB Using Lambda. I have been provided with an existing AWS Cognito User pool. So far, I have written a lambda function & testing it with demo data so that it can save the data into DynamoDB. But while testing, it doesn't save my data into the Database. For guidance, I have been following this article. In the article it is said that upon testing, my DynamoDB Table should be populated.
My lambda function looks like this:
const aws = require('aws-sdk');
const ddb = new aws.DynamoDB.DocumentClient({region: 'us-east-2'});
exports.handler = async (event, context) => {
console.log(event);
let date = new Date();
const tableName = 'User';
const region = 'us-west-2';
console.log("table=" + tableName + " -- region=" + region);
aws.config.update({region});
let ddbParams = {
Item: {
'user_name': {S: event.request.userAttributes.user_name},
'first_name': {S: event.request.userAttributes.first_name},
'last_name': {S: event.request.userAttributes.last_name},
'email_address': {S: event.request.userAttributes.email_address},
'mobile_phone': {S: event.request.userAttributes.mobile_phone},
'password': {S: event.request.userAttributes.password},
'referred_by': {S: event.request.userAttributes.referred_by},
'createdAt': {S: date.toISOString()}
},
TableName: tableName
};
try {
await ddb.putItem(ddbParams).promise()
console.log("Success");
} catch (err) {
console.log("Error", err);
}
console.log("Success: Everything executed correctly");
context.done(null, event);
};
Sample input that I am testing with:
{
"userName": "Proteeti Prova",
"request": {
"userAttributes": {
"user_name": "Proteeti13",
"first_name": "Proteeti",
"last_name": "Prova",
"email_address": "[email protected]",
"mobile_phone": "7777777",
"password": "ironman",
"referred_by": "tony stark"
}
},
"response": {}
}
Log output
START RequestId: XXXXXXXXXXXXXXXXXXXXXXXX Version: $LATEST 2020-10-23T07:59:49.334Z XXXXXXXXXXXXXXXXXXXXX INFO Error { ResourceNotFoundException: Requested resource not found at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27) at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20) at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10) at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14) at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10) at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12) at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10 at Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9) at Request. (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12) at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18) message: 'Requested resource not found', code: 'ResourceNotFoundException', time: 2020-10-23T07:59:49.233Z,
requestId: 'XXXXXXXXXXXXXXXXXXXXXXXXX',
statusCode: 400, retryable: false, retryDelay: 37.83355253768394 } 2020-10-23T07:59:49.335Z XXXXXXXXXXXXXXXXXXX INFO Success: Everything executed correctly END RequestId: XXXXXXXXXXXXXXXXXXXXXXXXXXXX REPORT RequestId: XXXXXXXXXXXXXXXXXXXXXXXXXXXX Duration: 1203.41 ms Billed Duration: 1300 ms Memory Size: 128 MB Max Memory Used: 89 MB Init Duration: 391.58 ms
us-west-2
? – Marcin