Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

EC2 Instance Connect from the browser will not open an SSH session

ErrorHost key validation failed for EC2 Instance Connect

Short answer

Browser-based EC2 Instance Connect needs five things at once: a fresh host key in the AWS database, a current agent package, port 22 open to the EC2 Instance Connect prefix list, a public address with a route (or an Instance Connect Endpoint), and a browser that can reach the proxy on port 443.

  • Applies to Amazon Linux 2023, Ubuntu 24.04, EC2 console, AWS CLI v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log in to the same instance with your normal key pair. If that works, the instance and its network are fine and the fault is in the Instance Connect pieces below. If it does not, fix plain SSH first.

    ssh -i my_ec2_private_key.pem ubuntu@ec2-a-b-c-d.us-west-2.compute.amazonaws.com
  2. 2Match what you see, then jump to its fix

    1. 1Host key validation failed for EC2 Instance ConnectThe instance host keys were rotated and AWS still holds the old onesFix
    2. 2Any connection error on Ubuntu with Instance Connect, and no host key messageThe ec2-instance-connect package on an Ubuntu instance is out of dateFix
    3. 3The console session hangs, then fails, while SSH from your own IP worksThe security group does not allow SSH from the EC2 Instance Connect serviceFix
    4. 4Connection fails for a private-subnet instance or one with no public IPv4 or IPv6 addressThe instance has no public address, or its subnet has no route to the internetFix
    5. 5The page cannot open the session even though the instance and security group look rightYour browser cannot reach the Instance Connect proxyFix

What does the error look like?

  • Host key validation failed for EC2 Instance Connect

Cause 1 of 5

The instance host keys were rotated and AWS still holds the old ones

1Check

AWS keeps a trusted host key database. If you regenerate the instance's SSH host keys, the new keys are not uploaded on their own, so the browser client's check fails.

ls /usr/share/ec2-instance-connect/ /opt/aws/bin/ 2>/dev/null

2Fix

Log in with your key pair and run the eic_harvest_hostkeys script. It is in /usr/share/ec2-instance-connect/ on Ubuntu and in /opt/aws/bin/ on Amazon Linux 2. A successful run prints nothing.

cd /usr/share/ec2-instance-connect/
./eic_harvest_hostkeys

3Verify

Open the browser client again; the host key check now passes.

# EC2 console > Instances > select instance > Connect > EC2 Instance Connect > Connect

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 5

The ec2-instance-connect package on an Ubuntu instance is out of date

1Check

The AWS page lists an old ec2-instance-connect package as a possible cause on Ubuntu.

dpkg -l ec2-instance-connect

2Fix

Connect another way (your key pair works) and update the packages on the instance.

sudo apt update && sudo apt upgrade

3Verify

Try the browser connection again.

dpkg -l ec2-instance-connect

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 5

The security group does not allow SSH from the EC2 Instance Connect service

1Check

From the console, the traffic that reaches the instance comes from the EC2 Instance Connect service, not from your computer. AWS manages its addresses in a prefix list named com.amazonaws.<region>.ec2-instance-connect (IPv4) and com.amazonaws.<region>.ipv6.ec2-instance-connect (IPv6). Your own-IP rule for port 22 does not cover it.

aws ec2 describe-security-groups --group-ids sg-1234567890abcdef0

2Fix

Add an inbound SSH rule whose source is that prefix list. In the console, pick the prefix list from the Source field. With the CLI you need the prefix list ID (shown in the VPC console under Managed prefix lists).

aws ec2 authorize-security-group-ingress \
  --group-id sg-1234567890abcdef0 \
  --ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,PrefixListIds=[{PrefixListId=pl-0123456789abcdef0}]'

3Verify

Reconnect from the EC2 console.

aws ec2 describe-security-groups --group-ids sg-1234567890abcdef0

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 5

The instance has no public address, or its subnet has no route to the internet

1Check

To connect over the internet, the instance needs a public IPv4 or IPv6 address and must sit in a public subnet with a route to the internet. A default public subnet routes IPv4 only, not IPv6.

aws ec2 describe-instances --instance-id i-1234567890abcdef0

2Fix

Give the instance a public address and a route, or use an EC2 Instance Connect Endpoint, which lets you reach an instance over SSH even when it has no public IPv4 or IPv6 address.

# EC2 console > Instances > Connect > EC2 Instance Connect Endpoint tab

3Verify

Connect again with the method you chose.

aws ec2 describe-instances --instance-id i-1234567890abcdef0

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 5 of 5

Your browser cannot reach the Instance Connect proxy

1Check

The console opens a WebSocket on port 443 to a Region-specific proxy, for example prod.us-east-1.oneclickv2-proxy.ec2.aws.dev for US East (N. Virginia). A firewall or VPN that blocks it makes the connection fail.

curl -sS -o /dev/null -w '%{http_code}
' https://prod.us-east-1.oneclickv2-proxy.ec2.aws.dev

2Fix

Allow outbound HTTPS and WebSocket to that endpoint for your Region, or pause the VPN or proxy and try again.

# ask the network owner to allow prod.<region>.oneclickv2-proxy.ec2.aws.dev on port 443

3Verify

Open the browser client again.

# EC2 console > Instances > Connect > EC2 Instance Connect > Connect

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • If you ever regenerate host keys, run eic_harvest_hostkeys straight after.
  • Keep the Instance Connect prefix-list rule in the security group that your servers share, so new instances work at once.
  • Keep one working SSH key pair route, so a fault in the browser path never locks you out.
  • Update the instance packages on a regular schedule.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

Apache has a certificate, but https:// does not open

No line with :443 in the output of ss -ltnp

HTTPS needs four things at once: mod_ssl loaded, Apache listening on 443, a virtual host on 443 with SSLEngine on, and a firewall that lets 443 in.

4 causes, each with a fix

Servers

AWS CLI says you must specify a region

You must specify a region. You can also configure your region by running "aws configure".

The AWS CLI sends every request to one region and has none to use.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.