Skip to Content

Connecting to EC2

Logging in to an instance with SSH or PuTTY, EC2 Instance Connect, Session Manager and the EC2 Instance Connect Endpoint, and fixing SSH problems.

What it is

Running commands on a Linux instance takes a remote shell on it. The classic way is SSH (Secure Shell) on port 22 with a key pair. AWS also offers ways that need no key file, and even ways that need no open port or public IP. Windows instances use RDP (Remote Desktop) on port 3389 instead.

The four ways to connect to Linux

Four ways to connect: SSH or PuTTY over the internet, EC2 Instance Connect from the browser, Session Manager through the SSM Agent, and an EIC Endpoint to a private IP Four ways to connect: SSH or PuTTY over the internet, EC2 Instance Connect from the browser, Session Manager through the SSM Agent, and an EIC Endpoint to a private IP

What each method needs on the instance side.

SSH client or PuTTY

The client connects from a local computer to the instance's public IP on port 22, proving its identity with the private key of the instance's key pair.

  • macOS, Linux and Windows 10 or later have the ssh command built in.
  • Older Windows uses the free PuTTY tool, which needs the key in .ppk format (PuTTYgen converts a .pem).
  • The security group must allow port 22 from the client's IP, and the instance needs a public IP.
chmod 400 saa-lab-key.pem
ssh -i saa-lab-key.pem ec2-user@<public IP>

The username depends on the AMI: ec2-user for Amazon Linux, ubuntu for Ubuntu.

EC2 Instance Connect

EC2 Instance Connect opens an SSH session in the browser from the EC2 console, or from a local SSH client with the AWS CLI.

  • No key file is needed. AWS pushes a temporary public key to the instance, valid for 60 seconds, and connects with it.
  • IAM policies decide who may connect, and every connection is logged in CloudTrail.
  • It's pre-installed on Amazon Linux 2023, Amazon Linux 2 and Ubuntu 20.04 or later.
  • The instance still needs a public IP and port 22 open, from the EC2 Instance Connect service's IP range (an AWS-managed prefix list) when the console is used.
  • No extra charge.

Session Manager

Session Manager, part of AWS Systems Manager, opens a shell through the SSM Agent running on the instance.

  • No inbound port, no SSH keys, no public IP needed. The agent connects out to Systems Manager, so the security group can have zero inbound rules.
  • The instance needs an IAM role with the AmazonSSMManagedInstanceCore policy, and a route to the Systems Manager endpoints (internet access or VPC endpoints).
  • Access is controlled with IAM, and sessions can be logged to S3 or CloudWatch Logs.
  • Works for Linux and Windows. The agent is pre-installed on Amazon Linux and recent Windows AMIs.

It's the most secure option and the one AWS recommends for admin access. It's covered again in the Monitoring & Governance group.

EC2 Instance Connect Endpoint

An EC2 Instance Connect Endpoint (EIC Endpoint) reaches instances that have only a private IP, from the internet, without a bastion host or internet gateway.

  • The endpoint is created in a subnet of the VPC. It acts as an identity-aware proxy: IAM checks the caller before any traffic enters the VPC.
  • It supports SSH and RDP. The instance's security group allows port 22 (or 3389) from the endpoint.
  • No extra charge, apart from data transfer between AZs.

Updated since the slides

The slides say EC2 Instance Connect works out of the box only with Amazon Linux 2. It now also comes pre-installed on Amazon Linux 2023 and Ubuntu 20.04 and later. The EC2 Instance Connect Endpoint (2023) isn't in the slides; it's how AWS now suggests reaching private instances without a bastion host.

Troubleshooting SSH

Most connection problems are one of these:

  • Connection times out: the security group doesn't allow port 22 from the client's current IP (a home IP may have changed), or the instance has no public IP or isn't in a public subnet.
  • Permission denied (publickey): wrong key file, wrong username, or the key file permissions are too open (run chmod 400 on it).
  • Unprotected private key file: same fix, chmod 400.
  • Connection refused: the instance is still booting, or the SSH service isn't running.
  • Nothing works: use EC2 Instance Connect or Session Manager instead. Any one working method is enough.

Exam tips

  • "Connect without opening port 22", "no SSH keys", "no bastion host", "audit sessions" → Session Manager.
  • "Connect to an instance with no public IP from the internet, without a bastion" → EC2 Instance Connect Endpoint (or Session Manager).
  • "Temporary SSH key, controlled by IAM" → EC2 Instance Connect.
  • Port 22 for SSH to Linux, port 3389 for RDP to Windows.

Common traps

  • "EC2 Instance Connect needs no open port." It still uses SSH on port 22. Session Manager is the one with no inbound port.
  • "Lost the key pair? Download it again." AWS keeps only the public key. Use Instance Connect or Session Manager to get in, or attach the volume to another instance.
  • "Allow SSH from 0.0.0.0/0." Works, but exposes the instance to the whole internet. Restrict it to the admin's IP.

Hands-on lab: three ways in

Goal: connect to the same instance with SSH, EC2 Instance Connect and Session Manager.

Costs money

Uses the saa-web instance, about $0.02 an hour. Session Manager itself has no charge for shell sessions.

SSH from your computer

On macOS or Linux, or Windows 10 and later in PowerShell, go to the folder with saa-lab-key.pem and run:

chmod 400 saa-lab-key.pem
ssh -i saa-lab-key.pem ec2-user@<public IP>

Type yes at the fingerprint question. You're in: try whoami and curl localhost. Type exit to leave. (On Windows, chmod isn't needed; use PuTTY with a .ppk key on older versions.)

EC2 Instance Connect

In the console, select saa-web and choose Connect → EC2 Instance Connect → Connect. A shell opens in a browser tab, with no key file. If it fails, add an inbound rule to saa-web-sg for port 22 from the prefix list com.amazonaws.us-east-1.ec2-instance-connect.

Give the instance a role for Session Manager

Open IAM → Roles → Create role, trusted entity AWS service → EC2, attach AmazonSSMManagedInstanceCore, name it saa-ssm-role. Then in EC2 select saa-web and choose Actions → Security → Modify IAM role, pick saa-ssm-role and save.

Connect with Session Manager

Wait a minute or two, then choose Connect → Session Manager → Connect. You get a shell as ssm-user. Now remove the SSH rule from saa-web-sg and connect with Session Manager again: it still works, because no inbound port is needed.

Expected result: three working shells, and proof that Session Manager needs no inbound rule.

Clean up: terminate saa-web if you're not continuing to the IP addressing lab. Detaching the role isn't needed, but you can delete saa-ssm-role when you finish this group.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt