DynamoDB在AWS LAMBDA忽略本地

DynamoDB在AWS LAMBDA忽略本地

问题描述:

我已经DynamoDB本地运行:DynamoDB在AWS LAMBDA忽略本地

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

,我试图运行lambda-local例如:

lambda-local -f aws -e event.json

不过,我不明白来自dynamodb的任何输出。 没有错误,它看起来像dynamodb.listTables()的呼叫被忽略/忽略。怎么了?

aws.js如下:

var AWS = require("aws-sdk"); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000", 
    accessKeyId: "BLAH", 
    secretAccessKey: "BLAH" 
}); 

var dynamodb = new AWS.DynamoDB(); 

exports.handler = function(event, context) { 
    console.log("EVENT: ", event); 

    event.int++; 

    console.log("LIST TABLES:"); 
    dynamodb.listTables(function(err, data) { 
     if (err) { 
      console.log("Error: ", err.code); 
     } else { 
      console.log("Table names are: ", data.TableNames); 
     } 
    }); 


    console.log("---SUCCEED:---"); 
    context.succeed(event); 
}; 

event.json

{ 
    "obj" : { "a" : "b" }, 
    "int" : 1, 
    "str" : "qwerty", 
    "arr" : [ 1, 2, 3, 4 ] 
} 

输出是:

EVENT: { obj: { a: 'b' }, int: 1, str: 'qwerty', arr: [ 1, 2, 3, 4 ] } 
LIST TABLES: 
---SUCCEED:--- 
OUTPUT 
-------------------------------- 
{ 
    "obj": { 
     "a": "b" 
    }, 
    "int": 2, 
    "str": "qwerty", 
    "arr": [ 
     1, 
     2, 
     3, 
     4 
    ] 
} 

我期待看到至少LIST TABLES和之间的事情 - 成功:--- 但没有输出,也没有e RROR。我还检查了DynamoDB日志,并且没有任何内容。行event.int++工作正常,我看到所有其他console.log()调用。

此外,我请从节点此代码只是为了证明DynamoDB正在工作并且它确实清单表细:

node ListTables.js

内容ListTables.js的(这基本上是相同的代码如上) :

var AWS = require("aws-sdk"); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000", 
    accessKeyId: "BLAH", 
    secretAccessKey: "BLAH" 
}); 

var dynamodb = new AWS.DynamoDB(); 

dynamodb.listTables(function(err, data) { 
    if (err) { 
    console.log("Error: ", err.code); 
    } else { 
    console.log("Table names are: ", data.TableNames); 
    } 
}); 

产出预期: 表名是: '电影']

创建d之前的表只是为了证明DynamoDB实际上正在运行并接受来自node的连接。

问题和解决方案是,DynamoDB函数被称为异步,因此脚本更早完成。

有一次,我从移动年底的dynamodb.listTables(function(err, data) {context.succeed(event);行,然后我有输出精细:

dynamodb.listTables(function(err, data) { 
console.log("INSIDE"); 
    if (err) { 
     console.log("Error: ", err.code); 
    } else { 
     console.log("Table names are: ", data.TableNames); 
    } 
    context.succeed(event); 
}); 

lambda-local -f aws -e event.json

输出:

EVENT: { obj: { a: 'b' }, int: 1, str: 'qwerty', arr: [ 1, 2, 3, 4 ] } 
LIST TABLES: 
---SUCCEED:--- 
INSIDE 
Table names are: [ 'Image', 'Movies' ] 
OUTPUT 
-------------------------------- 
...