简体   繁体   English

从 Lambda Function 写入 DynamoDB

[英]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.设置:我正在尝试设置一个 Lambda function,它由来自 API 网关的 Web Sockets 以及它订阅的 SNS 主题触发。 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.问题:我在理解如何使用 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.建立 web 套接字连接后,我将该连接 ID 插入 Dynamo,以便在收到来自 SNS 的消息时响应它。 For whatever reason, all my calls to Dynamo do not call their respective callback.无论出于何种原因,我对 Dynamo 的所有调用都不会调用它们各自的回调。 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?我在这里调用“put”命令的方式有问题吗?

My code at the top of my index.js is:我的 index.js 顶部的代码是:

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:我处理 API 网关调用的方法是:

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.当我运行测试请求时,控制台日志显示我所有环境变量的正确值(我授予 Lambda function 对 dynamo db 的完全访问权限,所以我认为我的访问键值不是必需的,但我把它们放在那里只是因为我在这一点上尝试了一切)但是在我的 documentClient.put 调用之后没有记录任何内容,我认为基本上,对 put 命令的回调从未被调用过。

My DynamoDB has a partition key of: ConnectionId (it's the only value I had in there) with no SortKey.我的 DynamoDB 有一个分区键:ConnectionId(这是我在其中的唯一值),没有 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.您的问题听起来类似于由于put操作的异步性质而出现的问题。

Bit of a "hail mary" here, but have you tried switching the callback version of documentClient.put with the async/await version?这里有点“万岁”,但是您是否尝试过将documentClient.put的回调版本切换为异步/等待版本?

It would look something like this:它看起来像这样:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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