首页 > 解决方案 > 无法将 AWS lambda 连接到 mysql 数据库

问题描述

嗨,我似乎无法将我的 sql 数据库连接到我的 lambda 函数。以下是我尝试使用的代码。我已经为我的 package.json 添加了一个层。我将我的 lambda 函数链接到 amazon lex,但是当我尝试打印 ${connection.state} 时,它抛出了这个错误“发生错误:无效的 Lambda 响应:收到来自 Lambda 的错误响应:未处理”。我还添加了必要的 lambda 权限。

感谢您的帮助谢谢!

function dispatch(intentRequest, callback) {
 const sessionAttributes = intentRequest.sessionAttributes;
 const slots = intentRequest.currentIntent.slots;

 const mysql = require("mysql");

 var connection = mysql.createConnection({
 host     : 'xxx',
 user     : 'admin',
 password : 'xxx',
 database : 'xxx',
 port : 3306
});

exports.handler = (event, context) => {
 connection.connect(function(err) {
   if (err) context.fail();
   else context.succeed('Success');
 });
};

 callback(close(sessionAttributes, 'Fulfilled',
 {'contentType': 'PlainText', 'content': `Thank you ${connection.state}`}));
}
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
 try {
     dispatch(event,
         (response) => {
             callback(null, response);
         });
 } catch (err) {
     callback(err);
 }
}

标签: mysqlamazon-web-services

解决方案


推荐阅读