0
votes

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

3
Can you make sure that you actually have a table called User in the us-west-2 region?Robert Kossendey
Can you double check that your table actually exists in us-west-2?Marcin
Yes the table exists in us-west-2Proteeti Prova

3 Answers

0
votes

So the ResourceNotFoundException means this, according to the AWS Doc:

enter image description here

You should double check if your table exists and if you you are using the right access key. In addition, why are you creating your DynamoDB object with us-east-2 as a region just to change it in your lambda to us-west-2. Try initiating the instance with the correct region right away.

0
votes

Could you try with this code ?

const aws = require('aws-sdk');
aws.config.update({region: 'us-east-2'});
const ddb = new aws.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});

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:  event.request.userAttributes.user_name,
                first_name:  event.request.userAttributes.first_name,
                last_name:  event.request.userAttributes.last_name,
                email_address:  event.request.userAttributes.email_address,
                mobile_phone:  event.request.userAttributes.mobile_phone,
                password:  event.request.userAttributes.password,
                referred_by:  event.request.userAttributes.referred_by,
                createdAt:  date.toISOString()
            },
            TableName: tableName
        };


        try {
            await ddb.put(ddbParams).promise()
            console.log("Success");
        } catch (err) {
            console.log("Error", err);
        }

        console.log("Success: Everything executed correctly");
        context.done(null, event);


};

Issue seems coming for the putItem and also I advise you to use the documentClient with last version for more simplicity

0
votes

Your problem is firstly, change your table name look your db name from the table section. Then enter the functions from aws console find your lambda function, basic settings > View your_lambda_function_name there are permissions and attach policy, create new policy for permissions give full access for dynamodb.