Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection timed out using Amazon RDS and EC2

I'm trying to connect from an EC2 instance to RDS instance with no success using php.

I tried with these 3 lines to establish a connection (I'm using MysqliDb btw):

$db = new MysqliDb ('rds_endpoint', 'username', 'password', 'databaseName');
$db = new MysqliDb ('rds_endpoint', 'username', 'password', 'databaseName', 3306);
$db = new MysqliDb ('rds_endpoint:3306', 'username', 'password', 'databaseName',);

And when I try to get a table it gives me 2002 Connection timed out.I checked and the table exists.

Also I checked the Security Groups of both, RDS and EC2 and EC2 inbound rules accepts from all the ips and RDS inbound rules accepts my EC2 instance public IP. I have another Amazon account and tried the above and worked fine, so I don't know where to find errors.

Thanks

like image 263
VictorBG Avatar asked Feb 02 '26 15:02

VictorBG


1 Answers

Turning this into an answer:

You'll want to whitelist your EC2 instance's private IP, not the public one. Better yet, whitelist its security group instead.

Instances within AWS will connect to an RDS via the EC2 and RDS instances' internal IP addresses, not their external internet-facing ones.

Whitelisting the security group instead of individual IPs means an IP change to the EC2 server will not revoke its RDS access, and means launching new instances into that security group will automatically get the right access.

like image 104
ceejayoz Avatar answered Feb 05 '26 04:02

ceejayoz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!