0

I'm having some trouble with DynamoDB. I've set up my Lambda permissions for full CRUDL (administrator access, so no restrictions). The following is the handler, and it's based on the doca https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html

const uuidv4 = require("uuid/v4");
const services = require("./services/services");
var AWS = require("aws-sdk");
AWS.config.update({ region: "eu-west-2" });
var docClient = new AWS.DynamoDB.DocumentClient();
var tableName = "usersTable";

module.exports = {
  registerUser: async (event, context) => {
    const id = uuidv4();
    let body;
    if (event.body !== null && event.body !== undefined) {
      body = JSON.parse(event.body);
    }

    const isValid = await services.validateUser(body);

    if (isValid) {
      var params = {
        TableName: tableName,
        Item: {
          userId: "123abc",
          firstName: "finn",
          lastName: "murtons",
          email: "[email protected]",
          password: "secret"
        }
      };

      console.log("Adding a new item...");
      console.log(" Adding params", params);
      docClient.put(params, function(err, data) {
        if (err) {
          console.error(
            "Unable to add item. Error JSON:",
            JSON.stringify(err, null, 2)
          );
        } else {
          console.log("Added item:", JSON.stringify(data, null, 2));
        }
      });
    }
  },
  ... other functions

For this example, I'm hardcoding the params for clarity, but obviously I would usually get them from the event.body.

When I make a post request to this Lambda, I get a 502 error. Looking at the cloudwatch logs, it gets as far as:

INFO     Adding params { TableName: 'usersTable',
  Item:
   { userId: '123abc',
     firstName: 'finn',
     lastName: 'murtons',
     email: '[email protected]',
     password: 'secret' } }

Then there are no more logs after that. Ignore the isValid function, it's just checking that the request event body has the required fields.

Anybody have any ideas where I might be going wrong?

4
  • Have you configured your function to run inside a VPC? Commented Jan 24, 2020 at 22:29
  • I haven't, no, I haven't seen any mention of using a VPC in any of the docs or tutorials on Lambda/Dynamo serverless apps. Is that something that I need to do? Commented Jan 24, 2020 at 22:32
  • 1
    No, if you were using a VPC that would clue into the issue. You definitely shouldn't run it in a VPC unless required. Is there a timeout message or something in the logs? Commented Jan 24, 2020 at 22:33
  • Nope, no time out messages. the last message is the log of the params, then END RequestId: 99a5904e-c47e-4e24-8117-756daad2029d After that, nothing. Commented Jan 24, 2020 at 22:35

1 Answer 1

2

It's likely that the lambda is exiting before the DynamoDB call is made. You should make the call a Promise and await it:

await docClient.put(params).promise();
Sign up to request clarification or add additional context in comments.

3 Comments

That did the trick! I'm still not sure why though, I followed the docs pretty closely, why do you think that the lambda was exiting early?
You must be explicitly ending the function somewhere else in the part of the code you didn't include in your question.
It's not that the lambda is exiting early, it's exiting right when it should. When you create a callback, like you did, the code keeps running past that. You get to the end of your function and the lambda exits. When you do this is something like a browser it still has something running the JavaScript, so the callback fires and your code runs (technically the call the DynamoDB runs and then your code). But in a lambda Amazon is shutting it down as soon as the code exits. Think of it like running a kill on the container it's running it.

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.