2

ラムダ関数からdynamodbデータベースへのAWSの書き込みに取り組んでいます。私はNode.JSに書いています。私はコンソールのログに書込み前後で書込みを行いますが、書込みはテーブルには表示されません。私はおそらく認証エラーまたは設定エラーと考えています。以下は私のコードの始まりです。私はそれが実行されるラムダの簡単なexecのバージョンとして、これを実行するとAWSラムダファンクションから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}"`)); 
    } 


}; 

は、コンソールに書き込みますが、書き込みは発生しません。これをマイクロソフトのサービスとして実行すると、requireステートメントで爆発します。すべてのヘルプが感謝しています。

答えて

2

非同期呼び出しを正しく処理していません。ヒント:データベースコールがまだ実行されている間は、2番目のconsole.logが発生します。 DBコールにコールバック関数を渡す必要があります。そのコールバック関数では、エラーをチェックし、完了したメッセージのログのように、呼び出しが完了した後に発生する必要があるものを実行する必要があります。

+0

これは私はまだかかわらず、権限の問題を持つ、コールにまっすぐしまいました。別の質問として投稿します。 – Art

+0

ラムダ機能に割り当てた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

関連する問題