当我查询MySQL并尝试通过AWS SES

时间:2018-01-19 12:24:23

标签: mysql node.js aws-lambda amazon-ses serverless

我有以下节点代码

'use strict';
var aws = require('aws-sdk');
var async = require('async');
const mysql = require('mysql');

var ses = new aws.SES({
  region: 'eu-west-1'
});

module.exports.helloWorld = function(event, context, callback) {
  //context.callbackWaitsForEmptyEventLoop = false; //Updated on @dashmug recommendation
  console.log("Hit here 1");
  let connection = mysql.createConnection({
    host: "xxx",
    user: "xxx",
    password: "xxx",
    database: "xxx"
  });
  connection.connect(); //Updated on @dashmug recommendation
  console.log("Hit here 2");
  connection.query(`SELECT * FROM User U WHERE id = 1`, function(error, row) {

    console.log("Hit here 3");
    console.log(row);
    let eParams = {
      Destination: {
        ToAddresses: [row[0].email]
      },
      Message: {
        Body: {
          Html: {
            Data: "Test"
          },
          Text: {
            Data: "Test"
          }
        },
        Subject: {
          Data: "Test Email"
        }
      },
      Source: "team@hello.com"
    };
    console.log("Hit here 4");
    connection.end(); //Updated on @dashmug recommendation
    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data) {
      if(err) {
        console.log(err);
        //context.fail(err);
        callback(err); //Updated on @dashmug recommendation
      } else {
        console.log("===EMAIL SENT===");
        console.log("EMAIL CODE END");
        console.log('EMAIL: ', email);
        console.log(data);
        callback(null, event); //Updated on @dashmug recommendation
      }
    });
  });
}
  

注意:我的SES默认限制已删除。我也尝试在结果回调中移动connection.end()。我得到了相同的结果。

此代码总是超时。我的云监视日志

{
  "errorMessage": "2018-01-19T11:58:16.794Z cc9e6998-fd0f-11e7-ae0a-xxxx Task timed out after 100.06 seconds"
}

    START RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816 Version: $LATEST
2018-01-25T11:28:43.928Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    Hit here 1
2018-01-25T11:28:44.163Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    Hit here 2
2018-01-25T11:28:44.207Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    Hit here 3
2018-01-25T11:28:44.164Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    Hit here 4
2018-01-25T11:28:44.209Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    [ RowDataPacket {
    id: 1,
    name: 'Karthik',
    email: 'xxx@xxx.com' } ]
2018-01-25T11:28:44.223Z    e60d142a-01c2-11e8-99da-fb3cb3a59816    ===SENDING EMAIL===
END RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816
REPORT RequestId: e60d142a-01c2-11e8-99da-fb3cb3a59816  Duration: 100038.81 ms  Billed Duration: 100000 ms  Memory Size: 512 MB Max Memory Used: 54 MB  
2018-01-25T11:30:23.965Z e60d142a-01c2-11e8-99da-fb3cb3a59816 Task timed out after 100.04 seconds

我的无服务器配置。

service: test
provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: eu-west-1
  memorySize: 512
  timeout: 100
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:*
        - SNS:Publish
        - SES:SendRawEmail
        - SES:SendEmail
        - lambda:InvokeFunction
      Resource: "*"
  environment:
    NODE_ENV: live
  vpc:
    securityGroupIds:
      - sg-xxx
      - sg-xxx
      - sg-xxx
      - sg-xxx
    subnetIds:
      - subnet-xxx
      - subnet-xxx
      - subnet-xxx
functions:
  emailEmailFeedback:
    handler: handler.helloWorld
    events:
      - http:
          path: user-feedback
          method: get
          cors: true
  

注意: VPC配置用于我的MySQL连接。我只有一个VPC,我的所有服务都在其中。

当我取出MySQL查询并运行我的SES测试电子邮件时,该功能成功运行。

此外,当我拿出SES并运行MySQL时,我成功地恢复了结果。

但他们不能一起工作。

我还尝试将SES移动到另一个函数并尝试从我的MySQL查询回调中调用sendEmail函数,它没有工作,我得到相同的超时错误。

lambda.invoke({
            FunctionName: 'sendEmail',
            Payload: JSON.stringify(rows, null, 2) // pass params
        }, function(error, data) {
            if (error) {
                context.fail({"hello": "world"});
            }
            if(data.Payload){
                context.succeed({"hello": "world"});
            }
        });

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

使用callback()代替context.succeed()context.fail()。这些上下文方法已被弃用。

根据https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-using-old-runtime.html#transition-to-new-nodejs-runtime

  

Node.js运行时v0.10.42不支持运行时v4.3和v6.10支持的Lambda函数的回调参数。使用运行时v0.10.42时,使用以下上下文对象方法正确终止Lambda函数。上下文对象支持可用于终止Lambda函数的done(),succeed()和fail()方法。为了向后兼容,这些方法也存在于运行时v4.3和v6.10中。

因此,在sendMail()的回调中,它变为......

if(err) {
    console.log(err);
    callback(err);
} else {
    console.log("===EMAIL SENT===");
    console.log("EMAIL CODE END");
    console.log('EMAIL: ', email);
    console.log(data);
    callback(null, event);
}

另一件事,你不需要context.callbackWaitsForEmptyEventLoop = false;

此外,由于您连接到数据库inside您的处理程序,因此您还需要在获得结果后与其断开连接。