【问题标题】:Writes to a dynamodb from an AWS lambda functon从 AWS lambda 函数写入 dynamodb
【发布时间】:2016-11-15 04:47:40
【问题描述】:

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


};

当我将它作为 lambda 的简单 exec 版本运行时,它会运行,写入控制台,但不会发生写入。当我将其作为微服务运行时,它会在 require 语句中爆炸。任何和所有的帮助表示赞赏。

【问题讨论】:

    标签: node.js amazon-web-services amazon-dynamodb aws-lambda aws-api-gateway


    【解决方案1】:

    您没有正确处理异步调用。提示:您的第二个console.log 发生在数据库调用仍在运行时。您需要将回调函数传递给 DB 调用。在该回调函数中,您需要检查错误,然后执行调用完成后需要执行的任何操作,例如记录已完成的消息。

    【讨论】:

    • 这让我理顺了电话,但仍然存在权限问题。将作为一个单独的问题发布。
    • 权限问题将通过调整您分配给 Lambda 函数的 IAM 角色来解决。
    【解决方案2】:

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

    考虑以下几点:

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

    【讨论】:

    • 当我添加这个
    • 当我添加这个时,我会得到 { "errorMessage": null }
    • 你在日志中看到了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 2021-08-28
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多