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 [email protected]

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
How to Set Up Your Digital Wallets?
Magnetic Canadian Coins | Classroom Essentials Scholastic Canada
Pollen Count Los Altos
Amateur Lesbian Spanking
Craigslistdaytona
Shemal Cartoon
Burn Ban Map Oklahoma
Craigslist Farm And Garden Tallahassee Florida
Kvta Ventura News
Bnsf.com/Workforce Hub
Webcentral Cuny
Sni 35 Wiring Diagram
Puss In Boots: The Last Wish Showtimes Near Cinépolis Vista
Touchless Car Wash Schaumburg
Anonib Oviedo
Renfield Showtimes Near Paragon Theaters - Coral Square
Harrison County Wv Arrests This Week
Craigslist Rentals Coquille Oregon
Divide Fusion Stretch Hoodie Daunenjacke für Herren | oliv
Skidware Project Mugetsu
John Philip Sousa Foundation
Mississippi Craigslist
How rich were the McCallisters in 'Home Alone'? Family's income unveiled
Possum Exam Fallout 76
Maths Open Ref
Isablove
5 Star Rated Nail Salons Near Me
Frequently Asked Questions - Hy-Vee PERKS
35 Boba Tea & Rolled Ice Cream Of Wesley Chapel
Vanessa West Tripod Jeffrey Dahmer
Daily Jail Count - Harrison County Sheriff's Office - Mississippi
Merge Dragons Totem Grid
Nearest Ups Office To Me
Is The Nun Based On a True Story?
Telugu Moviez Wap Org
Leena Snoubar Net Worth
Aurora Il Back Pages
St Anthony Hospital Crown Point Visiting Hours
Barstool Sports Gif
Luvsquad-Links
Arigreyfr
Florida Lottery Claim Appointment
Executive Lounge - Alle Informationen zu der Lounge | reisetopia Basics
Lucyave Boutique Reviews
Wilson Tire And Auto Service Gambrills Photos
Quaally.shop
Sapphire Pine Grove
De boeken van Val McDermid op volgorde
San Diego Padres Box Scores
Arnold Swansinger Family
Costco Gas Price Fort Lauderdale
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.