How to Fix EC2 Connection Timeout Issues Quickly
To fix an
EC2 connection timeout, check that your instance's security group allows inbound SSH (port 22) from your IP and that the network ACLs and route tables are correctly configured. Also, ensure the instance is running and has a public IP if connecting over the internet.Why This Happens
Connection timeouts happen when your computer tries to reach the EC2 instance but cannot get a response. This usually occurs because the instance's security settings block the connection or the network setup is incorrect.
Common causes include:
- Security group missing inbound rule for SSH (port 22).
- Network ACL blocking traffic.
- Instance has no public IP or wrong subnet routing.
- Instance is stopped or unreachable.
hcl
resource "aws_security_group" "example" { name = "example" description = "Example security group" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } # Missing SSH port 22 inbound rule causes timeout
Output
ssh: connect to host ec2-instance-ip port 22: Connection timed out
The Fix
Add an inbound rule to the security group to allow SSH access on port 22 from your IP address. Also, verify the instance has a public IP and the subnet's route table allows internet access.
hcl
resource "aws_security_group" "example" { name = "example" description = "Example security group" ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["203.0.113.0/32"] # Replace with your IP } ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } # Ensure instance has public IP and subnet route table includes 0.0.0.0/0 to internet gateway
Output
ssh connection established successfully
Prevention
To avoid connection timeouts in the future:
- Always include SSH inbound rules in security groups before launching instances.
- Use Elastic IPs or ensure instances get public IPs if accessed over the internet.
- Check network ACLs to allow inbound and outbound traffic on required ports.
- Verify route tables have correct routes to internet gateways.
- Test connectivity right after instance launch to catch issues early.
Related Errors
Other errors similar to connection timeout include:
- Connection refused: The instance is reachable but SSH service is not running or blocked by firewall.
- Network unreachable: Incorrect subnet or route table configuration.
- Permission denied: Wrong SSH key or user name.
Quick fixes involve checking instance status, security group rules, and SSH key correctness.
Key Takeaways
Allow inbound SSH (port 22) from your IP in the EC2 security group to avoid timeouts.
Ensure the EC2 instance has a public IP and subnet route table routes traffic to the internet gateway.
Check network ACLs do not block inbound or outbound SSH traffic.
Verify the instance is running and reachable before troubleshooting network settings.
Test connectivity immediately after setup to catch configuration issues early.