Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node lambda not writing to DynamoDB and no error

var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'})
AWS.config.update({region: 'eu-west-1'})
var docClient = new AWS.DynamoDB.DocumentClient()
  const params = {
    TableName: 'TC_QUESTIONS',
    Item: {
      'questionId' : {S: '001'},
      'questionText' : {S: 'Richard Roe'}
    }
  }

  var putItemPromise = docClient.put(params).promise()
  putItemPromise.then(function(data) {
    console.log("Added item:", JSON.stringify(data, null, 2));
  }).catch(function(err) {
    console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
  });

  await putItemPromise

No error is returned. No success callback. When I use the wrong column name I get an error back from Dynamo. I've tried running the it locally and on lambda. It executes, and exits. No rows get added to the Table. What am I doing wrong?

Edit: Here is my CF for the DynamoDB table:

QuestionsTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: 'TC_QUESTIONS'
    AttributeDefinitions:
      - AttributeName: questionId
        AttributeType: S
      - AttributeName: questionText
        AttributeType: S
    KeySchema:
      - AttributeName: questionId
        KeyType: HASH
      - AttributeName: questionText
        KeyType: RANGE
    ProvisionedThroughput: 
      ReadCapacityUnits: "5"
      WriteCapacityUnits: "5"
like image 370
Bobby Avatar asked Mar 08 '19 15:03

Bobby


People also ask

How do I create a lambda function in DynamoDB?

We have successfully created our DynamoDB table, lets now make our lambda function. On the AWS Lamba dashboard click “Create function”. Select “Author from scratch” and name the function “WriteMessage”, make sure Node.js be selected for the runtime.

How to use AWS Lambda function readmessage in DynamoDB?

Navigate to you DynamoDB and go to the items tab, here you will see your message from the AWS Lambda Function. Repeat the steps above to create a function and name this lambda function ReadMessage.

How many records can I send to Lambda from DynamoDB?

DynamoDB table – The DynamoDB table to read records from. Batch size – The number of records to send to the function in each batch, up to 10,000. Lambda passes all of the records in the batch to the function in a single call, as long as the total size of the events doesn't exceed the payload limit for synchronous invocation (6 MB).

How does DynamoDB polling work with Lambda?

Lambda polls shards in your DynamoDB stream for records at a base rate of 4 times per second. When records are available, Lambda invokes your function and waits for the result. If processing succeeds, Lambda resumes polling until it receives more records.


1 Answers

'use strict';
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});

exports.handler = async (event) => {

    const params = {
      TableName: 'TC_QUESTIONS',
      Item: {
        'questionId': '001',
        'questionText' : 'Richard Roe'
      }
    }
  
    try {
      await docClient.put(params).promise()
    } catch (e) {
      console.log(e.message)
    }
};

If your table exists and questionId is your partition key, the code above works.

I think you were mixing things up a little bit, because since you're using DocClient you don't need to specify DynamoDB's types. This was the problem on your code. Just specify the raw values (like I did above, passing the String itself) and it will work.

like image 139
Thales Minussi Avatar answered Oct 11 '22 23:10

Thales Minussi