2017-09-13 1 views
0

Der Übersichtlichkeit halber erfolgt dies in einer Node 6.10 AWS Lambda-Funktion.DynamoDB verursacht Modulinitialisierungsfehler in Node/Lambda

Fehler:

module initialization error: Error at Object.Service (/var/task/node_modules/aws-sdk/lib/service.js:24:28) at Object.features.constructor [as DynamoDB] (/var/task/node_modules/aws-sdk/lib/util.js:602:24) at Object.<anonymous> (/var/task/web.js:4:22) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19)

Die Höhe des Moduls Initialisierung:

const aws = require('aws-sdk'); this.dynamoDb = new aws.DynamoDB();

nicht sicher, was hier zu tun; etwas offensichtliches vermissen?

+0

Versuchen ohne 'aws-sdk' von Ihrem Bereitstellungspaket. Es ist sowieso schon in der Lambda-Umgebung enthalten. – dashmug

+0

Hatte Idee, danke! – user1381745

Antwort

3

Bitte versuchen Sie,

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

//If you are running in Lambda you don't need below AWS.config section 
AWS.config.update({ 
region: "REGION" 
}); 

var docClient = new AWS.DynamoDB.DocumentClient(); 

und verwenden 'docClient' DynamoDB Funktionen zuzugreifen.

Zum Beispiel

docClient.put(params, function (err, data) { 
    if (err) { 
     console.error("Unable to add item. Error JSON:", JSON.stringify(err,null, 2)); 
    } else { 
     callback(null, "Error while adding data:", JSON.stringify(data, null, 2)) 
    } 
}); 

Bitte entnehmen Sie die Probe ich in meinem GitHub erstellt haben: - https://github.com/vnathv/DynamoDb-CRUD.git

+2

Wenn Sie in Lambda laufen, aktualisieren AWS.config ist unnötig – Simon

+1

Ja, ich stimme zu. Bearbeitete die Lösung, um Ihren Kommentar hinzuzufügen. Vielen Dank :) –