我工作的公司目前正在尝试将部分现有代码库迁移到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
任何建议都会很棒。谢谢。
答案 0 :(得分:1)
您没有正确使用回调。阅读它。并记住在处理程序外部初始化模块,以便更快地执行函数。
如果不查看代码的作用,并且我没有测试过这段代码,您需要更多内容:
/**
* 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);
}
});
}
});
}