Sub topic 1
Three ways to reach an instance
Prerequisites
This chapter assumes you already know the following:
- Basic Linux command line use, including file permissions and running commands in a shell.
- Basic EC2 concepts: instances, AMIs (Amazon Machine Images), and VPC subnets.
- Basic IAM concepts: users, roles, and policies.
- The AWS CLI installed and configured with credentials.
Later chapters cover each method in detail. This chapter gives you the working model of all three, so the details have somewhere to fit.
What "reaching an instance" means
An EC2 Linux instance is a virtual machine. Running commands on it means getting an interactive shell, a way to forward network ports, or a file transfer channel. AWS gives you three common ways to do this:
- SSH with a key pair that you create and keep.
- EC2 Instance Connect, which is still SSH, but with a temporary public key that AWS pushes to the instance for you.
- Systems Manager Session Manager, which does not use SSH or any inbound port. An agent on the instance opens an outbound connection to AWS, and your session travels over it.
The three methods differ in two ways: where the trust decision is made, and which network direction has to be open. Those two differences drive most of the choices you will make later.
Method 1: SSH with a key pair
SSH (Secure Shell) is a protocol for an encrypted remote shell. The instance runs an SSH server (sshd) that listens on TCP port 22. You run an SSH client on your machine and prove who you are with a private key.
The authorization model has two halves:
- When you launch the instance, you name a key pair. EC2 places the public half in the login user's
~/.ssh/authorized_keysfile. - You keep the private half, a
.pemfile, and present it when you connect.
Whoever holds the private key can log in. AWS IAM is not involved at connection time, so IAM cannot revoke access for a person who has a copy of the key. Access control lives in files on the instance.
For the connection to succeed, all of these must be true:
- The instance is reachable at an IP address from where you are. That usually means a public IPv4 address, a route to an internet gateway, or a private path such as a VPN or a bastion host.
- A security group allows inbound TCP 22 from your address.
- Your client has the private key, with permissions that SSH accepts.
- You use the login user that the AMI defines, such as
ec2-useron Amazon Linux.
A typical connection looks like this:
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]The chmod 400 step matters. SSH refuses a private key that other users on your machine can read.
SSH is the most widely supported option. Every Linux AMI can do it, and tools such as scp, rsync, and Ansible expect it. The cost is that you operate an open inbound port and you manage keys yourself. The next chapters show how to narrow that exposure.
Method 2: EC2 Instance Connect
EC2 Instance Connect keeps the SSH protocol but changes how keys are handled. Instead of a long-lived key sitting in authorized_keys, you call an AWS API that sends your public key to the instance. The instance accepts that key for about 60 seconds. You then connect with SSH using the matching private key.
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0123456789abcdef0 \
--instance-os-user ec2-user \
--ssh-public-key file://$HOME/.ssh/id_ed25519.pub
ssh -i ~/.ssh/id_ed25519 [email protected]What changes compared with plain SSH:
- Authorization moves to IAM. The caller needs permission for the
ec2-instance-connect:SendSSHPublicKeyaction. You can scope that permission to specific instances and login users, and you can remove it to revoke access. - Keys expire. A leaked public key is useless after the window closes.
- API calls are logged. CloudTrail records each key push, so you can see who requested access.
What does not change:
- The network path is the same. The instance still needs a reachable IP address, and the security group must still allow inbound port 22. Instance Connect does not remove the open port.
- The instance needs a helper. It must run the EC2 Instance Connect software. It is installed by default on current Amazon Linux and recent Ubuntu AMIs. On other images you install it yourself.
The EC2 console's browser-based "Connect" button uses this mechanism. That button works only if the security group allows port 22 from the AWS address range used by the service.
Method 3: Session Manager
Session Manager is a feature of AWS Systems Manager. It reverses the connection direction:
- The SSM Agent on the instance makes outbound HTTPS connections (port 443) to Systems Manager endpoints.
- You call
ssm:StartSessionthrough the AWS API, using the console or the AWS CLI. - Systems Manager joins your session to the agent's existing outbound channel.
No process on the instance listens for your session, so the security group needs no inbound rule. The instance does not need a public IP address either, as long as it has a path to the endpoints through a NAT gateway or VPC endpoints.
aws ssm start-session --target i-0123456789abcdef0Running this from your own machine requires the Session Manager plugin for the AWS CLI. The console needs no plugin.
Three things must be in place:
- On the instance: the SSM Agent is running. It is preinstalled on Amazon Linux and many other AWS-provided AMIs.
- On the instance's role: an instance profile grants permissions that the agent uses to register with Systems Manager. The AWS managed policy
AmazonSSMManagedInstanceCoreis the usual choice. - For the user: IAM permits
ssm:StartSessionon that instance and on the session document.
Because authorization is IAM, there are no SSH keys to distribute, and you can tie each session to a named identity. Sessions can be logged to CloudWatch Logs or S3, and CloudTrail records the start of each session.
You can check whether an instance is ready with this command:
aws ssm describe-instance-information \
--query "InstanceInformationList[].[InstanceId,PingStatus]" \
--output textAn instance that shows Online is ready. An instance that is missing from the list usually has a problem with the agent, the instance profile, or the network path.
Session Manager can also carry other traffic. Port forwarding and SSH-over-SSM let you use tools that expect a TCP connection without opening port 22. Later chapters set these up.
Comparing the three methods
The table summarizes the differences that matter most.
| Aspect | SSH with key pair | EC2 Instance Connect | Session Manager |
|---|---|---|---|
| Inbound port on the instance | TCP 22 | TCP 22 | None |
| Public or routable IP needed | Yes | Yes | No, only outbound path to endpoints |
| Who authorizes you | Key file on the instance | IAM, then a temporary key | IAM |
| Key lifetime | Until you remove it | About 60 seconds | No SSH key |
| Instance requirement | `sshd` and key pair | `sshd` and Instance Connect software | SSM Agent and instance profile |
| Central session logging | No | Key push in CloudTrail | CloudTrail, plus CloudWatch Logs or S3 |
Common misunderstandings
- "Instance Connect closes port 22." It does not. The security group still needs an inbound rule, so restrict the source address as you would for plain SSH.
- "Session Manager needs no network." It needs no inbound network. The agent must still reach the Systems Manager endpoints over HTTPS, either through the internet, a NAT gateway, or interface VPC endpoints.
- "Session Manager replaces SSH completely." It starts a shell as a service-managed user,
ssm-user, by default. If you need file transfer tools or the standard SSH client behavior, you can tunnel SSH through it. - "A key pair is an AWS credential." A key pair is only an SSH credential for that instance. It gives no access to the AWS API.
Checking what an instance supports
Before choosing a method, you can read what an instance offers. This command shows the public IP, the key pair name, and whether an instance profile is attached:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query "Reservations[].Instances[].[PublicIpAddress,KeyName,IamInstanceProfile.Arn]" \
--output textThe output tells you something about each method:
- A value of
Nonefor the public IP means direct SSH from the internet is not possible. - A value of
Nonefor the key name means no key pair was assigned at launch, so plain SSH will not work unless a key was added in another way. - A value of
Nonefor the profile ARN means the agent has no permissions to register, so Session Manager will not work until you attach a profile.
Exercise
An instance is in a private subnet with no public IP address and no inbound security group rules. It has the SSM Agent running, an instance profile with AmazonSSMManagedInstanceCore, and a route to a NAT gateway. Which of the three methods can you use from your laptop, and why do the other two fail?
Show answer
Session Manager works. The agent opens an outbound HTTPS connection through the NAT gateway, so no inbound port or public IP is needed, and your IAM permission to call ssm:StartSession authorizes you. Plain SSH and EC2 Instance Connect both need a reachable IP address and an inbound rule for TCP port 22, and this instance has neither.