Sub topic 1
Three Ways to Reach an EC2 Instance
Prerequisites
Assume you know basic Linux commands, EC2 instances and subnets, IAM users and roles, and how to configure the AWS CLI. This chapter uses a running Linux instance and a private key that matches its EC2 key pair.
Three ways to reach an instance
You can reach an EC2 instance through:
- SSH: A network connection to port 22, authenticated with a private key.
- EC2 Instance Connect: AWS temporarily places your public key in the instance metadata. You then connect with SSH before the key expires.
- Session Manager: Systems Manager opens a session through the SSM Agent. The instance makes an outbound connection, so no inbound SSH port is required.
What must be true on the network before SSH works
SSH requires a reachable address and a permitted path:
- The instance needs a public IPv4 address, or you need network access through a VPN, bastion host, or another private route.
- Route tables and network ACLs must allow the traffic.
- The security group must allow inbound TCP port 22 from your current client address. Prefer a
/32rule for one address instead of0.0.0.0/0. - The instance must be running an SSH server and its operating system firewall must allow port 22.
A timeout usually indicates a routing, address, or filtering problem. “Connection refused” usually means the host is reachable but no service is accepting connections on port 22.
Key pairs and the right login user for your AMI
An EC2 key pair has a public key and a private key. AWS stores the public key for the instance, while you keep the private key. SSH proves possession of the private key without sending it to the instance.
The login user comes from the AMI, not from your AWS IAM username. Common users include ec2-user for Amazon Linux, ubuntu for Ubuntu, and admin for some Debian images.
Connecting with ssh
Use the instance's reachable address, the AMI's login user, and the matching key:
chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem [email protected]If authentication fails, verify the username and key before changing network rules. A Permission denied (publickey) error means the server was reached, but it rejected the offered credentials.
Exercise
Which access method avoids an inbound port 22 when the instance has a working SSM Agent and suitable IAM permissions?
Show answer
Session Manager.