我们有一个简单的查询来使用部署在 nodejs 上的无服务器应用程序从我们的数据库中获取信息。不幸的是,我无法从 Bluebird 的承诺中得到任何回复——回复总是超时,我不确定是什么导致了这个问题。以下是我的文件:
无服务器.yml:
service: myAuth0
provider:
name: aws
iamRoleARN: arn:aws:iam::XXXXXXXXX:role/test-role
runtime: nodejs4.3
stage: production
region: us-us-1
iamRoleStatements:
- Effect: "Allow"
Action:
- "ec2:CreateNetworkInterface"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DeleteNetworkInterface"
Resource: "*"
vpc:
securityGroupIds:
- ${self:custom.${opt:stage, self:provider.stage}.${opt:region, self:provider.region}.vpc.securitygroup}
subnetIds:
- ${self:custom.${opt:stage, self:provider.stage}.${opt:region, self:provider.region}.vpc.subnet1}
- ${self:custom.${opt:stage, self:provider.stage}.${opt:region, self:provider.region}.vpc.subnet2}
custom:
production:
us-east-1:
vpc:
subnet1: subnet-11111111
subnet2: subnet-22222222
securitygroup: sg-33333333
functions:
getUserRoles:
handler: app/handler.handle
events:
- http:
method: get
path: userstest/roles
处理程序.js:
'use strict';
require('dotenv').config();
var Promise = require('bluebird');
var getConn = require('./dbConn');
module.exports.handle = (event, context, callback) => {
Promise.using(getConn(), function(conn){
return conn.query('select ert.name from emp_roles ert order by ert.name').then(function(rows){
let roles = [];
rows.forEach(function(row){
roles.push(row.name);
});
return roles;
}).catch(function(err){
console.log(error);
});
}).then(function(roles){
console.log("found roles: " + roles);
callback(null, {roles: roles});
});
};
dbConn.js:
var mysql = require('promise-mysql');
var pool = mysql.createPool({
connectionLimit: 10,
host : process.env.MYSQL_HOST,
user : process.env.MYSQL_USER,
password : process.env.MYSQL_PW,
database : process.env.MYSQL_DB
});
function getConn() {
return pool.getConnection().disposer(function(connection) {
pool.releaseConnection(connection);
});
}
module.exports = getConn;
我添加了控制台日志条目,我可以看到它完成了它们......但是它没有返回任何内容,并且最终出现连接错误。我看到数据已获取,而 Lambda 只是坐在那里 6 秒内没有返回任何内容。以下是来自 Cloud Watch 的最新条目:
18:15:01 开始请求 ID:195a7218-a516-11e6-b52d-5f028bb2bdf6 版本:$LATEST 18:15:01 2016-11-07T18:15:01.820Z 195a7218-a516-11e6-b52d-5f028bb2bdf6 找到角色: :15:07 END RequestId: 195a7218-a516-11e6-b52d-5f028bb2bdf6 18:15:07 REPORT RequestId: 195a7218-a516-11e6-b52d-5f028bb2bdf6 持续时间: 6002.14 ms 计费持续时间: 6000 ms 最大内存使用量: 1024 : 18 MB 18:15:07 2016-11-07T18:15:07.468Z 195a7218-a516-11e6-b52d-5f028bb2bdf6 任务在 6.00 秒后超时
将 Bluebird 与 Lambda 一起使用时有什么问题吗?