我有一个NodeJS / Serverless Framework API并在AWS Lambda上运行。它已连接到可以正常工作的AWS RDS,但是突然之间,它不能仅在我的办公室Internet上连接到RDS实例。 API和数据库连接在我的家庭Internet甚至移动数据上都可以正常工作。
SequelizeConnectionError: connect ETIMEDOUT
将API部署在AWS Lambda和EC2实例上(成功的数据库连接)后,它可以正常工作。 安全组已为PORT 3306打开了入站流量访问权限
const Sequelize = require('sequelize')
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
ssl: true,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
dialectOptions: {
ssl: 'Amazon RDS'
}
}
)
// Models
const User = require('./models/User')(sequelize, Sequelize)
const Post = require('./models/Post')(sequelize, Sequelize)
const Comment = require('./models/Comment')(sequelize, Sequelize)
let connection= {}
let Models = {
User,
Post,
Comment
}
/**
* Creating Associations
*/
Object.keys(Models).forEach(function(modelName) {
if (Models[modelName].associate) {
Models[modelName].associate(Models);
}
})
module.exports = async () => {
if(connection.isConnected){
console.log("use existing connection")
return Models
}
try {
// await sequelize.sync()
await sequelize.authenticate()
connection.isConnected = true
console.log("use new connection")
return Models
} catch (error) {
console.log(`Connection Error: ${error}`)
}
}
您可以找到仓库here。
答案 0 :(得分:0)
要使Lambda正常运行,请检查以下几件事:
dialectOptions: {
ssl: {
ca: 'certPathDownloadedFromAWS'
}
}