【问题标题】:DynamoDB, Lambda function / custom module TimeoutDynamoDB、Lambda 函数/自定义模块超时
【发布时间】:2018-08-12 19:37:20
【问题描述】:

我有以下两个 JS 文件。我的问题是当我调用调用 Archive.js 将日志归档到 DynamoDB 的 Calls.js 时,请求超时。 我已经尝试了很多东西,阅读了很多东西,在本地/AWS 环境中尝试过,但没有运气。我错过了什么?

Link1, Link2, Link3, Link4, Link5,

存档.js

module.exports.archive = archive;
...
function archive(input, callback){
  AWS.config.update({
    region: "eu-west-1",
    endpoint: "http://localhost:8000"
  });
  var documentClient = new AWS.DynamoDB.DocumentClient({
    httpOptions: {
      agent: new https.Agent({
        rejectUnauthorized: true,
        secureProtocol: "TLSv1_method",
        ciphers: "ALL"
      })
    }
  });
...
  var paramsPUT = {
    TableName: "Logging",
    Item: {
      HashKey: dbID,
      archiveEntry: archiveEntry
    }
  };
...
documentClient.put(paramsPUT, function(err, data) {

    if (err) console.log(err);
    if (data) console.log(data);
...
callback(data);
  });


}

Calls.js

exports.handler(event, context, callback)   => {
    const archive = require("./..path..").archive;
    ...
    context.callbackWaitsForEmptyEventLoop = false;
    ...
        archive(input, callback);
    ...
    }

【问题讨论】:

    标签: node.js callback aws-lambda amazon-dynamodb


    【解决方案1】:

    我无法使用您的代码重现超时情况。您的代码正在与http://localhost:8000 的 AWS 端点通信,所以我假设您已启动并运行本地 DynamoDB,不是吗?本地 DynamoDB 运行失败会导致超时。

    话虽如此,我强烈建议重构您的代码以使用 Promise 和 NodeJS 8 提供的新 async/await,而不是传递 Lambda 回调。

    这是修改后的代码。

    const AWS = require("aws-sdk");
    
    async function archive(input) {
    
        return new Promise( (resolve, reject) => {
            AWS.config.update({
                region: "eu-west-1",
                endpoint: 'http://localhost:8000'        
            });
    
            //use client specific AWS configuration instead of the global one
            const documentClient = new AWS.DynamoDB.DocumentClient();
    
            var paramsPUT = {
                TableName: "Logging",
                Item: {
                    HashKey: "123",
                    archiveEntry: input
                }
            };
    
            documentClient.put(paramsPUT, function (err, data) {
    
                if (err) {
                    console.log("ERROR " + err);
                    reject(err);
                }
    
                console.log("Returned from DDB " + JSON.stringify(data, null,2));
                resolve(data);
            });
    
        });
    }
    
    exports.handler = async (event, context, callback) => {
        const result = await archive("abc");
        callback(result);
    }
    
    // stuffs to test locally 
    
    callback = function (data) {
        console.log("callback called with " + JSON.stringify(data,null,2));
    }
    
    event = context = {}
    
    exports.handler(event, context, callback);
    

    【讨论】:

    • 是的,我确实有本地 DynamoDB。感谢您提供出色的解决方案,问题出在我的本地环境中:由于某种原因,本地 Lambda 函数无法到达本地 DynamoDB。可能是因为容器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2018-11-01
    • 2021-05-27
    • 2022-01-13
    相关资源
    最近更新 更多