gpt4 book ai didi

mysql - AWS Lambda 不执行 node.js MySQL 查询

转载 作者:行者123 更新时间:2023-11-29 02:46:04 25 4
gpt4 key购买 nike

我工作的公司目前正在尝试将我们现有的一些代码库迁移到 AWS Lambda。我们正在运行 node.js 4.3(为 lambda 提供的最高版本),我们正在 VPC 内执行以连接到 RDS 数据库。 Lambda 函数可以很好地连接到数据库,但是对我们的 MySQL 表的简单选择查询没有执行或返回任何错误。这是我们正在尝试的代码:

exports.handler = (event, context, callback) => {

/**
* Require Config file
**/
var config = require('./config.js');

/**
* AWS/MWS Configuration
**/
var AWS = require('aws-sdk');
var MWS = require('mws');

var mws = require('./lib/mws-reports/lib/mws.js');
var XML = require('./lib/mws-reports/pixl-xml');
var mwsReportsAPI = require('./lib/mws-reports/lib/reports.js');
var mwsFeedsAPI = require('./lib/mws-reports/lib/feeds.js');

AWS.config.region = config.AWS.region;
AWS.config.credentials = config.AWS.credentials;

var client = new mws.Client(
config.MWS.credentials.accessKeyId,
config.MWS.credentials.secretAccessKey,
config.MWS.credentials.sellerID,
{}
);

/**
* Configure DB
* @var Promise - A library to maintain Promises (used for chained MySQL queries)
* @var Utils - A wrapper for utility functions.
* @var connection - The MySQL DB Connection
**/
var Promise = require('bluebird');
var utils = require('./lib/Utils');
var mysql = require('mysql');

var MySQLConnection = mysql.createConnection(config.mysql);

MySQLConnection.connect(function (err) {
if(err) {
console.log("Error connection: " + err.stack);
return;
}

console.log("Connected as id " + connection.threadId);
});

var connection = Promise.promisifyAll(MySQLConnection);
var InventoryHealth = require('./app/models/InventoryHealth');
var Items = require('./app/models/Items');

connection.query("SELECT * FROM items", function(err, rows) {
rows.forEach(function(item) {
console.log(JSON.stringify(item));
});
});

connection.end();
context.done(null, "Finished :)");
}

正如我所提到的,所有这一切中最奇怪的部分是我没有从 Lambda 获得响应。这就是它告诉我的全部内容:

START RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac Version: $LATEST
2017-02-07T04:22:01.123Z f726f0ba-ecec-11e6-b0b3-9d51c554a5ac (node) crypto.createCredentials is deprecated. Use tls.createSecureContext instead.
END RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac
REPORT RequestId: f726f0ba-ecec-11e6-b0b3-9d51c554a5ac Duration: 4232.01 ms Billed Duration: 4300 ms Memory Size: 128 MB Max Memory Used: 22 MB

任何建议都会很棒。谢谢。

最佳答案

您没有正确使用回调。仔细阅读。并记住在处理程序外部初始化模块以加快函数执行速度。

不看代码的作用,我也没有测试这段代码,你需要更多的东西:

  /**
* Require Config file
**/
var config = require('./config.js');

/**
* AWS/MWS Configuration
**/
var AWS = require('aws-sdk');
var MWS = require('mws');

var mws = require('./lib/mws-reports/lib/mws.js');
var XML = require('./lib/mws-reports/pixl-xml');
var mwsReportsAPI = require('./lib/mws-reports/lib/reports.js');
var mwsFeedsAPI = require('./lib/mws-reports/lib/feeds.js');

AWS.config.region = config.AWS.region;
AWS.config.credentials = config.AWS.credentials;

var client = new mws.Client(
config.MWS.credentials.accessKeyId,
config.MWS.credentials.secretAccessKey,
config.MWS.credentials.sellerID,
{}
);

/**
* Configure DB
* @var Promise - A library to maintain Promises (used for chained MySQL queries)
* @var Utils - A wrapper for utility functions.
* @var connection - The MySQL DB Connection
**/
var Promise = require('bluebird');
var utils = require('./lib/Utils');
var mysql = require('mysql');

var InventoryHealth = require('./app/models/InventoryHealth');
var Items = require('./app/models/Items');

exports.handler = (event, context, callback) => {

var MySQLConnection = mysql.createConnection(config.mysql);

MySQLConnection.connect(function (err) {
if(err) {
console.log("Error connection: " + err.stack);
callback(err, err);
} else {

console.log("Connected as id " + connection.threadId);

var connection = Promise.promisifyAll(MySQLConnection);


connection.query("SELECT * FROM items", function(err, rows) {
if (err) {
console.log(err);
callback(err, err);
} else {
rows.forEach(function(item) {
console.log(JSON.stringify(item));
});
connection.end();
callback(null, rows);
}
});


}
});


}

关于mysql - AWS Lambda 不执行 node.js MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com