iTranslated by AI
Stop the Connection Timeouts: A Visual Guide to AWS Security Groups Using the Auto-Lock Analogy
*This page contains promotions.
What you will learn in this article
- The real cause of "Connection timed out" errors when SSHing into an EC2 instance.
- Mistakes in configuring inbound and outbound rules.
- Why you don't need to allow "return traffic": The magic of being "Stateful (automatic access)."
Introduction: The server isn't broken. It's just rejecting you.
"I launched an EC2 instance!" You confidently run the ssh command in your terminal, but then...
ssh: connect to host xx.xx.xx.xx port 22: Connection timed out
The cursor keeps blinking, and the connection mercilessly times out. This is the moment many beginners have their spirits crushed for the first time on AWS.
"Is the server down? Did I mess up the settings?" No, the server is perfectly fine. Think of it as the building's "auto-lock" system treating you like a suspicious intruder and blocking you at the entrance.
Today, let's master the most fundamental and powerful defense mechanism in AWS: the Security Group.
Recommended Books
▼ Amazon Web Services: Network & Server Construction from Basics
▼ Thorough Preparation: AWS Certified Solutions Architect - Associate Textbook
1. Security Groups are like "Apartment Auto-locks"
In my previous article, I compared a VPC to a "high-rise apartment complex." A security group is the "auto-lock on the door of each individual apartment" inside that building.
- VPC: The property grounds (you need a key just to enter here).
- Security Group: The apartment door (you need another key to enter here).
If you cannot connect via SSH, it means you successfully entered the property grounds, but you are being locked out because the door to your specific apartment won't open. AWS follows an ironclad rule: "If you aren't on the permit list, you don't even exist." That is why you don't get a "Connection Refused" error, but rather a "Timed out" error (the server simply ignores you).
🔹 Note: Other reasons why SSH might time out (beyond Security Groups)
In actual AWS environments, there are several other potential causes for timeouts. If you check your settings and it still doesn't connect, checking these will help streamline your troubleshooting:
① Route Table (The path is not connected)
For public subnets, you need a route for:
0.0.0.0/0 → Internet Gateway (IGW)
Without this, you cannot communicate with the outside world.
② Forgetting to attach an Internet Gateway (IGW)
If an IGW is not attached to the VPC,
you cannot leave the apartment grounds in the first place.
③ Network ACL (NACL) denials
Since NACLs are stateless, you need rules for both:
- Outbound (Going out)
- Inbound (Coming back)
Unlike security groups, return traffic will not pass unless you explicitly allow it.
④ OS-level firewalls
Operating systems like Amazon Linux or Ubuntu have firewalls such as:
- firewalld
- ufw
Even if AWS allows port 22, if the OS-level firewall blocks it, you won't be able to connect.
⑤ Incorrect key pair or username
This usually results in a "Permission denied" error rather than a timeout.
2. Inbound vs. Outbound
Auto-locks have two directions.
① Inbound (Traffic coming in)
- Meaning: Someone trying to enter your room from the outside.
- Default setting: "Deny all (no one can enter)."
- What you need to do: Add an allow rule: "Only allow traffic coming from my specific IP address via SSH (port 22)."
② Outbound (Traffic going out)
- Meaning: Someone trying to leave the room (e.g., the server trying to perform a
yum update). - Default setting: "Allow all (can go anywhere)."
- What you need to do: Usually, you can leave this as is.
Beginners often fall into the trap of forgetting inbound settings. "I forgot to open HTTP (port 80) even though it's a web server," or "I forgot SSH (port 22)." These mistakes alone will block all communication.
3. Master the Devilish Concept: "Stateful"
This is where it gets real. Security groups have the most powerful feature called "stateful."
In a nutshell, this is a feature that acts like a "VIP pass for your return trip."
What does this mean?
Outgoing (Outbound): You (the server) head out to a "convenience store (Google)." Since outbound traffic is allowed, you can leave.
Returning (Return): You finish your shopping and come back.
Here is the question: The "Inbound" rules do not allow traffic from Google. Will you be allowed into your room?
Answer: Yes.
Why? Because the auto-lock (security group) remembers you: "Oh, you're the person who just left the room, right? Then you don't need to check your key on the way back." This is what being stateful (maintaining connection state) means.
▼ [Diagram] The Magic of Stateful

💡 A quick word from our sponsor
I have published a notebook with illustrated explanations.
▼ Click here for a free preview: https://zenn.dev/miyaco_log/books/d26a6fe78b33e9
4. Don't do the "Full Open"
If you can't connect, are you desperately resorting to settings like these?
- Protocol: All traffic
- Source: 0.0.0.0/0 (Anywhere)
This is the equivalent of "destroying the auto-lock and leaving your front door wide open 24/7." Bots from all over the world will find your server and start attacking it within minutes.
Correct Settings:
- SSH (22): Select "My IP" (only your PC's IP).
- HTTP (80): 0.0.0.0/0 (OK for anyone to view if it's a website).
The AWS console has a "My IP" button. Just by clicking this, your current IP address is automatically entered. There is no reason not to use it.
Conclusion: Walls are not your "Enemy," they are your "Friend"
A "Connection timed out" error is proof that your security group is "doing its job properly." It is effectively blocking suspicious traffic.
So, don't get frustrated; just review your settings. Is your permit included in the "Inbound" rules? Whether you can pass through the wall depends entirely on the "key" you hold.
📚 For those who want to improve their troubleshooting skills
"I understand the logic, but I want to be able to identify the cause when the network doesn't connect in a real-world environment."
"I want to learn deeper analysis methods like VPC Flow Logs."
For those people, I recommend this book, which is packed with troubleshooting know-how. You will internalize the flowcharts for narrowing down "Why isn't it connecting?"
▼ Amazon Web Services: Network & Server Construction from Basics
Also, if you want to learn AWS foundational knowledge (SAA scope) comprehensively, including security groups, this book remains the gold standard.
▼ Thorough Preparation: AWS Certified Solutions Architect - Associate Textbook
🍵 Recommended Reading: "AWS Illustrated Series"
Reading this article in conjunction with my articles on "VPC (The High-Rise Apartment)" and "NAT Gateway (The Butler)" will give you a complete grasp of the "inside and outside" of networking.
Discussion