简体   繁体   中英

AWS Node.js Lambda POST Function in AWS Console

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);
      }
    });   
} 

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM