Sub topic 1
Three ways to reach an instance
An EC2 instance has no screen or keyboard. To run commands on it, you connect over the network. This chapter compares the three standard methods, so the later chapters on each one make sense. It stays at the level of the working model and does not walk through setup.
Prerequisites
This chapter assumes you know the following:
- Basic Linux command line use.
- Basic EC2 concepts: instances, AMIs, and VPC subnets.
- Basic IAM concepts: users, roles, and policies.
- The AWS CLI installed and configured with credentials.
The three methods
You can get a shell on a Linux instance in three ways:
- SSH with a key pair. You run an SSH client on your machine and connect to port 22 on the instance. You prove your identity with a private key whose public half was placed on the instance when it launched.
- EC2 Instance Connect. This is still SSH, but AWS pushes a temporary public key to the instance for you. The key stays valid for about 60 seconds, so you do not manage a long-lived key on the instance.
- AWS Systems Manager Session Manager. You ask the Systems Manager service to open a session. An agent on the instance connects out to the service, so the instance needs no inbound port. IAM permissions decide who may start a session.
What each method needs
The main difference is where access control lives. SSH and Instance Connect depend on the network path and on keys. Session Manager depends on IAM and on an agent running on the instance.
| Requirement | SSH with key pair | EC2 Instance Connect | Session Manager |
|---|---|---|---|
| Inbound port 22 open | Yes | Yes | No |
| Network path to the instance | Public IP or private route | Public IP or private route | Outbound access to Systems Manager |
| Long-lived key on your machine | Yes | No, a temporary key is used | No |
| Identity check | Possession of the private key | IAM permission to push a key | IAM permission to start a session |
| Agent on the instance | No | Instance Connect software, preinstalled on some AMIs | SSM Agent and an instance profile |
| Session record | Only what you set up yourself | Mostly your own SSH logs, plus CloudTrail for key pushes | CloudWatch Logs, S3, and CloudTrail |
How the traffic flows
With SSH, your client talks directly to the instance. With Session Manager, both sides talk to a service, and the service relays the data.
The outbound connection is why Session Manager works on instances in a private subnet with no public IP. The instance still needs a way to reach Systems Manager, either through a NAT gateway or through VPC endpoints.
Common mistakes
- Assuming a correct key is enough for SSH. If the security group or route table blocks port 22, the connection times out before the key is ever checked.
- Opening port 22 to 0.0.0.0/0 for convenience. Restrict it to your own address.
- Expecting Session Manager to work on any instance. It also needs the SSM Agent running and an instance profile with the right permissions.
Where this goes next
The next chapters cover network reachability and key pairs, then connecting with ssh, troubleshooting, and Instance Connect. Later chapters cover Session Manager setup, sessions, tunneling, logging, and how to choose between the methods.
Exercise
An instance sits in a private subnet with no public IP and no inbound security group rules. Which of the three methods can reach it from your laptop, and why?
Show answer
Session Manager. The SSM Agent on the instance makes an outbound connection to Systems Manager, so no inbound port or public IP is needed. The instance still needs an instance profile and a path to Systems Manager, through a NAT gateway or VPC endpoints. SSH and Instance Connect both need a network path to port 22, which this instance does not offer.
Exercise
Your SSH attempt to a public instance hangs and then reports a timeout. Is the private key the likely cause? Explain.
Show answer
No. A timeout means packets never reached the SSH service, so the cause is on the network side, such as a security group without a rule for port 22 from your address, or a route table without a route to an internet gateway. A bad key produces a prompt for a password or a "Permission denied" message, because the server was reached.