Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

An EC2 instance cannot reach its RDS database

ErrorCan't connect to RDS instance from EC2 instance

Short answer

RDS denies all access until a security group allows it. For an EC2 client, add an inbound rule on the DB instance's security group that names the EC2 instance's security group as the source, on the DB port. Also check the endpoint and port, that the DB status is available, and test with nc, because RDS ignores ping.

  • Applies to Amazon RDS for MySQL and PostgreSQL, Amazon EC2, Linux client
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Test the TCP connection to the DB endpoint and port from the EC2 instance. Do not use ping; RDS does not accept ICMP.

    nc -zv <db-instance-endpoint> <port>
  2. 2Match what you see, then jump to its fix

    1. 1nc hangs and times out, while the DB status is availableThe DB security group has no inbound rule for the EC2 instanceFix
    2. 2ping to the endpoint fails, but the application was never tested on the real portYou are testing with ping, which RDS does not answerFix
    3. 3The status is creating, modifying or something other than availableWrong endpoint or port, or the DB instance is not available yetFix

What does the error look like?

  • Can't connect to RDS instance from EC2 instance
  • Could not open connection to the host, on port <port>: Connect failed

Cause 1 of 3

The DB security group has no inbound rule for the EC2 instance

1Check

By default DB instances allow no access. Access comes from a security group on the DB instance. The rule's source can be an IP address, a range, or another VPC security group.

aws rds describe-db-instances --db-instance-identifier <db-id> \
  --query "DBInstances[0].VpcSecurityGroups"

2Fix

Add an inbound rule to the DB security group for the DB port, with the EC2 instance's security group as the source.

aws ec2 authorize-security-group-ingress \
  --group-id <db-security-group-id> \
  --protocol tcp --port 3306 \
  --source-group <ec2-security-group-id>

3Verify

Repeat the connection test from the instance.

nc -zv <db-instance-endpoint> 3306

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

You are testing with ping, which RDS does not answer

1Check

Amazon RDS does not accept ICMP traffic, including ping. A failed ping says nothing about the database port.

nc -zv <db-instance-endpoint> <port>

2Fix

Test the database port itself with nc. On success it prints that the connection succeeded.

nc -zv <db-instance-endpoint> 3306

3Verify

A success message means the security group is configured properly.

mysql -h <db-instance-endpoint> -P 3306 -u <user> -p

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

Wrong endpoint or port, or the DB instance is not available yet

1Check

Read the endpoint, the port and the status from RDS. A new instance cannot take connections until its status is available, and the port you configured at creation is the one to use.

aws rds describe-db-instances --db-instance-identifier <db-id> \
  --query "DBInstances[0].[DBInstanceStatus,Endpoint.Address,Endpoint.Port]"

2Fix

Wait for available, and put the endpoint address and the port from this output in the application configuration.

DB_HOST=<endpoint-address>
DB_PORT=<endpoint-port>

3Verify

Connect with the client using exactly those values.

nc -zv <endpoint-address> <endpoint-port>

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Let the application's security group be the source of the DB rule, so new instances in that group work without edits.
  • Keep the DB endpoint and port in one configuration value, copied from RDS.
  • Test connectivity with nc or the database client, never with ping.
  • Keep the DB instance non-public when only EC2 in the same VPC needs it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.