专用子网中的节点AWS Lambda无法返回回调

时间:2019-04-01 13:47:54

标签: node.js amazon-web-services aws-lambda amazon-vpc private-subnet

以下是我的问题陈述-
1.我有一个区域API网关端点

  1. 在VPC专用子网中,有一个Lambda函数被Lambda调用以访问RDS。

  2. 对API网关的邮递员调用能够调用Lambda函数。

  3. Lambda函数能够正确处理其中的所有内容。

  4. Lambda函数代码到达代码的响应块,但无法返回响应。

  5. Lambda函数的安全组已通过公共子网中的NAT网关允许所有出站。

以下是函数处理程序-

SELECT *
FROM (VALUES(N'someString'),
            (N'someString'),
            (N'someString'),
            (N'someString'),
            (N'somestring')) V(S)
WHERE S = N'someString' COLLATE Latin1_General_100_CI_AI_KS_WS

CloudWatch日志显示代码能够到达成功块,

Latin1-General-100, case-insensitive, accent-insensitive, kanatype-sensitive, width-sensitive

邮递员回复-

var async = require('async');
var moment = require('moment');
var service = require('./some-service');

exports.handler = (event, context, callback) => {

    var requestBody = event['body-json'];

    async.waterfall([
        function(callback) {
            var processRequest = {};
            processRequest.validationMessage = validateRequest(requestBody);

            if(processRequest.validationMessage == ''){
                processRequest.isValid = true;

                service.processService(requestBody,function(err, response) {
                    if(err){
                        callback(err, null);
                    }
                    else{
                        callback(null, response);
                    }
                });
            }
            else{
                processRequest.isValid = false;
                callback(null, processRequest);
            }
        }
    ],
    function(err, resp) {
        let response = {};
        if (err) {
            response = {
                statusCode: 500,
                body: JSON.stringify('API Error : ' + err),
            };
            callback(err, response);
        } 
        else {
            if(resp.isValid){
                response = {
                    statusCode: 200,
                    body: 'Record updated for user_id '+requestBody.user_id,
                }; 
                console.log('Success block! ', response);
                callback(null, response);   
            }
            else{
                console.log('Failure block!');
                response = {
                    statusCode: 500,
                    body: resp.validationMessage,
                }; 
                callback(null, response);
            }
        }
    });
};

function validateRequest(requestBody){
    var isValid = '';

    if(requestBody['user_id'] == undefined){
        console.log('user_id missing');
        isValid += 'user_id, ';
    }
    if(requestBody['added_by'] == undefined){
        isValid += 'added_by, ';
    }
    if(isValid != ''){
        isValid = isValid.substring(0, isValid.lastIndexOf(','));
        isValid += ' missing in the request';
    }
    return isValid;
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

通过在处理程序下的第一行-

中添加以下内容来解决此问题
context.callbackWaitsForEmptyEventLoop = false;