Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudAdelAli's avatar

Point a public Instance with RDS on Private instance

I trying to deploy a laravel project on AWS instances , so i created private one and public one and both in same VPC , so i have installed our laravel project and all necessaries , with .env


DB_CONNECTION=mysql
 DB_HOST=saas-database-1.c2fge6icih75.us-east-1.rds.amazonaws.com
 DB_PORT=3306
 DB_DATABASE=saas-database-1
 DB_USERNAME=saasy
 DB_PASSWORD=XXXXX

and here i pointed my database to the private instance , but i can't connect to the private one , I thinking about using REDIS , but i don't know if that the Official usage for that , Does any one have idea how to do that ?

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! Here’s how you can solve your issue:

Problem Recap:
You have a Laravel project deployed on a public EC2 instance. Your RDS MySQL database is in a private subnet (private instance) within the same VPC. You cannot connect to the RDS instance from your public EC2.

Solution Steps:

  1. Check Security Groups:

    • Ensure the RDS security group allows inbound MySQL (port 3306) connections from the EC2 instance's security group (not from 0.0.0.0/0).
    • Example:
      • Type: MySQL/Aurora
      • Protocol: TCP
      • Port Range: 3306
      • Source: The security group of your public EC2 instance (e.g., sg-xxxxxx).
  2. Check Network ACLs and Routing:

    • Both EC2 and RDS should be in the same VPC (which you already have).
    • The public EC2 should have a route to the private subnet (this is usually set up by default in the same VPC).
  3. Check Subnet Configuration:

    • RDS should be in a private subnet (no direct internet access).
    • EC2 is in a public subnet (has a public IP and access to the internet).
  4. Check RDS Endpoint:

    • Use the endpoint provided by AWS RDS, not the IP address.
    • Your .env looks correct in this regard.
  5. Test Connection from EC2:

    • SSH into your EC2 instance.
    • Try connecting to the RDS instance using the mysql CLI:
      mysql -h saas-database-1.c2fge6icih75.us-east-1.rds.amazonaws.com -u saasy -p
      
    • If this fails, the issue is likely with security groups or network ACLs.
  6. No Need for Redis:

    • Redis is not a replacement for MySQL. Redis is an in-memory cache/store, not a relational database.
    • You should not use Redis to "bridge" connections to MySQL.
  7. Double-Check Laravel Configuration:

    • Your .env is correct:
      DB_CONNECTION=mysql
      DB_HOST=saas-database-1.c2fge6icih75.us-east-1.rds.amazonaws.com
      DB_PORT=3306
      DB_DATABASE=saas-database-1
      DB_USERNAME=saasy
      DB_PASSWORD=XXXXX
      

Summary:
You do not need Redis.
Just ensure your RDS security group allows inbound connections from your EC2 instance’s security group on port 3306.
After that, your Laravel app on the public EC2 will be able to connect to the RDS in the private subnet.

If you need to set the security group rule:

# In AWS Console:
# Go to RDS > Databases > Your DB > Connectivity & security > Security group rules

# Add inbound rule:
Type: MySQL/Aurora
Protocol: TCP
Port: 3306
Source: <EC2 instance's security group>

Test again after updating the security group.

Let me know if you need help with any of these steps!

MahmoudAdelAli's avatar

@Snapey Can't install mysql on the private one , else can't connect via mysql from the public-one

Please or to participate in this conversation.