简体   繁体   中英

Writing to DynamoDB from Lambda Function

Setup: I am trying to setup an Lambda function that is triggered by Web Sockets coming in from API Gateway and also an SNS topic that it is subscribed to. Based on the event passed in, I make the determination about which service initiated the call and handle things accordingly.

Issue: I'm having a real issue with understanding how to work with DynamoDB. When a web socket connection is established, I am inserting that connection id into Dynamo so that I can respond on it when a message comes in from SNS. For whatever reason, all my calls to Dynamo do not call their respective callback. I'm assuming they fail but it doesnt look like any errors are thrown. Is there something wrong with the way that I am calling the "put" command here?

My code at the top of my index.js is:

const aws = require("aws-sdk");
const tableName = process.env.TABLENAME;
var accessKeyId = process.env.DYNAMODB_ACCESS_KEY_ID;
var secretAccessKey = process.env.DYNAMODB_SECRET_ACCESS_KEY;

var documentClient = new aws.DynamoDB.DocumentClient({
    region: 'us-east-1',
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey
});

My method to handle the API Gateway call when it comes in is:

const {
    requestContext: {connectionId, routeKey}
} = event;

if (routeKey === "$connect") {
    // handle new connection
    console.log("Adding Connection",  event);
            
    const putParams = {
        Item: {
            'ConnectionId': connectionId
        },
        TableName: tableName,
    };

    console.log("put params", putParams);
    
    documentClient.put(putParams, function(err, data) {
        if (err) {
            console.log(err, err.stack);
            return {
                response: 400,
                body: "Error " + JSON.stringify(err)
            };

        }
        else {
            console.log("Response Received: ", data);
            return {
                response: 200,
                body: "Connected - " + connectionId
            };
        }
    });
}
else  if (routeKey === "$disconnect") {
    // handle disconnection
    console.log("Disconnected");
}
else { 
     // $default handler
    console.log("Default");
    return {
        response: 200,
        body: "default"
    };
}

When I run a test request through, the console logs show the proper values for all my environment variables (I granted the Lambda function full access to dynamo db so I dont think my access key values are necessary but I put them in there just because I'm trying everything at this point) but nothing is logged after my documentClient.put call so essentially, I think, the callback to the put command is never called.

My DynamoDB has a partition key of: ConnectionId (it's the only value I had in there) with no SortKey.

I'm new to all of this so any help that could point me in the right direction would be fantastic.

Thank you in advance!

Your issue sounds similar to problems that arise due to the the asynchronous nature of the put operation.

Bit of a "hail mary" here, but have you tried switching the callback version of documentClient.put with the async/await version?

It would look something like this:


try{
  await documentClient.put(putParams).promise()
  console.log("All done!")
}catch(error){
  console.log(error)
}

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