使用Lambda查询DynamoDB什么也不做 [英] Querying DynamoDB with Lambda does nothing

查看:161
本文介绍了使用Lambda查询DynamoDB什么也不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lambda函数的以下代码:

I have the following code for a Lambda function:

console.log('Loading function');
var aws = require('aws-sdk');
var ddb = new aws.DynamoDB();

function getUser(userid) {
    var q = ddb.getItem({
        TableName: "Users",
        Key: {
            userID: { S: userid } }
        }, function(err, data) {
            if (err) {
                console.log(err);
                return err;
            }
            else {
                console.log(data);
            }
    });
    console.log(q);
}


exports.handler = function(event, context) {
    console.log('Received event');
    getUser('user1');
    console.log("called DynamoDB");
    context.succeed();
};

我有一个[Users]表,定义如下:

I have a [Users] table that is defined as such:

{
    "cognitoID": { "S": "token" },
    "email": { "S": "user1@domain.com" },
    "password": { "S": "somepassword" },
    "tos_aggreement": { "BOOL": true },
    "userID": { "S": "user1" }
}

AWS控制台或CLI)我可以看到日志中的消息,但getItem()的回调从未被调用。

When I call the function (from the AWS Console or the CLI) I can see the messages in the logs but the callback for the getItem() is never called.

我试过做getItem(params)没有回调,然后定义回调的完成,成功和失败,但是当我做send(),甚至完整的回调也不调用。

I tried with doing getItem(params) with no callback, then defined the callbacks for complete, success and failure but when I do the send(), even the complete callback isn't called either.

调用是异步的,我认为也许,lambda函数在完成查询之前完成,因此回调将不会被调用,但是,我添加了一个简单的笨的循环在函数的结尾,呼叫超时后3

I know that the calls are asynchronous and I thought that maybe, the lambda function was finishing before the query was done and therefore the callback would not be called, but, I added a simple stupid loop at the end of the function and the call timed out after 3 seconds, without the callbacks being called at all.

我尝试了不同的函数batchGetItem,getItem,listTables和scan。结果是一样的,没有错误,但回调函数从未被调用。

I tried with different functions batchGetItem, getItem, listTables and scan. Result is the same, no error but the callback function is never called.

我打赌,如果我查询dynamoDB而不使用Lambda它会得到我的结果,所以我

I'm betting that if I query dynamoDB without using Lambda it will get me the results so I'm really wondering why nothing is happening here.

我创建了一个函数的角色,我创建了一个策略,允许访问我需要的dynamoDB的功能,但是无效。

I create a role for the function and I created a policy that would allow access to the functionalities in dynamoDB that I need but to no avail.

政策如下:


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": "arn:aws:lambda:*:*:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:GetItem",
                "dynamodb:BatchGetItem",
                "dynamodb:Scan",
                "dynamodb:PutItem",
                "dynamodb:Query",
                "dynamodb:GetRecords",
                "dynamodb:ListTables"
            ],
            "Resource": "arn:aws:dynamodb:*:*:*"
        },
        {
            "Action": [
                "logs:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}


我在模拟器中运行策略,它的工作原理是我想的。
建议?

I ran the policy in the simulator and it worked as I thought it would. Suggestions?

推荐答案

所以,结果是代码是正确的。
问题是,dynamodb API使用所有这些回调,基本上函数在数据被检索之前结束。

So, it turns out that the code is correct. The problem is that the dynamodb API uses all those callbacks and basically the function ends BEFORE the data has been retrieved.

最快的解决方法是删除 context.succeed()调用,并且将检索数据。
当然使用async模块会有所帮助,如果你不想使用它,只需添加一个计数器或布尔值到回调,然后等待值改变,指示回调已被调用如果你想到它是什么样的)

The quickest fix is to remove the context.succeed() call and the data will be retrieved. Of course using the async module would help and if you don't want to use that, just add a counter or a boolean to your callback and then wait until the value has changed, indicating that the callback has been called (which kind of sucks if you think of it)

这篇关于使用Lambda查询DynamoDB什么也不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆