0

I have a NodeJS/Serverless Framework API up and running on AWS Lambda. It is connected to an AWS RDS which was working fine, however all of a sudden, it can't connect to the RDS Instance only on my office internet. The API and database connection works fine on my home internet and even on mobile data.

Database Connection Error

SequelizeConnectionError: connect ETIMEDOUT

The API works fine when deployed on AWS Lambda & EC2 Instance (successful database connections). Security Group has inbound traffic access open for PORT 3306

Database Connection File

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}`)
    }
}

Solutions Tried

  • Setup a dummy database at remotemysql.com. API connects to database on my office internet.
  • Ran code as a simple NodeJS script. Did not work on office internet from local machine. Tried on multiple local machines.
  • Deployed simple NodeJS code to a new EC2 instance. It connected to database API working fine.
  • Removed Sequelize and connected to RDS Instance using npm mysql package. Connected to RDS from local machine using office internet.
  • Connected to the RDS Instance from Laravel (PHP) repository on local machine using office internet. Worked!!
  • Changed database connection config settings. 1) Increased acquire time to 1000000. 2) Added ssl:true & dialectOptions{ ssl: "Amazon RDS" }.
  • Added subnet IDs & Security Group IDs in serverless.yml file. So lambda and RDS instance are in the same VPC.
  • Tried multiple RDS instances. Connection fails on developement & staging instances.
  • Created new RDS instance for testing. It connects to database (sometimes) from local machine on office internet.

You can find the repo here.

Haseeb Burki
  • 676
  • 2
  • 8
  • 23

1 Answers1

0

To get the Lambda working check a couple of things:

  1. Is the Lambda in the same VPC as the RDS instance? Does your network settings in the AWS Console show this?
  2. Do you provide credentials in your Lambda to access your RDS instance or does your Execution role need to have that access?
  3. Does your RDS Security Group allow a connection from the subnet that the Lambda is in?
  4. Is your subnet an internal one that needs a bridge or gateway to access RDS? (Try using the same subnet as the EC2 that worked)
  5. Do you need to set the CA cert for RDS on you settings? For RDS you normally needs this..
dialectOptions: {
 ssl: {
   ca: 'certPathDownloadedFromAWS'
 }
}
bach942
  • 77
  • 1
  • 9
  • 1) Lambda and RDS are in the same "default" VPC. 2) We provide database credentials. 3) SC has allowed inbound traffic to all IP addresses. 4) VPC is the default one AWS sets up itself. I have not messed with it. 5) Added certificate for RDS in dialectOptions, did not work. – Haseeb Burki Apr 11 '19 at 11:19
  • What I don't understand is, why is it working on everyother internet except for the one I use in the office? :/ – Haseeb Burki Apr 12 '19 at 06:10
  • Do you use a VPN at your office? If you are connected to a VPN sometimes your traffic is routed differently and doesn’t have access to your VPC. – bach942 Apr 12 '19 at 11:32
  • No we don't. I don't know whats changed but the API started working on our office network. I still don't understand why this happened. Maybe RDS blocked the IP because of too many requests ?!?!? but that doesn't make sense either :| – Haseeb Burki Apr 16 '19 at 08:53