Sub topic 15
Choosing between SSH, Instance Connect, and Session Manager
You have now used all three ways to get a shell on an EC2 instance. This chapter compares them so you can pick one for a given situation. It assumes you can open an SSH connection with a key pair, you know what a security group and an instance profile are, and you have the AWS CLI configured. It also assumes you have seen the Session Manager setup from the earlier chapters.
What you are really choosing
The three methods differ in three things: how you prove who you are, which network path the traffic takes, and what record is left afterward.
- SSH with a key pair. The instance holds your public key, usually installed at launch. You prove identity with the private key. Traffic goes straight to the instance on TCP port 22, so the security group, route table, and a public (or otherwise routable) address must allow it.
- EC2 Instance Connect. This is still SSH, and the network path is the same. The difference is the key. You call the AWS API to push a one-time public key to the instance, and it is valid for about 60 seconds. Your IAM permissions decide who can push a key.
- Session Manager. There is no SSH and no inbound port. The SSM Agent on the instance opens an outbound HTTPS connection to AWS, and your session is relayed over it. IAM decides who can start a session, and AWS can log what happens in it.
Side-by-side comparison
| Aspect | SSH with key pair | EC2 Instance Connect | Session Manager |
|---|---|---|---|
| Inbound port 22 needed | Yes | Yes | No |
| Public IP or route from you | Yes | Yes | No |
| Credential | Long-lived private key | Temporary key (about 60 seconds) plus IAM | IAM only |
| Who controls access | Whoever holds the key | IAM policy on key push | IAM policy on `ssm:StartSession` |
| Instance requirement | sshd, key in `authorized_keys` | sshd and the Instance Connect package | SSM Agent and instance profile |
| Outbound path from instance | None needed | None needed | HTTPS (443) to SSM endpoints or VPC endpoints |
| Built-in session record | Only what you set up on the host | CloudTrail records the key push, not the session | CloudTrail, plus optional CloudWatch Logs or S3 transcripts |
| Port forwarding and file tools | Native | Native | Port forwarding session, or SSH over SSM |
Two points in the table are easy to miss. First, Instance Connect improves how credentials are handled, not how the network is exposed. A key that expires after a minute does not help if port 22 is open to the whole internet. Second, Session Manager shifts the work to the instance and its network. If the agent cannot reach AWS, you cannot connect at all.
Decision criteria
Security posture
If your policy says no inbound ports, Session Manager is the only one of the three that fits. An instance in a private subnet with no public IP and no port 22 rule can still be reached, because only outbound traffic is required.
If you must keep SSH, restrict the security group to known source addresses and avoid 0.0.0.0/0. Prefer Instance Connect over distributing shared private keys, because there is no key to lose or to remove from every host when someone leaves.
Identity and offboarding
With plain SSH, access is tied to who holds a private key. Removing a person means editing authorized_keys on each instance, and you have no central view of who can connect where. With Instance Connect and Session Manager, access is an IAM policy. You revoke it once, and it applies everywhere, including to users federated through your identity provider.
Auditing
CloudTrail logs the SendSSHPublicKey call for Instance Connect, which tells you who was allowed in and when. It does not tell you what they did. Session Manager records the StartSession call in CloudTrail. You can additionally stream the session's input and output to CloudWatch Logs or S3, so you can review what was run. If a compliance rule requires command-level records, Session Manager is the practical choice.
Operational dependencies
Each method fails for different reasons, so consider what you can keep healthy.
- SSH depends on the security group, route table, public address, key, and login user all being correct.
- Instance Connect adds the instance package and the IAM permission on top of that.
- Session Manager depends on a running SSM Agent, an instance profile with the
AmazonSSMManagedInstanceCoremanaged policy (or equivalent permissions), and outbound access to the Systems Manager endpoints. In a subnet without internet or NAT access, that means VPC interface endpoints.
Tooling and workflow
Session Manager needs the Session Manager plugin installed next to the AWS CLI. Native SSH tools such as scp, rsync, and IDE remote development expect an SSH connection. If you need them without opening port 22, use SSH over Session Manager, which gives you both.
A decision flow
Worked scenarios
A development box you use alone
One instance, one user, a public IP, and a security group that allows only your address. Plain SSH with a ~/.ssh/config entry is simple and fast to set up. Use Instance Connect only if you do not want a permanent key on the instance.
A fleet in private subnets
The instances have no public IPs and the security groups have no inbound rules. Session Manager is the natural choice. Give the instances an instance profile, create VPC endpoints for ssm, ssmmessages, and ec2messages if there is no NAT, and grant users ssm:StartSession on the instances they need. Add session logging if you have audit requirements.
A team that must use SSH tooling
Developers need scp and IDE remote sessions, but you want port 22 closed. Run SSH over Session Manager with the ProxyCommand entry from the earlier chapter. The instance still needs sshd and an authorized key. Only the transport changes.
Contractors needing short access
A contractor needs access for a day. With Instance Connect or Session Manager you grant an IAM permission and remove it afterward. You do not hand out a private key or edit the instance.
Moving from SSH to Session Manager
A common migration is to confirm Session Manager works first and only then close port 22. First check that the instance is registered and online:
aws ssm describe-instance-information \
--filters "Key=InstanceIds,Values=i-0123456789abcdef0" \
--query "InstanceInformationList[].PingStatus" \
--output textIf the output is Online, start a session to prove it works end to end:
aws ssm start-session --target i-0123456789abcdef0Only after that, remove the inbound SSH rule. Use the same parameters you used when you added it:
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32Common mistakes when choosing
- Assuming Instance Connect closes the network exposure. It still needs port 22 reachable. Its benefit is short-lived keys and IAM control.
- Expecting Session Manager to work on a new instance with no profile. The instance will not appear in
describe-instance-informationuntil it has the right role and agent connectivity. - Treating CloudTrail as a session transcript. CloudTrail records API calls such as
StartSession. The commands typed in the shell appear only if you enabled session logging to CloudWatch Logs or S3. - Using broad IAM grants. An
ssm:StartSessionpermission on*lets a user open a shell on every managed instance. Scope it to specific instance ARNs or use tag conditions. - Keeping both paths open indefinitely. If SSH stays open to the world "just in case", you have gained no security from adding Session Manager.
Summary of the guidance
- If you cannot or will not open port 22, use Session Manager.
- If you need command-level audit records, use Session Manager with logging enabled.
- If you need SSH but want IAM-controlled, short-lived credentials, use Instance Connect.
- If it is a personal or short-lived host and the network is already restricted to your address, plain SSH is acceptable.
- If you need SSH-based tools with no inbound port, use SSH over Session Manager.
Exercise
A company runs instances in private subnets with no public IPs and no inbound security group rules. Auditors want a record of the commands administrators run. Which method fits, and what two things must you configure beyond the basics of getting a session to start?
Show answer
Session Manager fits, because it needs no inbound port or public IP. Beyond the instance profile, SSM Agent, and outbound path to the SSM endpoints (VPC endpoints if there is no NAT), configure session logging to CloudWatch Logs or S3 for the command record. Also scope the users' IAM permissions on ssm:StartSession to the instances they should reach. CloudTrail alone records only that sessions started, not the commands.
Exercise
A teammate says that because EC2 Instance Connect keys expire after 60 seconds, it is safe to leave port 22 open to 0.0.0.0/0. What is wrong with this reasoning?
Show answer
The temporary key limits how long a pushed credential is valid, but it does not reduce who can reach port 22. Anyone on the internet can still attempt connections, scan for vulnerabilities in sshd, and try other credentials such as any permanent key left in authorized_keys. The security group should still restrict the source to known addresses, or you should use Session Manager so that no inbound port is needed.
Exercise
You need to copy files with scp to instances that have no inbound port 22 rule. Which approach works, and what must still be present on the instance?
Show answer
Use SSH over Session Manager by adding a ProxyCommand entry that runs aws ssm start-session with the AWS-StartSSHSession document. The instance still needs a running sshd, an authorized public key for the login user, the SSM Agent, and an instance profile that lets it register with Systems Manager. Only the transport changes, so port 22 stays closed to the network.