Sub topic 15
Choosing between SSH, Instance Connect, and Session Manager
This chapter assumes you have read the earlier chapters. You should know how to connect with ssh and a key pair, what an instance profile is, and how Session Manager sessions start. Here you compare the three methods and pick one for a given requirement.
What differs between the methods
All three methods end in a shell on the instance. They differ in what you must open, what credential proves who you are, and what record is left behind.
| Question | SSH with a key pair | EC2 Instance Connect | Session Manager |
|---|---|---|---|
| Inbound port 22 needed | Yes | Yes | No |
| Public IP or network route to the instance | Yes | Yes | No. The instance only needs outbound HTTPS. |
| Credential | Long-lived private key | Temporary public key (about 60 seconds) | IAM credentials |
| Who is allowed | Anyone holding the key | IAM policy on `ec2-instance-connect:SendSSHPublicKey` | IAM policy on `ssm:StartSession` |
| Instance setup | Key pair at launch | Instance Connect package installed | SSM Agent and an instance profile |
| Central audit | None built in | CloudTrail records the key push | CloudTrail, plus optional CloudWatch Logs or S3 transcripts |
Reading the table
SSH is the baseline. It works with any client and any tool built on SSH, such as scp and rsync. Its weak points are key handling and exposure. A copied .pem file gives access until you remove it from the instance. Port 22 also stays reachable, even if you restrict it to one /32 address.
Instance Connect keeps the SSH path but replaces the permanent key. IAM decides who may push a key to which instance. The key expires quickly, so there is no key file to revoke later. You still need network reachability and an open port 22.
Session Manager removes the inbound path. The SSM Agent on the instance opens an outbound connection to Systems Manager, and your session travels over that connection. You can close port 22 entirely and keep the instance in a private subnet. The cost is setup: the agent, an instance profile, and a route to the Systems Manager endpoints (internet or VPC endpoints).
Decision flow
Common situations
- Private subnet, no public IP, strict audit rules. Use Session Manager. Enable logging to CloudWatch Logs or S3.
- Quick access to a development box you already expose. SSH with a
~/.ssh/configentry is fine. Restrict the security group to your own address. - Many people need occasional access and you do not want to distribute keys. Use Instance Connect, or Session Manager if you can drop port 22.
- You need
scp,rsync, or an IDE's remote SSH feature but want no open port. Use SSH over Session Manager with theProxyCommandentry from the earlier chapter. - You need to reach a web app or database on the instance from your laptop. Use a Session Manager port forwarding session.
Failure modes to weigh
Session Manager depends on a working agent. If the agent stops or the instance loses its path to the endpoints, you cannot connect, and an SSH fallback may be the only way in. Keep that fallback in mind when you close port 22 everywhere.
Instance Connect depends on the package on the instance. Some custom AMIs do not include it.
SSH leaves you responsible for key storage, rotation, and removing old keys from authorized_keys.
Exercise
A team runs instances in a private subnet with no public IPs and no inbound rules. Auditors require a record of every command typed in a shell. Which method fits, and which two settings give the record?
Show answer
Session Manager fits, because it needs no inbound port or public IP. Turn on session logging to CloudWatch Logs or S3 to capture the session transcript. CloudTrail also records each StartSession call, which shows who connected and when.