AWS RDS responds with (Got an error reading communication packets). Works locally I've been on this issue for a couple of days now, I will cover things I've tried at the end. So I'm running a NodeJS server from a GitHub repo using netlify-lamda. I am connecting to an Amazon RDS (MySQL). So the strangest thing is this works locally however when I bring it over to Netlify I get an error.

I'm sending the HTTP requests using Postman.

My db.js file:

var mysql = require("mysql");
var db;
require("dotenv").config();

function connectDatabase(){
    if(!db){
        db = mysql.createPool({
            connectionLimit: process.env.CONN_LIMIT,
            host: process.env.DB_HOST,
            user: process.env.DB_USER,
            password: process.env.DB_PASS,
            database: process.env.DB_NAME,
            port: process.env.DB_PORT
        });
    }
    return db;
}

module.exports = connectDatabase();


To query the DB I do the following:

router.post('/login',(req,res) =&gt; {
    var username = req.headers.username;
    var password = req.headers.password;
    var sql = `SELECT password FROM admin WHERE username= ?`;
    db.query(sql,[username],function(err,results,query){
        if(err){
            res.status(401);
            res.send("Unauthorized-sql err"); // This is where the error occurs.
        } 
        else if(!err){
            if(crypt.decrypt(results[0].password).match(password)){
                res.status(200);
                res.send("Authorized");
            }else{
                res.status(401);
                res.send("Unauthorized");
            }
        }
    });
});


RDS Error Log:

2020-04-03T14:11:31.874911Z 22 [Note] Aborted connection 22 to db: 'db' user: 'admin' host: 'xxx.xxx.xx.xx' (Got an error reading communication packets)
2020-04-03T14:11:31.877847Z 29 [Note] Aborted connection 29 to db: 'db' user: 'admin' host: 'xxx.xxx.xx.xx' (Got an error reading communication packets)
----------------------- END OF LOG ----------------------


NodeJS Error Log

6:17:18 PM: 2020-04-03T17:17:18.120Z    1d561b89-f8a1-4e78-9d7c-61dac84ea37f    INFO    Error: connect ECONNREFUSED 127.0.0.1:3306    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16)

at Protocol._enqueue (/var/task/admin.js:29255:48)
at Protocol.handshake (/var/task/admin.js:29162:23)
at PoolConnection.connect (/var/task/admin.js:9041:18)
at Pool.getConnection (/var/task/admin.js:27212:16)
at Pool.query (/var/task/admin.js:27366:8)
at /var/task/admin.js:27556:6
at Layer.handle [as handle_request] (/var/task/admin.js:7030:5)
at next (/var/task/admin.js:6641:13)
at Route.dispatch (/var/task/admin.js:6616:3)
at Layer.handle [as handle_request] (/var/task/admin.js:7030:5) 
{  
errno: 'ECONNREFUSED',  
code: 'ECONNREFUSED',  
syscall: 'connect',  
address: '127.0.0.1',  
port: 3306,  
fatal: true
}   


Things I've tried:


Changing the PORT.
Within the DB instance I'm allowing all connections from any IP address.
Setting a different PORT for the NodeJS instance.
I've now switched to using a MySQL pooling technique, the error is still there.
Preventing the minification/encoding of the code (for the DB instance in particular as that's an issue with the mysql npm package)


Sidenote: It's my first time working with AWS RDS so it's highly likely that the error is something very very stupid.
I encountered this in our QA environment. I spent a great deal of time enabling AWS RDS Audit plugin, improving performance and setting logs everywhere.
The way to ressolve it was to do a proper handling of the JDBC connections from the Java client app.
Please refer to the following question that relates to what you are asking here.
RDS has a lot of messages got an error reading communication packets