Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 70 ยท Networking with VPC

Bastion Hosts and Private Subnet Access

Learn the bastion host pattern on AWS: public and private subnets, security group rules, SSH ProxyJump and agent forwarding, and why Session Manager is often better.

A well-designed VPC keeps servers and databases in private subnets, with no public IP address and no route from the internet. That is good for security, and it creates a practical problem: how does an engineer log in to one of those servers to debug something?

The classic answer is a bastion host, also called a jump box or jump server. It is one small, hardened instance in a public subnet that you can reach from the internet. You connect to it first, and from there to the private instances. All administration goes through one well-guarded door. This lesson builds the pattern, shows the SSH commands, and then compares it with the modern alternatives, which are often better.

Instances and networks cannot be created on this site, so the CLI and SSH commands are read-only real examples. The Python programs are stand-ins that evaluate security-group chains and compare access options. They are simplified, not a real network simulation.

The architecture

flowchart LR
    ME["You<br/>203.0.113.7"] -->|"SSH 22<br/>from your IP only"| B
    subgraph VPC["VPC 10.0.0.0/16"]
        subgraph PUB["Public subnet"]
            B["Bastion host<br/>public IP"]
        end
        subgraph PRIV["Private subnet"]
            APP["App server<br/>no public IP"]
            DB[("Database")]
        end
        B -->|"SSH 22<br/>from bastion SG only"| APP
        APP --> DB
    end

The only way in from the internet is the bastion. Private instances accept SSH from the bastion's security group and nothing else.

The pieces:

  1. A public subnet has a route to an internet gateway. The bastion gets a public IP (an Elastic IP is convenient so that its address does not change).
  2. A bastion security group allows inbound TCP 22 only from your office or home IP range, not from 0.0.0.0/0.
  3. A private-instance security group allows inbound TCP 22 with the bastion's security group as the source, instead of an IP range. This is a security group reference: any instance in the bastion's group may connect, whatever its address.
  4. A key pair for each hop, or the same key used through forwarding or a jump option.

Creating the two rules with the CLI (read-only):

Terminal
# Bastion: SSH from my IP only
aws ec2 authorize-security-group-ingress \
  --group-id sg-0bastion1 --protocol tcp --port 22 --cidr 203.0.113.7/32

# Private app servers: SSH only from the bastion's security group
aws ec2 authorize-security-group-ingress \
  --group-id sg-0app0001 --protocol tcp --port 22 --source-group sg-0bastion1

The first stand-in checks whether a connection is allowed, following those rules.

Python Is this SSH path allowed?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the laptop cannot reach the app server directly, and the bastion cannot reach the database. Each hop is deliberate.

Connecting through the bastion

There are three ways to chain an SSH connection, and the difference matters for security.

1. ProxyJump (recommended). The -J flag tells SSH to open the second connection through the bastion. Your laptop does the encryption end to end, and your private key never lands on the bastion:

Terminal
ssh -J [email protected] ec2-user@10.0.2.15

The same in ~/.ssh/config, so that plain ssh app works:

Text
Host bastion
    HostName bastion.example.com
    User ec2-user
    IdentityFile ~/.ssh/my-key.pem

Host app
    HostName 10.0.2.15
    User ec2-user
    ProxyJump bastion

2. Agent forwarding (ssh -A). SSH makes your local key agent available on the bastion, so you can run ssh 10.0.2.15 there. It is convenient, but anyone with root on the bastion can use your forwarded agent while you are connected. If you must use it, forward only to hosts you trust, and prefer ProxyJump.

3. Copying the key to the bastion. Never do this. A private key stored on an internet-facing server is a gift to attackers.

ProxyJump also carries port forwarding, which is how you reach a private database from a desktop tool:

Terminal
ssh -N -L 5433:db.internal.example.com:5432 -J [email protected] ec2-user@10.0.2.15
# then connect your database client to localhost:5433

Hardening the bastion

A bastion is a target, so make it boring and small:

  • Allow inbound SSH from known IP ranges only, never the whole internet.
  • Run nothing else on it. No web server, no database, no shared users.
  • Keep it patched (or rebuild it regularly from an image), and enable automatic security updates.
  • Use individual accounts and keys, and log connections: send /var/log/secure or auth.log to CloudWatch Logs.
  • Start it only when needed, or put it in an Auto Scaling group of size 1 so that a failed bastion is replaced, as covered in Auto Scaling.
  • Use IMDSv2 and give the instance a role with no permissions it does not need.

Alternatives to a bastion

You can often remove the bastion entirely.

AWS Systems Manager Session Manager gives you a shell on an instance from the console or CLI with no inbound port, no public IP and no SSH key. Access is controlled by IAM, and every session can be logged to CloudTrail, S3 or CloudWatch. The instance needs the SSM agent, an instance profile with the right permissions, and a path to the Systems Manager endpoints (a NAT gateway or VPC interface endpoints). See Systems Manager.

Terminal
aws ssm start-session --target i-0123456789abcdef0

Session Manager can also forward ports (AWS-StartPortForwardingSessionToRemoteHost), which replaces the SSH tunnel to a database.

EC2 Instance Connect Endpoint lets you connect to instances in private subnets over SSH or RDP through an endpoint you create in the VPC, again without a public IP or a bastion. IAM decides who may use it.

A VPN or Direct Connect gives your whole office private access to the VPC, so administrators connect to private addresses directly. See VPC connectivity.

This stand-in scores the options against requirements, in the way you might discuss a design review.

Python Bastion host or something else?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scoring is made up, but the conclusion is sound: for a new design, start with Session Manager or an Instance Connect Endpoint, and keep a bastion only when you need classic SSH tunnels, Windows tools or a setup that your team already runs.

Common mistakes

  • Opening port 22 on the bastion to 0.0.0.0/0. Bots will find it within minutes.
  • Putting the bastion in a private subnet and wondering why it cannot be reached.
  • Forgetting that the private subnet's route table has no route to the internet gateway. That is correct. Package updates need a NAT gateway, not the bastion.
  • Using one shared key for everyone, so nobody can tell who did what.
  • Leaving the bastion running with outdated software.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does a bastion host live?

    It must be reachable from the internet, so it sits in a public subnet. It then reaches the private instances over the VPC network.
  2. 2. What should the private instances' security group use as the source of the SSH rule?

    Referencing the bastion's security group allows only members of that group to connect, whatever their IP addresses.
  3. 3. Why is SSH ProxyJump safer than agent forwarding?

    With ProxyJump the bastion only relays encrypted traffic. Agent forwarding exposes your agent socket to anyone with root on the bastion.
  4. 4. Which statement about Session Manager is correct?

    Session Manager connects through the SSM agent over outbound connections, so no inbound ports are required, and access is governed by IAM.
  5. 5. What is the worst place to keep your private SSH key?

    If the bastion is compromised, an attacker gets the key to everything behind it.

Summary

  • A bastion host is a small, hardened instance in a public subnet that is the single entry to private instances.
  • Allow SSH to the bastion from known IP ranges only, and to private instances from the bastion's security group.
  • Use ssh -J (ProxyJump), not copied keys or casual agent forwarding.
  • Harden, patch and log the bastion, and keep nothing else on it.
  • Session Manager, EC2 Instance Connect Endpoint and a VPN often remove the need for a bastion, with no open inbound ports.

Next: Route 53 and DNS