2

I'm trying to create a lambda function inside of the AWS Console that does a POST of a record to my DynamoDB table records.

I am currently getting a success message when I run the test as I haven't hooked up a trigger yet but the test message is coming back null and isn't posting anything to my table.

I've gone through the AWS SDK docs and haven't found what I'm looking for in terms of running the exports.handle needed for the lambda function to work. I see the code side Node.js without the exports.

I've tried setting recordId and recordAlbum like this,

let recordId = 1;
let recordAlbum = "Album";

and that just returns a structure error when testing.

Does anyone have any resources or experience with this? I'm playing around to build a serverless CRUD app. Any advise or resources would really help.

This is the code I am using in my AWS Lambda function.

let AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'us-east-1'});

// Create the DynamoDB service object
let ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
let recordId = {N: '001'};
let recordAlbum = {S: 'Album Here'}

exports.handler = async function(event, context) {

  let params = {
    TableName: 'TABLE_NAME',
    Item: {
      'recordId' : recordId,
      'album' : recordAlbum
    }
  };

  console.log('generating record ID', recordId);
  console.log('generating Album', recordAlbum);
  console.log('generated parmas', params);
    // Call DynamoDB to add the item to the table
    ddb.putItem(params, function(err, data) {
      if (err) {
        console.log("Error", err);
      } else {
        console.log("Success", data);
      }
    });   
} 

2 Answers 2

1

You've got an async handler so you should use promises rather than callbacks.

Try this:

exports.handler = async function(event, context) {

  let params = {
    TableName: 'TABLE_NAME',
    Item: {
      'recordId' : recordId,
      'album' : recordAlbum
    }
  };

  console.log('generating record ID', recordId);
  console.log('generating Album', recordAlbum);
  console.log('generated parmas', params);

  try {
    let result = await ddb.putItem(params).promise();
    console.log(result);
  } 
  catch(err)
  {
    console.error(err);
  }

} 

Here's a bit more about async/await on the AWS blog, and documentation on the promise() method in the AWS JS SDK.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that seems to get part of it. It's now { showing ResourceNotFoundException: Requested resource not found }. from the catch(err). I'll go through the async/await and docs you shared. Appreciate your help!
@CMiller it'd be great if you could mark bwet's answer as accepted because you're now running into a business logic problem
0

One answer to this question is found in this tutorial by following step 3 and modifying the information.

I changed the "Id" to "id" and changed exports.writeMovie to exports.handler

https://hackernoon.com/create-a-serverless-rest-api-with-node-js-aws-lambda-dynamodb-api-gateway-f08e7111fd16

It doesn't use the async and await and is a touch outdated but it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.