GOAL
Connecting EC2 Django API to RDS
Architecture
AWS VPC has two subnets
- 1 private with the RDS instance
- 1 public with my two EC2 servers
- Main server: Hosts Django API connected to an internet gateway
- Bastion server: Only authenticated SSH connections allowed, used for admin purposes on RDS instance
Background
I have established a successful connection on MySQL Workbench. SSH connection to EC2 server, then a connection to a MySQL RDS host is established. I am able to perform all C.R.U.D with this connection.
When I try and recreate this process manually on the command line, I can’t. I SSH to the EC2 server then try and connect to the RDS with SSH to MySQL at the same endpoint, user, port, and password but it fails.
When I try and run my Django server on the remove EC2 it also fails. I have the user, endpoint and password saved as environment variables. When I launch the Django Server it says connection failed.
This is frustrating since I am almost certain my AWS VPCs are configured correctly because I am able to connect via MySQL workbench, but when I try and connect manually or with Django, it does not work.
Terminal connection to EC2:
ssh -i [PRIVATE_KEY].pem [USER]@[IPv4_ADDRESS]
MySQL connection:
(venv) ubuntu@ip-10-0-1-146:~/BucketAPI$ mysql -h [DATABASE].[END_POINT].us-west-1.rds.amazonaws.com -P 3306 -u admin -p
Enter password:
ERROR 1045 (28000): Access denied for user 'admin'@'10.0.1.146' (using password: YES)
The RDS instance is only available to both the servers via inbound rules, it has an outbound rule. These are shown here:
RDS Configuration
Security Groups Configurations
Django: settings.py (Django config)
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bucket',
'USER': os.getenv('BUCKET_RDS_USER'),
'PASSWORD': os.getenv('BUCKET_RDS_PASS'),
'HOST': os.getenv('BUCKET_RDS_HOST'),
'PORT': '3306',
}
}
Terminal output screenshots: Imgur Link to Screenshots