我使用 AWS AppSync 和 Lambda 函数作为解析器。 这些函数查询 RDS Postgres 数据库(使用 Sequelize)。
我的问题是,无论我做什么,我的查询都不会被触发。我的代码有什么问题吗?
import { Context, Callback } from 'aws-lambda';
import * as Sequelize from 'sequelize';
const sequelize = new Sequelize({
database: process.env.RDS_NAME,
username: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
host: process.env.RDS_HOST,
port: 5432,
dialect: 'postgres',
pool: { idle: 1000, max: 1 }
});
const options = {
timestamps: false,
freezeTableName: true
};
const Node = sequelize.define(
'node',
{
nodeId: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
slug: {
type: Sequelize.STRING
}
},
options
);
export const getUser = async (
event: any,
context: Context,
callback: Callback
) => {
context.callbackWaitsForEmptyEventLoop = false;
Node.findAll({
where: {}
})
.then(nodes => {
console.log('nodes', nodes);
callback(null, {
id: 1,
name: JSON.stringify(nodes)
});
})
.catch(err => callback(err));
};
我看过这篇文章,但没有成功。 Sequelize Code Not Executing Inside AWS Lambda
最佳答案
我发现了问题。我的 RDS 位于与默认安全组不同的安全组中。我必须更改它才能使其正常工作。
关于lambda - Lambda 函数中永远不会调用 RDS 与 Sequelize 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010869/