简体   繁体   English

将 Lambda function 连接到 AWS 上的 RDS

[英]Conecting Lambda function to RDS on AWS

I'm trying to connect to a msql database on AWS using the following lambda function我正在尝试使用以下 lambda function 连接到 AWS 上的 msql 数据库

const dotenv = require('dotenv');
dotenv.config();

var mysql = require('mysql');


var pool  = mysql.createPool({
    host     : process.env.DBHOST,
    user     : process.env.DBUSER,
    password : process.env.DBPASSWORD,
    database : process.env.DBNAME
  });

exports.handler =  (event, context, callback) => {
  //prevent timeout from waiting event loop
  context.callbackWaitsForEmptyEventLoop = false;
  pool.getConnection(function(err, connection) {
    // Use the connection
    connection.query('select * from products', function (error, results, fields) {
      // And done with the connection.
      connection.release();
      // Handle error after the release.
      if (error) callback(error);
      else callback(null,results[0].emp_name);

    });
  });
};

When executed it gives the following error:执行时会出现以下错误:

Response:
{
  "errorMessage": "2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds"
}

Request ID: "281776f1-ee7f-4c8c-8862-a7371d1a8f37"请求 ID:“281776f1-ee7f-4c8c-8862-a7371d1a8f37”

Function logs: Function 日志:

START RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Version: $LATEST
END RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37
REPORT RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37  Duration: 3003.58 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 75 MB  Init Duration: 202.41 ms    
2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds

How do I fix this?我该如何解决?

This seems like a communication issue.这似乎是一个沟通问题。

  1. Make sure that the lambda can reach the RDS.确保 lambda 可以到达 RDS。 Either they are deployed on same VPC, or there is a path from the lambda to the RDS.它们要么部署在同一个 VPC 上,要么存在从 lambda 到 RDS 的路径。

  2. Make sure that the RDS has a security group with an inbound rule to allow connection from the lambda (protocol, port, ip range or security group)确保 RDS 具有带有入站规则的安全组,以允许来自 lambda 的连接(协议、端口、ip 范围或安全组)

Also, verify that the lambda has the appropriate privileges (ie AWS IAM Role) to support your desired action.此外,验证 lambda 是否具有适当的权限(即 AWS IAM 角色)以支持您所需的操作。

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

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