How EC2 allow SSH tunelling to access RDS in private subnet? - amazon-ec2

To access RDS in private subnet, in the below architecture,
Giving public IP of EC2, ssh private key location of EC2 in public subnet, DB credentials of RDS in private subnet, as shown below,
How EC2(in public subnet) allow ssh tunneling to RDS in private subnet? Is it something to do with /etc/ssh/sshd_config in EC2?

From the picture, the EC2 instances share the same private network (class B) 172.16.X.X with the RDS therefore by having access to any instance in the public segment and depending on the security groups defined on the RDS it may be possible to reach the database by doing something like:
ssh -L 3307:<db>.rds.amazonaws.com:3306 user#your.ec2
The option -L will do a local port forwarding from port 3307 (your computer) to port 3306 in <db>.rds.amazonaws.com going through your.ec2.instance.
You could use a bastion host to do this, you could read more about it here: https://docs.aws.amazon.com/quickstart/latest/linux-bastion/architecture.html
Besides defining how and what users to allow using ssh you will not have to deal with sshd_config most of the work will be either in AWS security groups or how you defined your VPC or network ACL's.

Related

Unable to telnet to a VPC instance - AWS

I am trying to connect to a particular port from another AWS instance. Getting connection refused.
In the AWS instance, I have added port 11626 in inboud rules under security groups. Also, I can check port 11626 is listening and I can do self telnet using localhost.
However when I do a self telnet using public/private IP, it is saying connection refused. Is the natting between Public and private IP missing? If yes How do i map these IP?
Are you connecting the AWS instance over the public Internet? Also, how are you connecting to the instance?
Basically, to be able to connect to AWS instance, you need to have the following:
A Public IP assigned to the AWS EC2 instance. You can't connect to an instance using Private IP unless you have VPN which is generally set up for an organisation.
Open the desired port (in your case port 11626). You can open this desired port for the entire public (0.0.0.0/0) or you can specify public IP address in the security group.
If you already created your EC2 instance without getting a public IP address assigned, you can request for allocation of a new Elastic IP address and assign it to your EC2 instance and connect using the Elastic IP address.

Can't connect to RDS from EC2 using terraform created infrastructure

I created the infrastructure manually following the steps provided in the link: connecting-to-a-database-within-an-amazon-vpc.
I could connect to RDS in private subnet from ec2 in public subnet perfectly. That is exactly what I wanted.
But when I tried to create the same exact replica using terraform, it just doesn't connect.
The command:
nslookup <my-hostname>
gives me the Private IP.
The command:
telnet x.x.x.x 5432
results in "telnet: Unable to connect to remote host: Connection timed out"
The command:
netstat -an | grep x.x.x.x
shows "SYN_SENT".
I tried allowing all the IP's in RDS security group by adding 0.0.0.0/0. That didn't work.
I tried to create RDS instance manually, but added it to terraform created VPC and tried to connect using terraform created ec2 instance. That didn't work.
Am i missing something ? Any help is much appreciated.
troubleshooting steps:
Check security group's egress (outbound rules) on your public ec2 instance,
Make sure it allows outbound rules with port 5432 to access rds (which is in private subnets)
Check security group's ingress (inbound rules) on your rds instance. Maybe sure it allows port 5432 from public ec2 instances CIDR ips or security groups.
If not, please update your terraform codes according.
The ec2 instance is in public subnet. That would mean ideally traffic would go from NAT Gateways. So I would suggest to edit the security group rule of RDS add a rule to allow public IPs of NAT Gateways to be accessible on the port 5432.

Amazon Web Services EC2 to RDS Connectivity with VPC

I have been trying to set up an AWS Free Tier account using an EC2 instance and an RDS database running MySQL. Unfortunately, I cannot figure out how to grant access to the database from the EC2 instance. I have read all of the AWS documentation, but it is unfortunately out of date as are all the questions posted on StackOverflow. All of the documentation states that I should go to the Security Groups section of the RDS Dashboard. However, when I do so, this is what I'm confronted with.
** I would have included the image but I don't have the reputation for it.
Okay, I understand that I am not using the EC2-Classic platform and that I must make these changes to the Security Group in the EC2 Dashboard, but how?! I do not want public access to port 3306, I only want the EC2 instance to be able to communicate with the RDS database on a private subnet. Any help would be greatly appreciated.
The links to "AWS Documentation on Supported Platforms" and "Using RDS in VPC" are not helpful. They are outdated and also keep referring me back to Security Groups under the RDS Dashboard, which then only shows me this message.
A rule of thumb: When you are setting up resources in VPC, use ONLY VPC Security Groups. The individual RDS, Redshift...etc. security groups work only in case of ec2-classic. Meaning, when you are not setting up things in VPC.
Go to the VPC console and then on the left hand side menu, you will find security groups. These are the security groups which should control access to your AWS resources deployed inside a VPC.
I can't elaborate much as I am unaware of your VPC configuration and which subnet (public/private) you are setting these up.
Example
Here is the hypothetical scenario...
VPC: 10.0.0.0/16
1 public subnet: 10.0.0.0/24
1 Private Subnet: 10.0.1.0/24
Assume you put your EC2 instance in Public Subnet
Assume you put your RDS instance in Private Subnet
And you want EC2 instance be accessible on 80,443 from the world and RDS instance should be accessible only via EC2 instance.
So, these are the security groups settings:
for EC2 instance Security group:
Inbound: port 80, 443 : from 0.0.0.0/0
Outbound: port 3306 : to 10.0.1.0/24
For RDS security group:
Inbound: port 3306: from 10.0.0.0/24
Explanation
Inbound: port 80, 443 : from 0.0.0.0/0
This will allow EC2 instance be accessible on port 80 and 443 from the Internet.
Outbound: port 3306 : to 10.0.1.0/24
This allows EC2 instance to send the traffic on port 3306 only to the private subnet which is 10.0.1.0/24
Inbound: port 3306: from 10.0.0.0/24
This allows the RDS instance to accept traffic on port 3306 from the public subnet which is 10.0.0.0/24. Your EC2 instance resides in Public subnet so inherently RDS will accept traffic from Ec2 instance on port 3306
NOTE: Above setup presumes that you have set your Routing tables for the public/private subnets accordingly.
Hope this helps.

Connection to amazon-ec2 VPC instance fails

I have the following setup:
- a VPC, with several subnets, and an access gateway and a NAT instance having public addresses that I can connect to,
- I create a Linux instance in a subnet of the VPC, that has NO public IP address or DNS name (I want that only the Load Balancer be known on the internet).
I want to connect to my Linux instance to install and configure software.
How do you connect to that instance? All the documentation I have seen mentions that you connect using "ec2-user#".
Since I have no public DNS, i have tried to connect from the access gateway via putty with the private DNS of my linux instance but it fails ("host does not exist").
I am obviously missing something ... in the NAT?
Thanks, Laurent
You need to have a hosts in the public subnet which you can access. Once you access this host, then you can connect to your other hosts in VPC using their private IP address.
Your instance in question has only private IP address so connecting it from your workstation is not going to work.
The host I am referring to is usually called Bastion Host. read the Tip in Scenario 2: VPC with Public and Private Subnets documentation.
Also, read first few results of this Google Search to gain overall understanding on use-cases for Bastion hosts.

Are EIP's required for internet traffic?

Sorry if this is a dumb question but I can't find any definitive answers. I setup a VPC with a private subnet and a public subnet. THe private subnet has a NAT'd instance to route for internet traffic. The public subnet is configured to go out of the IGW.
-I created a Bastion host to login into SSH
-I created Test instance on private subnet and connect from Bastion Host. Because of the route to the NAT instance and port 80/443 is allowed and ICMP, i can ping and access files on the internet.
-I create instance on public subnet without EIP. Since it has a route to the IGW, shouldn't I have internet access? I created a test security group to allow all traffic from all sources and i still can't ping or get http links.
A public subnet instance with a public DNS name should be accessible over the internet provided you have right VPC security group configured. You do not need an EIP.
Public DNS name of EC2 instance changes when you stop and start the instance. to avoid this, you can assign an EIP to the instance so that the IP address remains same across the instance stop/start cycles.
You need to answer these questions:
Does your public subnet instance has a public DNS hostname? Run curl -s http://169.254.169.254/latest/meta-data/public-hostname on your instance to see the public DNS hostname.
Do you have VPC security group configured properly to allow incoming internet access ?

Resources