How to Connect to Private EC2 Instance/Database via Bastion Host (2024)

How to Connect to Private EC2 Instance/Database via Bastion Host (3)

We don’t want some of our resources to be accessed from the internet, so we create them in a private subnet. This is an important part in terms of security, but we also need access to these resources over the internet.

For example, we created an Amazon Elastic Compute Cloud (Amazon EC2) instance or an Amazon RDS (Relational Database Service) Database (DB) instance in a private subnet. A private subnet is not attached to (Internet Gateway) IGW and is not reachable from the internet. These resources don’t have public IPs so we can’t connect them from local computers. What are the solutions for connecting private resources in Amazon Web Services (AWS)?

The best solution is to connect your private resources via Virtual Private Network (VPN) or AWS Direct Connect. But if you don’t have VPN or Direct Connect to AWS, you can use a Bastion Host (Jump Box) instead.

I will show how to connect to a private EC2 instance from a terminal, an RDS DB instance from a terminal, and from MySQL Workbench.

First of all, we need to create one of the common relational database MySQL RDS DB instance and an EC2 instance in a private subnet, and an EC2 instance in a public subnet. All these resources must be in the same VPC. If they are not in the same VPC you need to create a VPC Peering connection between VPCs.

A. Connecting to a private EC2 instance with a terminal via Bastion Host

  1. Creating an EC2 instance in a public subnet as a Bastion Host:
  • Select “Amazon Linux 2 AMI”,
  • Instance type “t2.micro”,
  • Select your custom VPC and public subnet,
  • Add tag “Name = Bastion_Host”
  • In the security group section, select My IP as the source for the SSH connection.
  • Select your key pair and launch your instance.

2. Creating an EC2 instance in a private subnet:

  • Select “Amazon Linux 2 AMI”,
  • Instance type “t2.micro”,
  • Select your custom VPC and private subnet,
  • Add tag “Name = Private_Instance”
  • In the security group section, select custom and paste the security group of the public instance (Bastion Host).
  • Select your key pair and launch your instance.

Edit your “config” file under ~/.ssh/ folder and paste the content below:

vi ~/.ssh/configHost bastion-host
HostName <Public IP address of Bastion Host>
User ec2-user
Port 22
IdentityFile ~/.ssh/<key pair>
IdentitiesOnly yes
Host private-ec2
HostName <Private IP address of private EC2 instance>
User ec2-user
Port 22
IdentityFile ~/.ssh/<key pair>
IdentitiesOnly yes
ProxyJump bastion-host

We can connect to the private EC2 instance with the following command due to the ProxyJump in the config file:

ssh private-ec2

B. Connecting to a private RDS DB instance with the terminal from Bastion Host:

  1. Creating an EC2 instance in a public subnet as a Bastion Host:
  • Select “Amazon Linux 2 AMI”,
  • Instance type “t2.micro”,
  • Select your custom VPC and public subnet,
  • Add tag “Name=Public_Instance”
  • In the security group section, select My IP as the source for the SSH connection.
  • Select your key pair and launch your instance.

2. Creating a MySQL RDS DB instance in a private subnet:

  • Master username = “admin”
  • Master password = “12345678”
  • DB instance class “db.t2.micro”,
  • Select your custom VPC,
  • Public Access = No
  • Select default VPC security group

Select your RDS DB instance, click the “VPC security groups”, change the inbound rule’s source option to “Custom”, enter the private IP address of the Bastion Host and click “Save rules”.

Open your terminal and run the command below for SSH tunneling:

ssh -i “<key pair>” -N -L 3306:<DB endpoint>:3306 -p 22 ec2-user@<Public IP address or DNS of Bastion Host>

ssh -i "adesso.cer" -N -L 3306:database-1.ccswxi20cprx.us-east-1.RDS.amazonaws.com:3306 -p 22 ec2-user@44.201.66.76

After running this command, open a new terminal and try to connect to the MySQL RDS DB instance with the below command:

mysql -u admin -h 127.0.0.1 -p

Enter the password of the MySQL RDS DB instance and connect to the database.

Another option to connect the MySQL RDS DB instance from a terminal is using the config file. Open ~/.ssh/config file and paste the content below:

vi ~/.ssh/configHost tunnel-to-RDS
User ec2-user
Port 22
Hostname <Public IP address of Bastion Host>
LocalForward 3306 <DB endpoint>:3306
IdentityFile ~/.ssh/<key pair>
Host tunnel-to-RDS
User ec2-user
Port 22
Hostname 44.201.66.76
LocalForward 3306 database-1.ccswxi20cprx.us-east-1.RDS.amazonaws.com:3306
IdentityFile ~/.ssh/adesso.cer

Open your terminal and run the command below for ssh tunneling:

ssh tunnel-to-RDS

This command will open an SSH tunnel and you can connect the database with the below command:

mysql -u admin -h 127.0.0.1 -p

Enter the password of the MySQL RDS DB instance and connect the database.

C. Connecting to a private RDS DB instance with MySQL Workbench from Bastion Host:

Open your MySQL Workbench and click MySQL New Connection “+” icon.

How to Connect to Private EC2 Instance/Database via Bastion Host (4)

Enter a name for your connection and select “Standard TCP/IP over SSH” as the Connection Method. Then fill in the fields according to the information below:

  • SSH Hostname = <Public IP address of Bastion Host>,
  • SSH Username = ec2-user,
  • SSH Key File = Select your key file from your local computer,
  • MySQL Hostname = <DB Endpoint>,
  • MySQL Server Port = 3306,
  • Username = admin,
  • Password = 12345678

Click the “Test Connection” button. You need to see “Successfully made the MySQL connection” on the pop-up window. Choose “OK” for saving connection. Then you can connect your database using an SSH tunnel.

How to Connect to Private EC2 Instance/Database via Bastion Host (5)

Congrats. You have access to your private resources in the AWS account from your local computer.

Some resources must have limited access to the Internet, especially in terms of security. Therefore, these resources are created in private subnets and do not have Public IPs. If there are no services such as VPN or Direct Connect that allow us to access resources over Private IP, we can generally access these resources through Bastion Hosts. In our article, we have shown several ways how we can access an EC2 instance and RDS created in a private subnet from our local computer through Bastion Host.

How to Connect to Private EC2 Instance/Database via Bastion Host (2024)

FAQs

How to Connect to Private EC2 Instance/Database via Bastion Host? ›

EC2 Linux instances use SSH key-pair files as the default authentication method. Key-pair files eliminate the need for SSH usernames and passwords. To maintain a secure environment, never store private keys on the bastion host. To connect using a bastion host, use ssh-agent forwarding on the client.

How to connect to private EC2 instance database via bastion host? ›

EC2 Linux instances use SSH key-pair files as the default authentication method. Key-pair files eliminate the need for SSH usernames and passwords. To maintain a secure environment, never store private keys on the bastion host. To connect using a bastion host, use ssh-agent forwarding on the client.

How do I connect to a private IP EC2 instance? ›

How It Works
  1. Create the Endpoint: First, you create an EC2 Connect Endpoint in your VPC. Think of it as your encrypted Bat-Signal. ...
  2. Private Tunnel Magic: The endpoint acts as a private tunnel. ...
  3. Subnet Love: Once you've set up the endpoint, it's like having a secret handshake with your subnets.
Feb 17, 2024

How to connect to bastion host? ›

In the Category menu, navigate to Connection > SSH > Auth. In the Private key file for authentication field, select the private SSH key file that corresponds to the public key you added to the VM. In the Authentication Parameters section, select Allow agent forwarding. Click Open to connect to the bastion host VM.

What is required to log in to an instance via a bastion host? ›

In order to access an instance, you need:
  • Access granted to the stack. ...
  • The stack ID that you want to access so you can be granted access to the instance. ...
  • The instance IP that you want to access. ...
  • The DNS friendly bastion name or the bastion IP.

What is bastion host ec2? ›

A bastion host is a server whose purpose is to provide access to a private network from an external network, such as the Internet. Because of its exposure to potential attack, a bastion host must minimize the chances of penetration.

How do I connect to a private EC2 instance SSM? ›

Ensure SSM Agent has been installed in destination instances. To enable the console access to connect the session manager a new user should be created in IAM. Create a IAM Role and attach the AmazonEC2RoleforSSM policy for the role. Finally attach the created IAM Role to the destination instance.

How to connect to EC2 without public IP? ›

Remotely Connect to Your Instances without a Public IP Address
  1. Step 1: IAM Permissions to use EC2 Instance Connect Endpoint. ...
  2. Step 2: Security Groups Configurations. ...
  3. Step 3: Create EC2 Instance Connect Endpoint. ...
  4. Step 4: Connect your EC2 Instance.

How do I connect to an EC2 instance with private IP using PuTTY? ›

Connect to the EC2 instance

Click on the desktop icon or the putty.exe file in the PuTTY folder to open PuTTY. 2. Type ubuntu@your_public_DNS in the Host Name (or IP address) box (Figure 4.6).

How does bastion host work? ›

A bastion host forms a bridge between your device and the network you want to connect to. Only authorized users can access the other computers on this private network using this bridge. This prevents unauthorized access to your business network, blocking hackers from accessing your resources and sensitive data.

How do I connect to bastion host using putty? ›

Create Putty Connection #1 from the Host to the Bastion
  1. Open Putty, under Host Name, put the public IP address of your Bastion host, and specify Port 22.
  2. Under SSH->Auth: ...
  3. Under SSH->Tunnels. ...
  4. Save the connection so you can use it for future use, name it “Bastion-EC2”
Jan 19, 2021

How many users can connect to Bastion? ›

SharedStandard = A user receives one bastion to connect to and two users can connect to the same bastion at once.

How to connect to a private EC2 instance through a bastion host? ›

Connecting to a private EC2 instance with a terminal via Bastion Host
  1. Select “Amazon Linux 2 AMI”,
  2. Instance type “t2. ...
  3. Select your custom VPC and public subnet,
  4. Add tag “Name = Bastion_Host”
  5. In the security group section, select My IP as the source for the SSH connection.
  6. Select your key pair and launch your instance.
Feb 18, 2022

How to connect to a private instance? ›

You can SSH into EC2 instances in a private subnet using SSH agent forwarding. This method allows you to securely connect to Linux instances in private Amazon VPC subnets via a bastion host (aka jump host) that is located in a public subnet.

What permissions are needed for bastion? ›

In order to make a connection, the following roles are required:
  • Reader role on the virtual machine.
  • Reader role on the NIC with private IP of the virtual machine.
  • Reader role on the Azure Bastion resource.
Apr 1, 2024

How to connect to a private RDS instance? ›

Connect to the RDS DB instance from your local machine
  1. Start a new connection, and then select Standard TCP/IP over SSH for the Connection Method.
  2. For SSH settings, enter the following details about the EC2 instance: Auto-assign Public IP: Make sure that Enable is selected for the DNS Hostnames option.

How do I Connect to a private EC2 instance SSM? ›

Ensure SSM Agent has been installed in destination instances. To enable the console access to connect the session manager a new user should be created in IAM. Create a IAM Role and attach the AmazonEC2RoleforSSM policy for the role. Finally attach the created IAM Role to the destination instance.

Top Articles
What are stock options & how do they work?
What are Realtime APIs?
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5893

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.