To connect use ssh like so:
ssh -i keyname.pem username@xxx.xx.xxx.xx
Where keyname.pem
is the name of your private key, username
is the correct username for your os distribution, and xxx.xx.xxx.xx
is the public ip address.
When it times out or fails, check the following:
Security Group
Make sure to have an inbound rule for tcp port 22 and either all ips or your ip. You can find the security group through the ec2 menu, in the instance options.
Routing Table
For a new subnet in a vpc, you need to change to a routing table that points 0.0.0.0/0 to internet gateway target. When you create the subnet in your vpc, by default it assigns the default routing table, which probably does not accept incoming traffic from the internet. You can edit the routing table options in the vpc menu and then subnets.
Elastic IP
For an instance in a vpc, you need to assign a public elastic ip address, and associate it with the instance. The private ip address can't be accessed from the outside. You can get an elastic ip in the ec2 menu (not instance menu).
Username
Make sure you're using the correct username. It should be one of ec2-user
or root
or ubuntu
. Try them all if necessary.
Private Key
Make sure you're using the correct private key (the one you download or choose when launching the instance). Seems obvious, but copy paste got me twice.
UFW (Ubuntu Firewall)
The default firewall may have been enabled before disconnecting, causing additional issues from the instance side, not necessarily from the server settings. Check this answer for quick disabling of ufw