AWS in 1 Min · Episode 66

EC2 key pairs and how to connect to an instance

And safer ways to connect. Watch the one-minute video, then read the details.

David Gomez1:20 video · 3 min read

"AWS keeps a copy of your private key." It doesn't. Lose that file and AWS can't give it back, so you can end up locked out of your own server. Key pairs are the classic way to log in to EC2, but they come with risks that newer options remove.

What a key pair is

A key pair is two matching keys used to prove your identity when you connect:

  • The public key is stored by Amazon EC2 and placed on the instance at first boot (on Linux, in ~/.ssh/authorized_keys).
  • The private key is yours. If EC2 generates the pair, you download the private key once, at creation. You can also import the public key of a pair you created yourself.

Because EC2 doesn't keep a copy of the private key, there is no way to recover it if you lose it. There are procedures to regain access to an instance, but they take effort.

Connecting with a key

  • Linux: you connect with SSH, presenting the private key. The instance's security group must allow port 22 from your address.
  • Windows: the private key is used to decrypt the administrator password, and you then connect with Remote Desktop.

The weak points

Key files are long-lived credentials, and they behave like it:

  • A key file sits on a laptop, and anyone who gets the file can connect to every instance that uses it.
  • Keys get shared in chats and wikis so that "the team" can log in. Then nobody knows who actually connected.
  • A key never expires. When someone leaves the company, the key they copied still works until you replace it on every server.
  • Port 22 has to be open to somewhere. A common mistake is opening it to the whole internet (0.0.0.0/0), which invites constant scanning and brute-force attempts.

Better: EC2 Instance Connect

EC2 Instance Connect removes the permanent key. When you connect, it pushes a temporary public key to the instance, where it stays valid for 60 seconds, just long enough to open the SSH session. Who is allowed to do that is controlled by IAM policies, and the request is recorded in AWS CloudTrail.

Best: Session Manager

Session Manager, part of AWS Systems Manager, gives you a shell in the browser or through the AWS CLI with:

  • No keys to create, share or lose.
  • No open inbound ports: the agent on the instance makes an outbound connection, so port 22 can stay closed.
  • Access through IAM: you grant and revoke access per person or role, in one place.
  • Logging: session activity can be sent to Amazon CloudWatch Logs or Amazon S3.

The instance needs the SSM Agent (preinstalled on many AWS-provided images), an IAM role that allows Systems Manager, and a network path to the Systems Manager endpoints.

Where AI fits

Machine learning servers deserve this more than most. A GPU instance is an expensive target: attackers who get in can run their own workloads on your bill, and the box often holds valuable models and training data. Yet it is tempting to share one key file among several data scientists, with SSH open so everyone can reach their notebooks.

Give each person access through Session Manager instead. It supports port forwarding, so a notebook or dashboard running on the instance can be opened locally without exposing any port to the internet. Access follows the person's IAM identity and ends when their permissions do.

Quick recap

  • A key pair is a public key on the instance and a private key you keep. AWS can't recover a lost private key.
  • Key files are a liability: shared, never expiring, and they need port 22 open.
  • Prefer Session Manager (or EC2 Instance Connect): no keys to manage, no open inbound ports, access through IAM.
AWS in 1 Min #66 · 1:20

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS