What is a SSH Tunnel — AWS EC2

February 21, 2019

This article is part of a series 😃. With the end goal of connecting to a private database on AWS from our local computers. ✨

In this article we are going to talk about what a SSH Tunnel does and what it looks like using AWS EC2. By leveraging a SSH Tunnel we can connect to private databases on AWS from our local computers.

What is a SSH Tunnel?

“SSH tunneling is a method of transporting arbitrary networking data over an encrypted SSH connection. It can be used to add encryption to legacy applications. It can also be used to implement VPNs (Virtual Private Networks) and access intranet services across firewalls.” — Source: SSH

Why are we using a SSH Tunnel to connect to DocumentDB/RDS?

With a SSH Tunnel we can keep our database private and ensure that connections can only be made from inside the same VPC (Virtual Private Cloud).

SSH Tunnel Diagram — Ryan Jones

What is Port Forwarding?

“Port Forwarding allows remote computers (for example, computers on the Internet) to connect to a specific computer or service within a private local-area network (LAN).” — Source: Wiki

On AWS, this means that we are connecting to the AWS EC2 instance with one terminal command.

  
$~: ssh -i ~/.ssh/sshtunnel.pem -N -L 27017:private_db_url:27017 ec2-user@12.345.67.89
  

Then, once that connection to our EC2 is established in the first terminal tab. We will run a second terminal command to make the connection to our private database.

  
$~: mongo --host private_db_url:27017 --username user --password password123!
rs0:PRIMARY> show dbs;
mydb1
mydb2
  

The end result, we can now run database commands we are familiar with against our private database on AWS. 🎉

Let’s keep going

In the next article, we will jump into actually creating the AWS EC2 instance and setting up our .pem file for local connections.

Once we have our SSH Tunnel setup we will cover creating a DocumentDB instance and how to connect to DocumentDB locally. Extra benefit, this same process can be done to connect to RDS as well!

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones
Founder
Book a meeting
arrow
Founder
Eduardo Marcos
Chief Technology Officer
Chief Technology Officer
Book a meeting
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.