Error connecting to AWS RDS DB from netlify function - works locally

I am trying to convert a node express api that was previously running an an AWS ec2 linux 2 ami into a netlify function. One endpoint in the api calls a RDS MySQL DB.

I am using the demo netlify express app to test. I have it working locally but when I deploy it, I get an error in the response

    "errorType": "TypeError",
    "errorMessage": "n is not iterable",
    "trace": [
        "TypeError: n is not iterable",
        "    at _._callback (/var/task/server.js:159:45687)",
        "    at _.p.end (/var/task/server.js:13:2747)",
        "    at /var/task/server.js:159:43264",
        "    at c.<anonymous> (/var/task/server.js:159:41337)",
        "    at c.<anonymous> (/var/task/server.js:71:1566)",
        "    at c._callback (/var/task/server.js:71:1354)",
        "    at c.p.end (/var/task/server.js:13:2747)",
        "    at /var/task/server.js:320:77668",
        "    at Array.forEach (<anonymous>)",
        "    at /var/task/server.js:320:77645"

I think it is not connecting successfully to the DB or the query is returning 0 results.

I thought it had to do with some settings on the RDS instance but everything in the inbound rules looks wide open.

Is there any example or docs on how to connect to a mysql DB the right way or a guide on what an RDS instance should look like in terms of inbound/outbound rules or security group settings?


Hi, @Ju66ernaut, and welcome to our Netlify community site.

I replied about this here also:

Finding the root cause will involve discovering the difference in the two function deployments (local versus at Netlify). Verbose logging is one of the best ways to discover this difference.

Hi @luke,
When I run the app locally the command yarn start just runs nodemon and the server-local.js file.

When it is deployed I see it runs npm install && npm run build I am not able to run that command successfully locally.

When i do, I get an error in the console saying Critical dependency: the request of a dependency is an expression in WARNING in ../node_modules/express/lib/view.js 81:13-25.

A lot of similar threads online keep pointing to the mysql package not working with webpack and to create a separate webpack.config.js file or a webpack-config.function.js file but I can not seem to find a definitive way to do it.

I have also looked in the netlify lambda examples for how to connect to a mysql DB but have not seen anything. Are there any you know of?

Thank you.

If you aren’t able to run npm run build locally, then the issue is definitely somewhere in the code, which is hinted at by that warning. You could try a query builder like knexjs to act as a middle layer between your express app and your mysql database. I don’t have any examples handy but using knexjs removes the need for using the mysql npm package directly.

Let me know if that works for you.

Thank you I will try that and respond if it is successful or not.

1 Like

hi @Dennis

When using knexjs I kept getting errors for other clients that weren’t installed (like mssql, oracle, etc).

Apparently knexjs has a bug with trying to figure out which client to use when making the connection. I went so far as to installing every other supported knexjs client just to see if adding them all would help but it still didn’t work.

All the solutions I have seen mention modifying the webpack.config.js to exclude the unused db clients but I can’t for the life of me figure out how to make my local webpack.config.js work with netlify when it is deployed as it seems to either be ignored or something.

I tried modifying my netlify.toml build command to point to my webpack.config.js/(webpack.functions.js)? as well.

I am also confused about the webpack.functions.js I keep seeing it mentioned in the netlify docs. Is this supposed to be the same file as thr webpack.config.js?

Unfortunately, I’m not familiar with the issue. Are you using netlify-lambda to deploy your function? Have you tried foregoing that and using our buildbot’s built-in ‘zip-it-and-ship-it’ feature instead?