Base on aws-serverless-express repository on Github. I've made a connection to AWS RDS in order to CRUD data. I'm using Sequelize for ORM.
I've tested on local, actions work perfectly. But when it's deployed on AWS Lambda the error was thrown at Sequelize initialization (without it works normally)
Sequelize initial
const database = new Sequelize('databasename', 'root', 'password', {
host: 'XXXXXX.XXXXXX.us-east-1.rds.amazonaws.com',
port: 3306,
logging: console.log,
maxConcurrentQueries: 100,
dialect: 'mysql',
dialectOptions: {
ssl: 'Amazon RDS',
},
pool: { maxConnections: 5, maxIdleTime: 30 },
language: 'en',
});
Error logs was got from CloudWatch:
module initialization error: Error
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.Module._extensions..js (module.js:662:20)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/mysql2/lib/packets/ssl_request.js:3:23)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at /var/task/mysql2/lib/packets/index.js:4:16
at Array.forEach (<anonymous>)
at Object.<anonymous> (/var/task/mysql2/lib/packets/index.js:3:4)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
I fear error throws cause SSL connection problem, but I don't know how to figure out.
Below is my config on RDS MySql instance:
Network & Security
Security group: default
Inbound:
Type: All trafic
Protocol: All
Port Range: All
Source: sg-3c941877 (default)
Outbound:
Type: All trafic
Protocol: All
Port Range: All
Destination: 0.0.0.0/0
Public accessibility: Yes
Database options
IAM DB authentication: Disabled
In AWS Lambda I also added AWSLambdaVPCAccessExecutionRole Policy
Thanks in advance.
Related
I have a replicaSet running in k8s, while trying to connect to the replicaSet from nodejs using mongoose, the connection fails with this logs:
MongooseServerSelectionError: connection timed out
at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:807:32)
at /app/node_modules/mongoose/lib/index.js:340:10
at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)
at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1140:10)
at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:339:20)
at /app/src/connect.ts:21:14
at Promise._execute (/app/node_modules/bluebird/js/release/debuggability.js:313:9)
at Promise._resolveFromExecutor (/app/node_modules/bluebird/js/release/promise.js:488:18)
at new Promise (/app/node_modules/bluebird/js/release/promise.js:79:10)
at connect (/app/src/connect.ts:20:10)
at initialiseDatabase (/app/src/connect.ts:8:5)
at Object.<anonymous> (/app/src/mongoose.ts:36:19)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (/app/src/user/operations/find-by-id.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (/app/src/middleware/authenticate.ts:7:1)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (/app/src/app.ts:8:1)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47 {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'mongodb-0.mongodb-headless.staging.svc.cluster.local:27017' => [ServerDescription],
'mongodb-1.mongodb-headless.staging.svc.cluster.local:27017' => [ServerDescription],
'mongodb-2.mongodb-headless.staging.svc.cluster.local:27017' => [ServerDescription]
},
And this is my connection string:
mongodb://username:pw#mongodb-0.mongodb-headless.staging.svc.cluster.local:27017,mongodb-1.mongodb-headless.staging.svc.cluster.local:27017,mongodb-2.mongodb-headless.staging.svc.cluster.local:27017/labxpert?authSource=admin&serverSelectionTimeoutMS=50000&replicaSet=rs0
When I test the connection with the mongo Shell, it works just fine.
What could be causing the MongooseServerSelectionError: connection timed out.
For anyone who may face this problem with mongoose v6+ and nodejs and white listing your ip has not solved it or changing from localhost to 127.0.0.1.
Solution
Set the following options for the mongoose client connection options:
mongoose.connect(url, {
maxIdleTimeMS: 80000,
serverSelectionTimeoutMS: 80000,
socketTimeoutMS: 0,
connectTimeoutMS: 0
}
These options stop the mongoose client from timining out the connection before it selects a primary from the DB cluster.
Please remember to set any other options relevant to your setup like if you are running a cluster, make sure to set the replicaSet option or if you have authentication enabled, set the authSource option or the database you are connecting to dbName.
Some of these options can be set in the connection string.
The full list of options can be found here: https://mongodb.github.io/node-mongodb-native/4.2/interfaces/MongoClientOptions.html#authSource
Im developing a nodejs application that needs to register to the AWS service discovery on the app start. I'm using the #aws-sdk/client-servicediscovery lib in my node application. As a reference, I am using the code from here: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-servicediscovery/index.html.
const { ServiceDiscoveryClient, CreateHttpNamespaceCommand } = require("#aws-sdk/client-servicediscovery");
const client = new ServiceDiscoveryClient({ region: "xxx" });
const params = {
"ServiceId":"xxx",
"InstanceId":"xxx",
"CreatorRequestId":new Date(new Date().toUTCString()),
"Attributes": {
AWS_INSTANCE_IPV4: "xxx.xx.xx.xx",
AWS_INSTANCE_PORT: xxx,
service: "xxx",
}
};
const command = new CreateHttpNamespaceCommand(params);
client.send(command).then(
(data) => {
console.log(data)
},
(error) => {
console.log(error)
}
);
While running the app, getting these errors.
/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5
const { readFile } = fs_1.promises;
^
TypeError: Cannot destructure property `readFile` of 'undefined' or 'null'.
at Object.<anonymous> (/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/slurpFile.js:5:27)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/application/node_modules/#aws-sdk/shared-ini-file-loader/dist-cjs/loadSharedConfigFiles.js:8:21)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
I am new to the AWS SD so need some help registering my service to the service discovery. Thanks in advance!
I've an error comes when I execute (npm start) :
TypeError: Cannot read properties of undefined (reading 'database')
at Object.<anonymous> (C:\Users\Ahmed Ra'fat\Downloads\AnotherDayWeb-master\AnotherDayWeb-master\passport.js:11:45)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\Ahmed Ra'fat\Downloads\AnotherDayWeb-master\AnotherDayWeb-master\index.js:24:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
I guess this is because environment variables are not defined well in passport.js
database is defined as the following:
local: {
//url to be used in link generation
url: 'http://localhost:8081/',
database: {
host: 'aa1kiub44j3f2lh.cch13jyjjqb1.us-west-2.rds.amazonaws.com',
user: 'anotherdayuser',
pw: 'anotherdayuser',
port: '3306',
db: 'ebdb'
},
and I call it in passport.js as the following:
var connection = mysql.createConnection({
host: config[""+process.env.env_var+""].database.host,
user: config[""+process.env.env_var+""].database.user,
password: config[""+process.env.env_var+""].database.pw,
database: config[""+process.env.env_var+""].database.db
});
The error statement:
Error: Dialect needs to be explicitly supplied as of v4.0.0 at new Sequelize (D:\tutorial\node_modules\sequelize\lib\sequelize.js:181:13) at Object. (D:\tutorial\util\database.js:3:19) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (D:\tutorial\app.js:9:19) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Function.Module.runMain (internal/modules/cjs/loader.js:742:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
My database request is:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('node_complete', 'root', 'Kornienko130', 'nodecomplete', {
dialect: 'mysql',
host: 'localhost',
});
module.exports = sequelize;
The dialect is explicitly stated, I don't know what's wrong, I googled it so many times and none of the solutions seems to work.
Im fresh to node.js. Im testing a node.js project.And I meet some problems. At first , I connect severals postgresql database, every goes well.But after added two new connection to postgrelSql database, here is my connection code:
var dbConfig = config.dbOrderV2;
exports.ORDERV2 = {};
exports.ORDERV2 = new Sequelize(dbConfig.dbname, dbConfig.username, dbConfig.password, {
dialect: dbConfig.dialect,
host: dbConfig.host,
port: dbConfig.port,
timezone: '+08:00',
logging: undefined,
pool: {
maxConnections: dbConfig.pool
}
});
var dbConfig = config.dbPay;
exports.PAY = {};
exports.PAY = new Sequelize(dbConfig.dbname, dbConfig.username, dbConfig.password, {
dialect: dbConfig.dialect,
host: dbConfig.host,
port: dbConfig.port,
timezone: '+08:00',
logging: undefined,
pool: {
maxConnections: dbConfig.pool
}
});
I run up the project and here is the warning:
(node) warning: possible EventEmitter memory leak detected. 11 exit listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at process.addListener (events.js:239:17)
at ConnectionManager (/Users/yangtao/Documents/code/zhike/data_import_export/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:45:11)
at new ConnectionManager (/Users/yangtao/Documents/code/zhike/data_import_export/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:13:29)
at new PostgresDialect (/Users/yangtao/Documents/code/zhike/data_import_export/node_modules/sequelize/lib/dialects/postgres/index.js:12:28)
at new Sequelize (/Users/yangtao/Documents/code/zhike/data_import_export/node_modules/sequelize/lib/sequelize.js:213:18)
at Object.<anonymous> (/Users/yangtao/Documents/code/zhike/data_import_export/src/common/dbs.js:185:19)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/yangtao/Documents/code/zhike/data_import_export/src/common/global.js:21:14)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/Users/yangtao/Documents/code/zhike/data_import_export/dummyWorker.js:3:1)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
Can anybody help me to fix it up? thanks.