简体   繁体   English

AWS Lambda 错误 - “errorType”:“string”,“errorMessage”:“handled”

[英]AWS Lambda Error - "errorType": "string", "errorMessage": "handled"

I am trying to write my first lambda function and use my MySQL database.我正在尝试编写我的第一个 lambda 函数并使用我的 MySQL 数据库。 The credentials to the database are correct and both are in the same subnet.数据库的凭证是正确的,并且都在同一个子网中。 The purpose of the function is to show all tables.该函数的目的是显示所有表。 Here my Node.js 10.x script:这是我的 Node.js 10.x 脚本:

'use strict';
console.log("Loading controls function");

const AWS = require('aws-sdk');
const mysql = require('mysql');

exports.handler = (event, context) => {
    // context.callbackWaitsForEmptyEventLoop = false; 
    
    const connection = mysql.createConnection({
        host: process.env.RDS_HOST,
        user: process.env.RDS_USER,
        password: process.env.RDS_PASSWORD,
        database: process.env.RDS_DATABASE
    });
    
    connection.query('select * from controls;', function (error, results, fields) {
      if (error){
        context.fail();  
        return { statusCode: 500, body: JSON.stringify(error) }
      } 
      else{
        context.succeed('Success');  
        return { statusCode: 200, body: JSON.stringify(results) }
      } 
    });
};

The error looks like this:错误如下所示:

{ "errorType": "string", "errorMessage": "handled", "trace": [] } { “errorType”:“字符串”,“errorMessage”:“已处理”,“跟踪”:[]}

Does anyone could imagine what could be the issue?有谁能想象可能是什么问题?

AFAIK a lambda handler should either be an async function or accept a callback .据我所知,lambda 处理程序应该是async函数或接受callback Given your code it is simpler to make it accept a callback .鉴于您的代码,让它接受callback更简单。

'use strict';
console.log("Loading controls function");

const AWS = require('aws-sdk');
const mysql = require('mysql');

exports.handler = (event, context, callback) => {
  const connection = mysql.createConnection({
    host: process.env.RDS_HOST,
    user: process.env.RDS_USER,
    password: process.env.RDS_PASSWORD,
    database: process.env.RDS_DATABASE
  });
    
  connection.query('select * from controls;', function (error, results, fields) {
    if(error) {
      context.fail();
      return callback(null, { statusCode: 500, body: JSON.stringify(error) });
    } 

    context.succeed('Success');  
    callback(null, { statusCode: 200, body: JSON.stringify(results) });
  });
};

https://github.com/mysqljs/mysql中,您需要在查询命令之前运行connection.connect()

I am going to take a while guess that you have not configured the VPC settings for your Lambda function.我会花点时间猜测您没有为 Lambda 函数配置 VPC 设置。 You see, by default, Lambda doesn't actually get an IP as far as your concerned, or a security group.您会看到,默认情况下,Lambda 实际上并未获得您所关心的 IP 或安全组。 So it cannot communicate to anything in your VPC by default really.所以默认情况下它无法与您的 VPC 中的任何内容通信。

Go to the Lambda, go to the VPC settings, and make sure you configure the Lambda to run in Subnets in your VPC, and give it a security group which will enable it to access your database.转到 Lambda,转到 VPC 设置,确保将 Lambda 配置为在 VPC 的子网中运行,并为其提供一个安全组,使其能够访问您的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM