An EC2 instance cannot reach its RDS database
Can't connect to RDS instance from EC2 instanceShort 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.
Fix it fast
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>2Match what you see, then jump to its fix
- 1
nc hangs and times out, while the DB status is availableThe DB security group has no inbound rule for the EC2 instanceFix - 2
ping to the endpoint fails, but the application was never tested on the real portYou are testing with ping, which RDS does not answerFix - 3
The status is creating, modifying or something other than availableWrong endpoint or port, or the DB instance is not available yetFix
- 1
What does the error look like?
Can't connect to RDS instance from EC2 instanceCould 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> 3306Confirmed by docs.aws.amazon.com
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> 33063Verify
A success message means the security group is configured properly.
mysql -h <db-instance-endpoint> -P 3306 -u <user> -pConfirmed by docs.aws.amazon.com
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Your laptop cannot reach an RDS database, and ping does not work either
Ping to the RDS endpoint gets no reply
When a client times out against an RDS endpoint, the network path is blocked, not the database login.
4 causes, each with a fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fix