How to access AWS Lightsail MySQL database from VPC (EC2 instance)? - amazon-web-services

I have an Amazon Lightsail MySQL database.
I have enabled "VPC Peering" in Amazon Lightsail account.
I have an EC2 server in the VPC account that is "peered" with Lightsail.
Now I want to access the Lightsail database from the EC2 instance. I know the "easy way" is to enable the "Public mode" in the Lightsail database. In this way, the endpoint is publicly available on Internet, so you can connect from anywhere.
I want to avoid this, so here is the question: Is it possible to access the Lightsail database from the EC2 instance, using the internal VPC communication? If not, is there any other way to make this connection more secure? Maybe a way to whitelist IPs in Lightsail databases?
Thanks!

To reproduce your situation, I did the following:
Launched an Amazon Lightsail Database (I chose PostgreSQL)
Enabled VPC Peering in the Lightsail console (Account / Advanced)
Launched an Amazon EC2 instance in the Default VPC (which is the only one that Lightsail connects to)
Tried connecting from the EC2 instance to the Lightsail database
It did not work.
The database DNS Name successfully resolved to an IP address that was in the correct range for VPC Peering. However, when attempting to connect to the database, psql hung for a long time before failing. This is an indication of no network connection between the EC2 instance and the Lightsail database.
Normally, the way to fix it would be to check the Security Groups, but Lightsail does not support security groups. It would appear that access to the Lightsail database is locked-down to the Lightsail network and it is not accessible via VPC Peering.

Related

Connect a Database with an Amazon VPC from local

Just created an VPC for EKS Cluster and started RDS PostgreSQL instance with custom VPC.
Custom VPC has Subnets.
My Custom VPC has Internet Gateway attached.
EKS and RDS is in same VPC so they have internal communication.
My problem is that i want to connect to RDS from my local machine and i am unable. Regarding the problem i have created a new Security Group with Inbound Rules for PostgreSQL.
PostgreSQL TCP 5432 0.0.0.0/0 –
Im still unable to connect
UPDATE
RDS is Publicly accessible
Security group allows access to RDS
In order to connect to RDS instances from the internet you need to do these 3 things
Deploy your RDS instance in a "public" subnet. This means the subnet must have an Internet Gateway attached to it so it can respond properly to outbound requests
In your RDS instance under Connectivity, extend the Additional configuration section, and then choose Publicly accessible.
Make sure the security group allows access to your RDS instance.
Note: exposing a database to public access is not secure. What I recommend you to do is create a proxy with haproxy or a VPN.
To be able to connect to the RDS database remotely you need to select "yes" option for the "Public Accessibility" setting for you database. Here are some additional configurations that need to be taken into account (form AWS docs):
If you want your DB instance in the VPC to be publicly accessible, you
must enable the VPC attributes DNS hostnames and DNS resolution.
Your VPC must have a VPC security group that allows access to the DB
instance.
The CIDR blocks in each of your subnets must be large enough to
accommodate spare IP addresses for Amazon RDS to use during
maintenance activities, including failover and compute scaling.
Best,
Stefan

Amazon RDS and VPC Endpoints Connectivity

I am having an Amazon RDS Postgres instance which resides in the default VPC.
To connect to it, i am using different EC2 instances (Java Spring Boot and NodeJs) running in ElasticBeanstalk. These instances also reside in the default VPC.
Do these EC2 instances connect to/query the RDS instance through the internet or the calls do not leave the AWS Network?
If they leave the AWS network and the calls go through the internet, is creating a VPC endpoint the right solution? Or my whole understanding is incorrect.
Thanks a lot for your help.
Do these EC2 instances connect to/query the RDS instance through the internet or the calls do not leave the AWS Network?
The DNS of the RDS endpoint will resolve to private IP address when used from within VPC. So communication is private, even if you use public subnets or set your RDS instance as publicly available. However, for connection from outside of AWS, the RDS endpoint will resolve to public IP address if the db instance is publicly available.
If they leave the AWS network and the calls go through the internet, is creating a VPC endpoint the right solution?
There is no VPC endpoint for RDS client connections, only for management actions (creating db-instance, termination, etc). In contrast, Aurora Serverless has Data API with corresponding VPC endpoint.
To secure your DB-Instances communications you need to be sure at least about the following:
locate your RD in private subnet (route table does not contain default outbound route to internet gateway).
RDS security group just accept traffic inbound only from instances security group/groups on TCP port for PostgreSQL which is usually 5432.
In this case Traffice to RDS will go localy in your vpc, for vpc endpoints it can be used to access RDS API operations privatly which is not your case (you just need to connect your app to DB using connection string)

connecting to private aws rds

I'm new and trying to explore AWS and creating a test app running on IIS (EC2 set in Public) and MSSQL RDS(set in Private). However my IIS is not able to connect on the RDS. Looking on my IIS EC2 it has a defined private IP too, same subnet used also from RDS, security group of RDS allows the subnet of the EC2, IAM of EC2 also has RDS Full access.
But still my ec2 wont connect on the RDS. May I ask on what I'm missing on my settings?
It appears that your configuration is:
One VPC
A Public Subnet containing an Amazon EC2 instance
A Private Subnet containing an Amazon RDS for SQL Server instance
You are attempting to connect from the EC2 instance to the RDS db instance
For this to work, you should configure:
A Security Group (let's call it EC2-SG) that is associated with the EC2 instance, permitting connections such that you can login to the EC2 instance
A Security Group (RDS-SG) associated with the RDS db instance, with an incoming connection configured for SQL Server (port 1433) with source set to EC2-SG
That is, the database security group should ALLOW an incoming connection with a source of the security group that is associated to the EC2 instance. The VPC will automatically figure out the IP addresses — you do not need to specify them.
Then, connect from the EC2 instance to the RDS db instance via the DNS Name of the RDS instance that is given in the RDS console.

AWS: How to connect VPN clients to RDS (VPN server EC2 and RDS are in the same VPC)

I have an EC2 that run as a VPN server. In the same VPC I have a RDS instance and another EC2 instance in a private subnet.
I have devices that connects to the VPN server and I have configured that they can communicate with each-other and with the private EC2 too. But I can't make them to communicate with the RDS instance.
I have configured the Security Group of the RDS to allow all inbound traffic from the SG of both EC2, tried to allow even All Traffic from 0.0.0.0/0 a still VPN clients can't communicate with the RDS. I see that RDS can communicate inside the VPC but not outside it. Once upon a time a remember and I'm sure that I was connected from my local MySQL Workbench to the RDS(3 years ago)
Is there anyway to make this work?
Your answer may be in this OpenVPN Support thread. I'm running into the same issue. From what I gather, when you're connected over vpn, public IPs and DNS names won't resolve. You can connect to other EC2 instances easily using private IPs. But the RDS instance's IP is not static, so it must be resolved using it's host name. The solution apparently is to make your OpenVPN server use the Amazon DNS server, so that it can resolve the RDS instance by its host name.

Connecting to RDS from EC2

This is a bit of a basic question, I don't have much experience of AWS (Azure is my usual bag!).
When connecting to an RDS instance from and EC2 instance using connection string such as : xxx.xxxxxx.eu-west-1.rds.amazonaws.com:1433 and both the RDS and EC2 are in the same VPC will there be bandwidth charges for that connection? The EC2 can only access the RDS when the RDS IP address is added to the security group.
Also, is the traffic routed over the internet or is there some way to configure traffic over some sort of virtual LAN? If so could someone point me in the direction of how that works?
Does this configuration sound correct?
Thanks for your help.
Carl
When connecting to an RDS instance from and EC2 instance using
connection string such as :
xxx.xxxxxx.eu-west-1.rds.amazonaws.com:1433 and both the RDS and EC2
are in the same VPC will there be bandwidth charges for that
connection?
The bandwidth charges are the same as data transfer between two EC2 instances in the same VPC. That is, there will be no charge for data transfer within the same availability zone, and will be charged $0.01 per GB for data transferred between availability zones.
The EC2 can only access the RDS when the RDS IP address is
added to the security group.
You should enable inbound access from the EC2 security group associated with your EC2 instance, instead of the EC2 IP address.
Also, is the traffic routed over the internet or is there some way to
configure traffic over some sort of virtual LAN? If so could someone
point me in the direction of how that works?
The VPC (Virtual Private Cloud) is the virtual LAN you are looking for. All traffic between instances and services within your VPC stays within your VPC. Communication within your VPC does not go out to the internet.
Since you mentioned you are coming from an Azure background, you should try to map your knowledge of Azure services to the corresponding AWS services. I believe an "Azure Virtual Network" is basically the same thing as an AWS VPC.
You can give permission to your ec2 instance ip address in your RDS security group.
Which database you used?
If you are using mysql then ping from server terminal:
mysql -h<<hostname>> -u<<username>> -p<<password>>