热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从AWS函数中写入到dynamodb-WritestoadynamodbfromanAWSlambdafuncton

IanworkinginAWSwritingtoadynamodbdatabasefromalambdafunction.IamwritinginNode.JS.

I an working in AWS writing to a dynamodb database from a lambda function. I am writing in Node.JS. I have console log writes before and after the write but the write doesn't seem to go to the table. I am thinking it is likely a authentication error or a config error. Below is the start of my code.

我在AWS上工作,从一个lambda函数编写到dynamodb数据库。我正在用Node.JS写作。我在写之前和之后都有写控制台日志,但是写似乎没有放到表中。我认为可能是验证错误或配置错误。下面是我的代码的开头。

'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}"`));
    }


};

When I run this as a simple exec version of lambda it runs, writes to the console, but the write doesn't happen. When I run this as a micros service it blows up on the require statement. Any and all help appreciated.

当我将它作为lambda的一个简单的执行版本运行时,将其写入控制台,但写入不会发生。当我将它作为微处理器服务运行时,它会在require语句上崩溃。任何和所有的帮助都被感激。

2 个解决方案

#1


2  

You aren't handling asynchronous calls correctly. Hint: your second console.log happens while the database call is still running. You need to pass a callback function to the DB call. In that callback function you need to check for errors, and then do anything that needs to occur after the call has completed, like logging a message that it has completed.

您没有正确地处理异步调用。提示:你的第二个控制台。当数据库调用仍在运行时发生日志。您需要将回调函数传递给DB调用。在这个回调函数中,您需要检查错误,然后执行调用完成后需要执行的任何操作,比如记录已完成的消息。

#2


2  

You should supply callback to your asynchronous calls and finish the Lambda function context.

您应该为异步调用提供回调并完成Lambda函数上下文。

Consider the following:

考虑以下:

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

推荐阅读
author-avatar
L-SUNMER
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有