Connecting to amazon RDS from netlify functions

Hello all, I’m having an issue with netlify functions where I cannot connect to my amazon RDS instance. The lambda function simply aims to make a request to one of my tables, and return the info. I can run this all just fine locally and it connects fine, but once deployed I’m getting the following error:

Mar 4, 09:04:06 PM: 5d09c703 INFO unknown err Error: ER_ACCESS_DENIED_ERROR: Access denied for user ‘admin’@‘ec2-x-xxx-xxx-xx.compute-1.amazonaws.com’ (using password: YES)
at Handshake.Sequence._packetToError (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
at Handshake.ErrorPacket (/var/task/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
at Protocol._parsePacket (/var/task/node_modules/mysql/lib/protocol/Protocol.js:291:23)
at Parser._parsePacket (/var/task/node_modules/mysql/lib/protocol/Parser.js:433:10)
at Parser.write (/var/task/node_modules/mysql/lib/protocol/Parser.js:43:10)
at Protocol.write (/var/task/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket. (/var/task/node_modules/mysql/lib/Connection.js:88:28)
at Socket. (/var/task/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
--------------------
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/var/task/node_modules/mysql/lib/Connection.js:116:18)
at Connection._implyConnect (/var/task/node_modules/mysql/lib/Connection.js:454:10)
at Connection.query (/var/task/node_modules/mysql/lib/Connection.js:196:8)
at internal/util.js:340:30
at new Promise ()
at Connection.query (internal/util.js:339:12)
at PortfolioImages.getTotalPages (/var/task/common/dao/PortfolioImages.js:67:47)
at Runtime.module.exports.handler (/var/task/functions/images.js:53:49) {
code: ‘ER_ACCESS_DENIED_ERROR’,
errno: 1045,
sqlMessage: “Access denied for user ‘admin’@‘ec2-x-xxx-xxx-xx.compute-1.amazonaws.com’ (using password: YES)”,
sqlState: ‘28000’,
fatal: true
}

And here is the code for connecting where it fails:

const sql = require(‘mysql’);
console.log({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
});
const connSQL = sql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
});

The console.log outputs my environment variables just fine, so that is not the issue (like in this other thread i found Trouble connecting to AWS RDS DB with Netlify Functions)

Over on aws here are the security rules for my database, as i understand this should open it up to the entire internet:

As I mentioned before I have no issues connecting locally and I cant seem to find any answers, so any help is appreciated :slight_smile:

Hey there, @reubunbun :wave:

Welcome to the Netlify Forums and thanks for reaching out! Apologies for the delay. Have you made any progress on this obstacle in the past six days? If not, can you please share a link to your site as well as the function name you are using? Additionally, sharing any debugging steps you have already taken would be beneficial.

Thanks so much!

Hey Hillary,
Thank you for responding :slight_smile:
I actually gave up on this soon after i posted and opted to just deploy my own aws lambda and call that instead which is working, but since you responded I just tried putting the netlify function back in to try again and… it works! I have no idea why it didn’t before :smile:

Embarrassingly most likely reason it wasn’t working before is probably because I didn’t copy and paste my database environment variables correctly into the netlify settings, as after i gave up on this a week ago I deleted them, so I probably just copied them in correctly this time. I logged out the environment variables last time and i thought they were all correct, but i guess its very easy to miss that a long string of random characters and symbols is missing one or two characters :grin:

Hey there, @reubunbun :wave:

Let’s call that a win that it is working now-- no embarrassing mistakes here!

Thanks for choosing Netlify, and happy building :netliconfetti: We will be here should you encounter future obstacles.

1 Like