已更新:AWS Lambda无法连接到MySQL

编程入门 行业动态 更新时间:2024-10-28 18:36:54
本文介绍了已更新:AWS Lambda无法连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法使用带有Node.js的AWS Lambda连接到MySQL.

I was not able to connect to MySQL using AWS Lambda with Node.js.

我曾尝试为AWS MySQL和Lambda配置安全组.当我使用console.log时,它会以the data from db : rk的形式显示来自数据库的正确响应,但是当我尝试进行测试时,它并未显示正确的响应.

I had tried configured the security groups for AWS MySQL and Lambda. When I used console.log it shows correct response from the data base as the data from db : rk, but when I tried to test it was not showing the correct response.

下面是日志以及index.js文件和日志.有人可以指导我吗?

Below was the logs and the index.js files and logs. Can anybody please guide me ?

index.js(我已经更新了我的代码,如下所示):

index.js (i had updated my code as below ):

var mysql = require('mysql'); var pool = mysql.createPool({ host : 'mydbinstancelamda.connqa9taxeg.us-east-1.rds.amazonaws', user : 'admin', password : 'password', database : 'dbname' }); exports.handler = (event, context, callback)=> { pool.getConnection(function(err, connection) { if (err) throw err; var queryString = "SELECT emp_name from employee where emp_name='rk'"; connection.query(queryString, function(err, rows, fields) { if (err) throw err; console.log("the data from db : " + rows[0].emp_name); callback(null); connection.release(); }); }); };

错误:

Response: { "errorMessage": "2018-06-11T02:34:19.817Z ef864d3d-6d1f-11e8-b6e3-97ac89a0f544 Task timed out after 3.00 seconds" } Request ID: "ef864d3d-6d1f-11e8-b6e3-97ac89a0f544" Function Logs: START RequestId: ef864d3d-6d1f-11e8-b6e3-97ac89a0f544 Version: $LATEST dadf1a33-6d22-11e8-869d-7d7e31ccaf6e the data from db : rk END

推荐答案

尝试从lambda控制台更改lambda执行超时,如下图所示:

Try changing the lambda execution timeout from lambda console as shown in the below picture:

确保RDS MySQL DB的安全组允许来自Lambda的安全组的连接,如果存在相同的连接,那么您就很好了.

Make sure that security group of RDS MySQL DB is allowing connections from Lambda's security group, if there are the same then you are good to go.

更新: MySQL返回响应后,您需要调用回调.

UPDATE: You need to call callback after the MySQL returns the response.

// change following line: exports.handler = (event, context, req,res,callback)=> { // To this line: exports.handler = (event, context, callback)=> {

工作完成后,您需要回调以告知lambda工作已完成:

After the work is finished, You need to callback to tell lambda that work is complete:

callback(undefined, result);

示例: docs.aws.amazon/lambda/latest/dg/nodejs-prog-model-handler.html#nodejs-prog-model-handler-example

更多推荐

已更新:AWS Lambda无法连接到MySQL

本文发布于:2023-11-15 16:28:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   AWS   Lambda   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!