2017-09-04 90 views
-1

我已經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的連接。

回答

0

問題和解決方案是,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 
-------------------------------- 
...