Trouble connecting to AWS RDS DB with Netlify Functions


I have some issues with Netlify Functions where I have a Express App that serves a few endpoints.
Those endpoints works perfectly fine locally and with ‘netlify dev’.

I have connected the Express App and endpoints with my AWS RDS Aurora Database and locally it works fine. But as soon as I deploy the app to Netlify the endpoint doesn’t work anymore.

The error i get is following:

Any help towards fixing this issue is much appreciated.

I think, this is where the problem lies:

You seem to be connecting to localhost.

Correct, but where do I change this to get the connection to work for my Database?

Maybe I got this all wrong (Probably), but is what I am trying to achieve here not possible with Netlify?
I try to achieve the following:
REST Api located on Netlify (Using Netlify Functions) which handles all my Express endpoints with Serverless functions. This REST Api has the connection to my Database.
These endpoints should be accessable from my other App located somewhere else, that sends CRUD requests to my Netlify REST Api ?

If this is not possible with Netlify then that’s probably just the issue.

From what I’ve understood, it seems like it’s possible. I currently have a setup in which I’m using FaunaDB to store the data. From my client-side JS, I send a request to my Netlify Function, which fetches the data from my database and returns it to the client-side JS. Is this something you’re trying to do as well?

About this, I’m not sure as I don’t know how AWS RDS DB works. Every service has a different way to setup. For example, FaunaDB only needs a API token, FirebaseDB needs a few options and so on. So, I think the answer to your question might exist in AWS docs. Maybe you need to configure the URL in Environment Variable, or maybe in your serverless function itself, I have no idea. I’ll make sure to update the thread if I find the answer.

This is exactly what I am looking for. I am just trying to get this to work with RDS.
I am currently looking at AWS RDS docs and Netlify Functions to try and spot the error. Do you know if this could be something about my environment variables?

My current DB connection is setup in .env and required on server.js to make the DB Connection. But does the .env get accessable on the Netlify Functions build?

Having .env file is less secure as you’re just exposing your secret in your repo. You should consider keeping it in .gitignore. Instead, you can add Environment Variables to Netlify UI: Build environment variables | Netlify Docs

My .env file is gitignored, I just wanted to make sure the actual file is compatible with Netlify Functions. From what i’ve read it should be.

I haven’t personally tried it, but yes, it should be. It’s based on the fact that it works perfectly fine with Netlify Dev.

I’m not sure if this helps, but this is how you can connect to your database probably: Creating a MySQL DB instance and connecting to a database on a MySQL DB instance - Amazon Relational Database Service

I think you need to specify that URL whenever you’re trying to connect.

The connection with the database works perfectly fine locally and with MySQL Workbench.
There isn’t anything that indicates that my connection to the database shouldn’t work (Atleast what I can see).

I also enabled all traffic towards my RDS.

Okay so I found the solution for my problem.

It seems I was wrong (Was most likely anyway).
Netlify is looking for the .env file in my repo and since (For obvious reasons) my .env is not public available.

Therefore MySQL connection returns undefined in all my process.env variables which my default will be localhost. That’s why it’s trying to connect to a local database and not my RDS.


Store your API_KEY in the Netlify build environment variables and build the .env using a script prior to running the build command.


// Using this to create env file based on Netlify UI Environments
const fs = require('fs')
fs.writeFileSync('./.env', `

Run the script as part of your build

node ./scripts/create-env.js && <your_existing_webpack_build_command>

Solution from:

Thanks for trying to help @hrishikesh :slight_smile:


Thanks for sharing your solution here, @AndreasL! This will definitely be beneficial for future Forums members who encounter something similar.