2

我在AWS中使用lambda函數寫入dynamodb數據庫。我正在寫Node.JS.寫入之前和之後我都有控制檯日誌寫入,但寫入似乎沒有進入表格。我想這可能是一個身份驗證錯誤或配置錯誤。以下是我的代碼的開始。從AWS lambda函數寫入dynamodb

'use strict'; 
console.log('Loading function'); 

var AWS = require('aws-sdk'); 
AWS.config.update({region: 'us-west-2'}); 
var dynamo = new AWS.DynamoDB(); 

// let doc = require('dynamodb-doc'); 
//let dynamo = new doc.DynamoDB(); 

var DevicetableName = "DeviceReadings"; 
var AlarmtableName = "AlarmReports"; 
var datetime = new Date().getTime().toString(); 

/** 
* v10 
* Provide an event that contains the following keys: 
* 
* - eventType: type of event temp log or alarm 
* - deviceID: ID of device reporting temp or alarm 
* - deviceType: Type of device furnace or annealer 
* - temp: temperature the device is reporting in fahrenheit degrees 
* - alarmLevel: level of alarm severe, warning, informational.... 
* - alarmText: text of alarm for persisting and publishing 
* - datetime: time of alarm or temp report 
*/ 
exports.handler = (event, context, callback) => { 
    console.log('Received event:', JSON.stringify(event, null, 2)); 

    const eventType = event.eventType; 



    switch (eventType) { 
     /* Update DB with current Temperature and alarm if needed */ 
     case 'LogAnnealTemp':{ 
      /* parse temp out of payload */ 
      console.log('LogAnnealTemp Reached'); 
      dynamo.putItem(
       { 
        "TableName": DevicetableName, 
        "Item": 
        { 
        "deviceIDSensorID": {"S": "Dev1Sense1" }, 
        "deviceType": {"S": "Anneal" }, 
        "temp": {"N": "1969" }, 
        "timeTaken": {"S": "today" }, 
        "timeWritten": {"S": "alsotoday" } 
        } 
       }); 
      console.log('LogAnnealTemp After Write Reached'); 
      break; 
     } 
     case 'LogFurnaceTemp':{ 
      /* parse temp out of payload */ 
      console.log('LogAnnealTemp Reached'); 

      /* If temp is over 2300 write to alarm topic */ 
      if (event.temp >= 2300) 
      { 
       console.log('LogFurnaceTemp over 2300 Reached'); 
      } 
      /* Else if temp is under 1900 write to alarm topic */ 
      else if (event.temp <= 1900) 
      { 
       console.log('LogFurnaceTemp under 1900 Reached');  
      } 
      break; 
     } 
     case 'LogAlarm':{ 
      /* parse alarm level out of payload */ 
      /* If alarm is severe log and notify */ 
      if (event.alarmlevel = "severe") 
      { 
       console.log('LogAlarm Severe'); 
      } 
      /* Else if alarm is serious log and notify */ 
      else if (event.alarmlevel = "serious") 
      { 
       console.log('LogAlarm Serious'); 
      } 
      /* Else if alarm is warning log */ 
      else if (event.alarmlevel = "warning") 
      { 
       console.log('LogAlarm Warning'); 
      } 
      else if (event.alarmlevel = "informational") 
      { 
       console.log('LogAlarm Informational'); 
      } 
      else { 
       console.log('LogAlarm Unknown'); 
      } 
      break; 
     } 
     case 'ping':{ 
      callback(null, 'pong'); 
      break; 
     } 
     default: 
      callback(new Error(`Unrecognized operation "${operation}"`)); 
    } 


}; 

當我運行它作爲lambda的一個簡單的exec版本它運行,寫入控制檯,但寫入不會發生。當我把它作爲一個微服務運行時,它就要求聲明。任何和所有幫助表示讚賞。

回答

2

您沒有正確處理異步調用。提示:您的第二個console.log在數據庫調用仍在運行時發生。您需要將回調函數傳遞給數據庫調用。在該回調函數中,您需要檢查錯誤,然後在完成調用後執行任何需要執行的操作,例如記錄已完成的消息。

+0

這讓我理順的號召,仍然有權限的問題,雖然。將作爲單獨的問題發佈。 – Art

+0

權限問題將通過調整您分配給Lambda函數的IAM角色來解決。 –

2

您應該爲異步調用提供回調並完成Lambda函數上下文。

考慮以下幾點:

dynamo.putItem({ ... }, function(err, data) { 
    if(err) { 
     console.error('dynamo failed', err); 
     context.fail(); 
    } else { 
     context.done(); 
    } 
}); 
+0

當我在 – Art

+0

添加這個時當我添加這個我剛剛得到{ 「errorMessage」:null } – Art

+0

而你在日誌中看到? – kixorz