0

I'm trying to connect to my AWS rds postgres database through my Google Firebase Functions backend. After some trial and error, I successfully managed to connect to it locally (ran firebase emulators:start --only functions and successfully queried the DB). However, I am running into a { Error: connect ECONNREFUSED 127.0.0.1:3306 when I deploy my function to the remote Google Firebase Functions server.

I have set my AWS RDS database to publicly accessible. I know that I need to allow inbound traffic from my Google Firebase server, but from what I managed to dig up, this shows that the Availability Zone I am using (us-central-1) has dozens of IP addresses associated with it. I gave up and allowed all incoming traffic (Yes I know that's bad but I am not in production just yet). However, I am still getting the same error!

I have also read this and this to no avail.

Any ideas on how to get this working, and how to connect it properly so I'm not allowing all incoming traffic?

Below is how I am connecting to my aws db in my firebase function. The function itself (where the code is stored) is us-central:

  const {Pool} = require('pg');

  private static readonly config = {
    user: 'xxxxxxxx',
    host: 'xxxxxxx.yyyyyyyyy.us-east-2.rds.amazonaws.com',
    database: 'postgres',
    password: 'xxxxxxxxxx',
    port: 5432
  };
  private static readonly pool = new Pool(RemoteDBHandler.config);

Edit: Somebody tagged my question as duplicate so I made a copy here. Looking at it now, it is odd that the connection is being attempted on localhost (127.0.0.1). I have no idea why that's happening, since I am making the connection to the aws server. Therefore, I don't think this applies to me

Random_User
  • 71
  • 1
  • 8
  • Can you share the entire code of your `index.js` file. Can you show how do you deploy. Also, on which Firebase pricing plan are you? (PS: It is me who marked your previous similar question as duplicate since it seems that your Cloud Function generates the exact same error). Can you please explain why you think that the duplicate doesn't apply to you? Please add the above requested info in order to confirm it is not a duplicate. – Renaud Tarnec Aug 08 '20 at 18:06
  • Your question needs to show the complete, minimal code that isn't working the way you expect, in addition to any error messages generated. https://stackoverflow.com/help/minimal-reproducible-example – Doug Stevenson Aug 08 '20 at 18:16
  • Thanks for the comments. @RenaudTarnec The reason I don't think the duplicate applies to me is because in the duplicate, the OP is trying to connect/listen on localhost, as evidenced by this piece of code: const req = http.request({ host: 'localhost', port: 443, path: '', method: 'GET', agent: agent, } I have specified my host to be the aws server that I am connecting to, not localhost. It seems like OP was trying to either connect to his computer or run the functions locally. Either way, not what I wanted to do – Random_User Aug 08 '20 at 22:16
  • @DougStevenson I will be updating my code promptly – Random_User Aug 08 '20 at 22:20
  • Also I am on Blaze plan if that matters – Random_User Aug 08 '20 at 22:21

1 Answers1

1

Looks like the issue was actually with how I was calling the functions API itself. I was accidentally sending an https request to XXXXX/widgets/widgets/route instead of XXXXX/widgets/route. Basically, I had my function name in there twice.

Another issue, which may be more relevant to later readers, is that because I was porting over a fully functional express app, I still had a method that I was calling in the index.ts that started up the express server. On Google Functions, this isn't necessary, and may have been another source of my headaches

Random_User
  • 71
  • 1
  • 8