Friday, February 4, 2022

[SOLVED] Error when checking connection between ec2 insatnce and rds database

Issue

I want to test if my ec2 instance can connect to my database.

I ssh into my instance ran the command:

echo '\dS' |psql -h rds-endpoint-xxx.amazonaws.com 5432
  • the result I get is this:
psql: error: could not connect to server: Operation timed out
    Is the server running on host "rds-endpoint-xxx.amazonaws.com" (192.168.12.233) and accepting
    TCP/IP connections on port 5432?

What does this mean? and what should i do to fix?

  • My ec2 instance IP is 192.168.0.138 and in the security group of my rds database I have allowed inbound access from 192.168.0.0 enter image description here

Solution

Unless your two VPC are peered, you will not be able to access your RDS from the instance due to your security group (SG). Without peering, the connection from the instance to the rds will go over internet, and your SG does not allow internet connections.

To solve this, you either have to peer your VPCs, which you can't do right now as they have same CIDRs. For peering you need non-overlapping CIDRs for the VPC.

Alternative is to allow internet connections to your RDS, which means you need to allow incoming traffic (0.0.0.0/0) unless you have EIP for the instance. Also RDS must be set to be allow public connections.



Answered By - Marcin
Answer Checked By - Pedro (WPSolving Volunteer)