Unable to access amazon RDS mysql instance from lightsail instance - amazon-web-services

I am new to AWS and not a network admin, mere a developer, and need your help.
I am unable to connect to my aws RDS (mysql) from my lightsail ubuntu instance. when trying to connect, it just wait for a minute and then fails.
I am unable to ping my RDS either.
here is the setup
the lightsail instance has vpc peering enabled in lon-zone-A
I have created a mysql RDS instance in aws and used default vpc peering. mysql is restricted to VPC and using default security group which has a rule for inbound - All traffic for default security group source
the default VPC have 2 subnets in CIDR 172.31.16.0/20 and 172.31.0.0/16 for two availability zone A and B.
In route table of the subnet, i have
172.26.0.0/16 as destination and target to vpc peering which further has
Requester VPC CIDRs 172.26.0.0/16
Accepter VPC CIDRs 172.31.0.0/16
My lightsail instance has private IP 172.26.15.xxx and in lon-Zone-A
When i ping my mysql intance, i get ip 172.31.10.9
command using to connect mysql -h xxxxxx.xxxxx.eu-west-2.rds.amazonaws.com -P 3306 -u db_master_username -p

To enable access from AWS Lightsail to AWS RDS you can accomplish in two separate ways:
Method 1.
Make RDS publicly accessible.
In RDS pick you instance and click 'Modify'. In section 'Network & Security' choose 'Publicly accessible' to Yes. Apply settings and wait until they are effective. Your RDS has public IP now.
Add your Lightsail public IP to the RDS security group inbound traffic.
Use CIDR: x.x.x.x/32 where x.x.x.x is your Lightsail instance public IP.
Method 2. (better, RDS with no public IP)
Make sure you Lightsail instance is in the same Availability Zone as RDS.
Set up VPC peering beetween Lightsail VPC and Amazon VPC.
Add your Lightsail local IP to the RDS security group inbound traffic.

I managed to solve. it.
I had to add my lightsail instance IP CIDR in the RDS inbound rule as mysql/aurora TCP allowed traffic.
:-)

Related

How do I share an RDS database to another VPC without allowing the peered VPC to access everything else in the same subnet?

I have an RDS database in VPC A, that I'd like to share with an EC2 instance in VPC B.
How do I do so by giving access specifically ONLY to the database (especially given that RDS doesn't expose a static IP and rather a DNS endpoint)?
Assuming your VPCs are peered using VPC peering or transit gateway, you can whitelist ec2's security group in the security group that is attached to your rds instance.
So, you can add an inbound rule to rds's security group which will allow access on port 3306 (mysql) or 5432 (postgres) from security group id attached to ec2 instance.

How to verify EBS and RDS in VPC?

I believe that I've successfully setup an EBS instance and RDS instance in a VPC! It is structured like this:
Elastic load balancer: public available to the internet
Elastic instance: in private subnets
RDS instance: in private subnets
What can I do, both in AWS and outside through testing, to verify that my elastic instance is protected in the VPC and my RDS instance is as well?
Thank you so much!
You can verify the following:
ensure that SGs of the instances allow only incoming traffic from the SG of ALB.
ensure that the EB instances are in private subnet, i.e. they don't have public IP.
ensure that RDS has no public IP option enabled and also it is in private subnets.
also ensure that the SG of the RDS allows only incoming connections from the EB instances.
Adding on to #Marcin's comments, I would also do the below to ensure you are following best practises:
Enabling access logs on the ELB to have some sort of logs on who is accessing the ELB. Would definitely help in troubleshooting.
Have your ec2 in ASG.
Create a certificate and terminate the HTTPS connections on ELB or you can pass the HTTPS through and terminate the SSL on the ec2.
Redirect all requests from the http port to https using the redirect feature in ELB.
Now, to answer your question on how to test the security:
try to ssh into your ec2 directly. It should not work as you are only allowing traffic from ELB and your ec2 is in private subnets.
try accessing your RDS. It should not work as it will only allow traffic from EC2 security group
Build a bastion server (a blank ec2) on AWS and try to access the EC2. It should not work as the ec2 should only allow traffic from the ELB security group
using the bastion, try accessing RDS. Same as above, it should not work as it should only allow traffic from ec2 security group.

Connect to RDS in a different region from EC2 instance

So I have a primary RDS in us-east-1 & a replica in us-west-1. Both are inside VPCs in their respective regions. I want to have one of my EC2 instances in us-east-1 connect to the replica instance.
A simple solution is to enable public access for the RDS replica and add the IP of the EC2 to its security group and it works.
But instead of allowing a static IP, I would like to allow access to the entire CIDR range of my us-east-1 VPC and also I don't want my instances to be public accessible.
To do this, I've setup a VPC peering connection between the two regions and I have added entries in the routing tables of both the VPCs to forward traffic to each other's CIDR ranges to the peering connections.
The CIRD range of the EC2 instance is 172.31.0.0/16 and I have added this to the security group of the RDS replica in the us-west-1 region. But for some reason the RDS is not reachable from my EC2.
Have I missed anything else? Thanks!
To summarize my setup:
US EAST:
VPC CIDR: 172.31.0.0/16
Route Table entry: Destination 10.0.0.0/16 routes to the peering connection of us-west-1 VPC.
EC2 IP: 172.31.5.234
US WEST:
VPC CIDR: 10.0.0.0/16
Route Table entry: Destination 172.31.0.0/16 routes to the peering connection of us-east-1 VPC.
RDS:
Public Accessible: Yes
Security Group: Allow connections from 172.31.0.0/16
To reproduce your situation, I did the following:
In us-east-1:
Created a VPC in us-east-1 with a CIDR of 172.31.0.0/16 using the "VPC with Public and Private Subnets" VPC Wizard
Launched an Amazon EC2 Linux instance in the public subnet
In us-west-1:
Created a VPC in us-west-1 with a CIDR of 10.0.0.0/16 using the "VPC with Public and Private Subnets" VPC Wizard
Added an additional private subnet to allow creation of an Amazon RDS Subnet Group that uses multiple AZs
Created an RDS Subnet Group across the two private subnets
Launched an Amazon RDS MySQL database in the private subnet with Publicly accessible = No
Setup peering:
In us-east-1, created a Peering Connection Request to the VPC in us-west-1
In us-west-1, accepted the Peering Request
Configure routing:
In us-east-1, configured the Public Route Table (used by the EC2 instance) to route 10.0.0.0/16 traffic to the peered VPC
In us-west-1, configured the Private Route Table (used by the RDS instance) to route 172.31.0.0/16 traffic to the peered VPC
Security Groups:
In us-east-1, created a security group (App-SG) that allows inbound port 22 connections from 0.0.0.0/0. Associated it to the EC2 instance.
In us-west-1, created a security group (RDS-SG) that allows inbound port 3306 connections from 10.0.0.0/16 (which is the other side of the peering connection). Associated it to the RDS instance.
Test:
Used ssh to connect to the EC2 instance in us-east-1
Installed mysql client (sudo yum install mysql)
Connected to mysql with:
mysql -u master -p -h xxx.yyy.us-west-1.rds.amazonaws.com
This successfully connected to the RDS database across the peering connection.
FYI, the DNS name of the database resolved to 10.0.2.40 (which is in the CIDR range of the us-west-1 VPC). This DNS resolution worked from both VPCs.
In summary, the important bits are:
Establish a 2-way peering connection
Configure the security group on the RDS instance to permit inbound connections from the CIDR of the peered VPC
No need to make the database publicly accessible

What are the security group rules for Amazon RDS dbinstance and EC2 instance over the different VPCs with VPC Peering?

What security group rules should I set for my db instance and my EC2 instance for accessing DB instance from my EC2 instance?
Both are in different VPCs and I used VPC Peering between them.
I did following configuration:
I created two VPC's
One is with public subnet and another is with private subnet
Launch EC2 web instance with public VPC and MySQL db instance with private subnet
Set VPC peering between them and they both have different security groups
Created a NAT Gateway in public subnet
So, how should I set both security group rules for establishing connections between them?
You should configure:
A security group on the Amazon EC2 instance (App-SG) that permits access to the instance/application as desired
A security group on the Amazon RDS DB instance (DB-SG) that permits inbound access on port 3306 for App-SG
That is, DB-SG should specifically refer to App-SG in the inbound rules.
When connecting from the EC2 to the database, make sure you are using the DNS Name of the RDS database. This should resolve to a private IP address.
The NAT Gateway is not required for the above connection.

VPN appliance in AWS VPC

I have a VPC on AWS with a public and a private subnet. I've deployed an instance of OpenVPN appliance in the public subnet to access my EC2 nodes in the private subnet. As expected, with VPN I can access (for e.g. SSH into) any EC2 node that I manually create in the private subnet. But I can't access services (for example Elastic Search or RDS Postgres) that AWS creates in the same private subnet. (I did make sure all security groups are properly configured on the Postgres and RDS). What am I missing?
I use a similar setup when connecting to my private RDS instances via VPN. I apologize, I cannot comment since this account is new and I do not have the reputation, I will have to make assumptions.
Your security groups need to be VPC security groups, not ec2 security groups (if they are not already).
VPC SG 1 (ec2 Bridge): This group is assigned to your OpenVPN server and allows traffic on your Postgres port and private IP CIDR.
Here is an example of mine for MSSQL and MySQL (I have multiple tunnels):
VPC SG 2 (Dev RDS Bridge): This has to allow traffic from VPC SG 1
Here is an example group I made just made for Aurora MySQL:
Finally, assign VPC SG 2 to your RDS Instance:
Now you should be able to talk to your RDS over your VPN connection while the RDS remains closed to the public. The process is similar for other AWS private resources.
Let me know if I wrongly assumed anything or can help more.