为什么我不能将api params传递给lambda函数?

时间:2018-03-25 14:08:52

标签: node.js lambda api-gateway

我正在尝试设置一个api网关,只需通过lambda发送和发送电子邮件,而且是ses。

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-east-1'
});
const querystring = require('querystring');
const inspect = require('util').inspect;


exports.handler = function(event, context) {
    var output = querystring.parse(event);
    console.log("Incoming: ", output["email"]);
    email=output["email"]

    console.log("Incoming: ", email);
    var eParams = {
        Destination: {
            ToAddresses: [email]
        },
        Message: {
            Body: {
                Text: {
                    Data: "some trial text"
                }
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "info@ayyy.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

Lambda代码看起来像这样。如何从api传递事件数据?

控制台日志如下所示:

十四点00分57秒 START RequestId:f07a7595-3034-11e8-81c7-d3dcc4528819版本:$ LATEST START RequestId:f07a7595-3034-11e8-81c7-d3dcc4528819版本:$ LATEST  14点00分57秒 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819传入:未定义 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819传入:未定义  14点00分57秒 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819传入:未定义 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819传入:未定义  14点00分57秒 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===发送电子邮件=== 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===发送电子邮件===  14点○○分58秒 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 {[ValidationError:检测到1个验证错误:值为null' destination'未能满足约束:成员不能为空]消息:'检测到1个验证错误:值为\'目的地\'未能满足约束:成员不能为空',代码:' ValidationError',时间:Sun Mar 25 2018 14:00:58 GMT + 0000(UTC),r 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 {[ValidationError:检测到1个验证错误:值为null' destination'无法满足约束:成员不能为空] 消息:'检测到1个验证错误:\'目的地的值为空无法满足约束:成员不能为null', 代码:' ValidationError', 时间:2018年3月25日星期日14:00:58 GMT + 0000(UTC), requestId:' f14b32f1-3034-11e8-897b-633b17ff4491', statusCode:400, 可重试:false, retryDelay:54.40075590740889}  14点○○分58秒 END RequestId:f07a7595-3034-11e8-81c7-d3dcc4528819 END RequestId:f07a7595-3034-11e8-81c7-d3dcc4528819  14点○○分58秒 REPORT RequestId:f07a7595-3034-11e8-81c7-d3dcc4528819持续时间:1498.93 ms结算时长:1500 ms内存大小:128 MB最大使用内存:37 MB

2 个答案:

答案 0 :(得分:1)

Log the event and see how it is structured. Your function fails from the start as the email is undefined. In API Gateway, if you defined LAMBDA PROXY INTEGRATION, your event body will be structured like this:

{
  "message": "Good day, John of Seattle. Happy Friday!",
  "input": {
    "resource": "/{proxy+}",
    "path": "/Seattle",
    "httpMethod": "POST",
    "headers": {
      "day": "Friday"
    },
    "queryStringParameters": {
      "time": "morning"
    },
    "pathParameters": {
      "proxy": "Seattle"
    },
    "stageVariables": null,
    "requestContext": {
      "path": "/{proxy+}",
      "accountId": "123456789012",
      "resourceId": "nl9h80",
      "stage": "test-invoke-stage",
      "requestId": "test-invoke-request",
       ... 
      },
      "resourcePath": "/{proxy+}",
      "httpMethod": "POST",
      "apiId": "r275xc9bmd"
    },
    "body": "{ \"callerName\": \"John\" }",
    "isBase64Encoded": false
  }
}

You want to read the queryStringParameters Object.

答案 1 :(得分:0)

确定。我解决了这个问题。需要用JSON.parse解析JSOn然后它工作。