Reputation: 507
I have a RDS postgreSQL intsnce in my AWS. In my RDS PostgreSQL instance public accessibilty is 'NO' and i have my own VPC and private subnet. I choose that private subnet and VPC for my instance. I created a lambda function to connect the same RDS instance using the endpoint and checked. It was successfully connected.
Now, I want to access the RDS instance and create some table on this RDS postgreSQL instance. How could I access this RDS Instance and how to create tables on the same?
I installed AWS CLI in my system. I used below command to connect with AWS CLI.
psql -h endpoint -p 5432 -U username Databasename
But i am getting an error like "connection time out". But opened the port 5432 for the same security group.
Is there any way to connect using AWS CLI? I have an EC2 instance also.
Is there anyone to help me? This would be great if you can help me in this. Waiting for your favorable replay..
Upvotes: 1
Views: 3006
Reputation: 1063
There are two ways you can connect to a Database in Private subnet -
Using Nat
First ssh into NAT using public IP or elastic IP.
ssh -i key(pem) [email protected](Public IP)
Then you can Run the command directly. you don`t need any AWs CLI or boto for this
psql -h endpoint -p 5432 -U username -p Databasename
From your system, you can create a tunnel through NAT. for that you need to run this on a command line. you only Need psql to be installed on your system or NAT.
ssh -i key.pem -N -q -o "StrictHostKeyChecking=no" -L 54320:ENDPOINT:5432 key_user@PublicIP &
& will ensure that process will run in the background
Ensure that your process is running in Background after this by ps -ef | grep ssh After that, you can connect to psql using
psql -h 127.0.0.1 -p 54320 -U username -p
Here 54320 is the local port which will be used for connecting over a tunnel.
For Security Group Configuration Make sure you have these inbound rules -
Upvotes: 2
Reputation: 6109
Since you Mentioned you are new to AWS I will try to answer accordingly :
You can connect to your aws ec2 instance via Putty , Please follow this Documentation
Now this is assuming that your ec2 is public subnet and RDS is in Private Subnet of Same VPC
Now Assuming that both EC2 and RDS have different security groups firstly you will have to :
To create a rule in a VPC security group that allows connections from another security group, do the following:
Now you can Connect using your normal sql connect command :
mysql -h myinstance.123456789012.us-east-1.rds.amazonaws.com -P 3306 -u mymasteruser -p
Also for More Detailed Guide you can refer to AWS Guide
Hope this Helps you!
Upvotes: 0
Reputation: 779
Since your RDS instance is in Private Subnet and is configured not to have public access, you wont be abke to access it directly from your system outside thr VPC.
What you can do here is to do an ssh/rdp to your ec2 instance (assuming its having elastic ip/public dns) using putty or terminal from your system and then access the rds from your ec2 instance.
You should enable access to port 22 (ssh) or port 3389 (rdp) to access your ec2 instance from your system
Upvotes: 0
Reputation: 4436
You will need to first connect to an ec2 instance in a public subnet in same vpc from which you can then connect to RDS instance.
Upvotes: 0