Every EC2 instance needs a way for administrators to reach it, and that path is one of the most attacked parts of any AWS account. Port 22 open to the whole internet, private keys shared between colleagues and forgotten key pairs are common findings in account reviews.
This guide compares the main ways to connect to EC2 instances: key pairs with SSH, EC2 Instance Connect, EC2 Instance Connect Endpoint and AWS Systems Manager Session Manager. It explains how each works, where each fits, and how to move toward access that is temporary, logged and does not depend on open inbound ports. Check current requirements in the official documentation, because supported operating systems and features evolve.
Compare the access options
| Method | Inbound port needed | Credential | Logging |
|---|---|---|---|
| SSH with a key pair | Yes, usually 22 | Long-lived private key | OS logs only, unless you add more |
| EC2 Instance Connect | Yes, 22 from the connecting source | Temporary key pushed through IAM | CloudTrail records the key push |
| EC2 Instance Connect Endpoint | Only from the endpoint inside the VPC | IAM plus SSH or RDP | CloudTrail records endpoint connections |
| Session Manager | No inbound port | IAM permissions | CloudTrail plus optional session logs |
The methods can be combined. Many teams keep a key pair for break-glass recovery while using Session Manager or Instance Connect for daily access.
Key pairs and SSH
An EC2 key pair consists of a public key that AWS places on the instance at launch and a private key that you keep. If AWS creates the key pair for you, the private key is offered for download only once. You can also import a public key generated on your own machine, which means the private key never leaves your control.
EC2 supports RSA and ED25519 key types for Linux instances; ED25519 keys are not supported for Windows instances. Treat private keys like passwords:
- Use one key per person or per automated system, never a single shared team key.
- Protect private keys with a passphrase and store them in a password manager or hardware-backed agent.
- Remove a person’s public key from
authorized_keyswhen they leave, rather than only deleting the key pair record in the console. Deleting the key pair in EC2 does not remove it from instances that already have it. - Rotate keys on a schedule and after any suspected exposure.
Lock down security group rules
A security group rule allowing port 22 or 3389 from 0.0.0.0/0 exposes the instance to constant automated scanning and password and key-guessing attempts. If you must allow direct SSH, restrict the source to specific IP ranges such as an office or VPN, and review those ranges regularly.
Better still, place instances in private subnets with no public IP address and reach them through one of the methods below. The VPC public vs private subnets guide explains how to design that network layout.
EC2 Instance Connect: temporary keys through IAM
EC2 Instance Connect lets an authorized IAM identity push a one-time public key to an instance. The key is valid for a short window, about 60 seconds, during which the user opens an SSH session. Access is controlled through IAM policies rather than through distributing long-lived private keys.
Instance Connect requires the Instance Connect package on the instance; it is preinstalled on some images such as recent Amazon Linux and Ubuntu AMIs. The instance still needs a reachable SSH port from the connecting source, so pair it with tight security group rules.

EC2 Instance Connect Endpoint: reach private instances
An EC2 Instance Connect Endpoint is a resource you create in a subnet of your VPC. It lets authorized users connect over SSH or RDP to instances in private subnets without a public IP address, a bastion host or an internet gateway route for that traffic.
The instance’s security group only needs to allow SSH or RDP from the endpoint’s security group. IAM controls who can open a tunnel through the endpoint, and connections are recorded in CloudTrail. This is often the simplest way to retire a bastion host.
Session Manager: no inbound ports at all
Session Manager, part of AWS Systems Manager, opens an interactive shell through the SSM Agent running on the instance. The agent makes outbound connections to Systems Manager, so the instance does not need any inbound port open.
To use it, an instance needs:
- The SSM Agent installed and running. It is preinstalled on many AWS-provided AMIs.
- An instance profile role with the permissions Systems Manager requires, commonly the
AmazonSSMManagedInstanceCoremanaged policy. - Outbound connectivity to the Systems Manager endpoints, either through a NAT gateway or through VPC interface endpoints for fully private subnets.
Session Manager adds controls that plain SSH lacks: IAM decides who can start a session on which instances, sessions can be logged to Amazon S3 or CloudWatch Logs, and sessions can be encrypted with a KMS key. It also supports port forwarding for reaching a database or internal web interface through the instance. Review the CloudTrail vs CloudWatch guide to decide where session records should be retained.
Windows instances
For Windows, the key pair is used to decrypt the initial administrator password rather than for SSH. After first sign-in, create named accounts, change or disable the default administrator password, and avoid exposing RDP to the internet. Fleet Manager in Systems Manager and Instance Connect Endpoint both offer RDP access without opening port 3389 publicly.
Lost keys and recovery options
AWS cannot recover a lost private key. Plan recovery before you need it:
- If the instance is managed by Systems Manager, connect through Session Manager and add a new public key.
- Use Instance Connect if the package is installed and IAM permissions allow it.
- As a last resort, stop the instance, attach its root volume to a helper instance, add a new key to
authorized_keys, and reattach it. Test this procedure on a non-production instance first, and make sure you have a recent snapshot. The AWS backup and restore guide covers snapshot planning.
Instance store volumes lose their data when an instance stops, so check the storage type before stopping an instance for recovery. The S3 vs EBS vs EFS guide explains the differences.
Access hardening checklist
- No security group allows SSH or RDP from
0.0.0.0/0or::/0. - Production instances sit in private subnets without public IPs.
- Daily access uses Session Manager, Instance Connect or an Instance Connect Endpoint, granted through IAM roles.
- Session activity is logged and retained according to your audit needs.
- Each person has individual credentials; no shared private keys.
- Departing users’ keys are removed from instances, not only from the EC2 console.
- A tested break-glass recovery procedure exists.
Apply the same checklist when you start working in an account you did not set up, including one you are assessing from AWS account listings: look for existing key pairs, open security groups and instance profiles before launching anything new. The AWS account handover checklist lists the wider access review.
EC2 access questions
Do I still need a key pair if I use Session Manager?
Not for Session Manager itself, which uses IAM permissions. Some teams keep a key pair for break-glass recovery, stored securely and used rarely.
Is it safe to open port 22 to the internet?
It is not recommended. Restrict SSH to specific trusted IP ranges, or remove inbound SSH entirely by using Session Manager or an Instance Connect Endpoint.
Does deleting a key pair in the EC2 console remove access?
No. Deleting the key pair record does not remove the public key from instances that already have it. Remove the key from each instance’s authorized keys file.
Can Session Manager work in a private subnet without internet access?
Yes, if you create VPC interface endpoints for the required Systems Manager services so the agent can reach them privately.
Which key type should I use for Linux instances?
EC2 supports RSA and ED25519 for Linux. ED25519 keys are compact and widely recommended, but they are not supported for Windows instances.
Editorial guide by Mark Biden · Reviewed 19 November 2026. Official sources are linked beside the relevant guidance. OldAccs is independent and is not affiliated with or endorsed by Amazon Web Services. Examples are illustrative; verify the current configuration and requirements of your own environment.

Leave a Reply