4

I'm using AWS RDS and I want to connect to the database remotely. However, I keep getting

Database connection failed due to connection to server at 
"mydb.12345.eu-central-1.rds.amazonaws.com" (x.xx.xxx.xx),
port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?

These seems to be a very common problem and all the solutions suggest inbound rules should be set to accept all traffic from you own IP.

However, for me this doesn't solve the issue.

This is my setup:

enter image description here

These are the inbound rules in security group sg-650cbe0b

enter image description here

I have also tried adding inbound rules such as:

enter image description here

or

enter image description here

But it didn't work.

I have tried connecting via my mobile network (to see if it's a firewall issue), but I got the same error.

However, I access this database from within a AWS Lambda function, and that works without problems.

This is the code I'm using to access the database:

import psycopg2
import sys
import boto3
import os

ENDPOINT="mydb.12345.eu-central-1.rds.amazonaws.com"
PORT="5432"
USER="admin"
REGION="eu-central-1b"
DBNAME="mydb"

#gets the credentials from .aws/credentials
session = boto3.Session()
client = session.client('rds')

token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION)

try:
    conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USER, password=token, sslrootcert="SSLCERTIFICATE")
    cur = conn.cursor()
    cur.execute("""SELECT now()""")
    query_results = cur.fetchall()
    print(query_results)
except Exception as e:
    print("Database connection failed due to {}".format(e))       
KarloSpacapan
  • 173
  • 2
  • 16
  • 1
    Is the DB instance [publicly accessible](https://stackoverflow.com/questions/22866490/how-do-i-change-the-publicly-accessible-option-for-amazon-rds)? Is it in a public subnet? Guide to [troubleshooting RDS connections](https://aws.amazon.com/premiumsupport/knowledge-center/rds-connectivity-instance-subnet-vpc/). – jarmod Feb 23 '22 at 18:20
  • 2
    Thanks for this link! I had 3 subnets listed, but the one associated with it only had `xxx.xx.x.x/16 > local`. I added `0.0.0.0/0 > internet gateway` and now it works. – KarloSpacapan Feb 23 '22 at 18:35

1 Answers1

1

There are a number of reasons that RDS connectivity could fail, including:

  • the RDS instance was not configured to be publicly accessible
  • it was launched into a private subnet and has no route to an IGW

I'd recommend the RDS connectivity troubleshooter: How can I troubleshoot connectivity to an Amazon RDS DB instance that uses a public or private subnet of a VPC?

jarmod
  • 71,565
  • 16
  • 115
  • 122