Security Groups & Connecting Safely
Control network traffic with security groups and log in without opening SSH to the internet.
Security groups: a firewall per instance
A security group is a list of allow rules for traffic coming in (inbound) and going out (outbound).
- Rules only allow. Anything not allowed is blocked.
- They're stateful: if a request is allowed in, the reply is automatically allowed out.
- You can reference another security group as the source: "allow port 5432 only from instances in the
web-serversgroup". That's how tiers of an app talk to each other safely.
| Inbound rule | Meaning |
|---|---|
TCP 443 from 0.0.0.0/0 | Anyone on the internet can reach HTTPS |
TCP 5432 from sg-web | Only web servers can reach PostgreSQL |
Opening SSH (port 22) to 0.0.0.0/0 invites constant brute-force attempts from bots. Avoid it.
Connecting without SSH keys: Session Manager
AWS Systems Manager Session Manager gives you a shell in the browser or CLI, with no open inbound ports and no SSH keys. Access is controlled by IAM and every session can be logged.
Requirements: the instance needs the SSM Agent (preinstalled on Amazon Linux and recent Ubuntu AMIs) and an
instance profile role with the AmazonSSMManagedInstanceCore managed policy.
aws ssm start-session --target i-0123456789abcdef0Instance roles
Just like Lambda, code on EC2 should get AWS permissions from an IAM role (attached as an instance profile), never from access keys copied onto the machine.
Key takeaways
- Security groups are stateful, allow-only firewalls attached to instances.
- Reference other security groups to let app tiers talk without opening to the world.
- Use Session Manager instead of public SSH, and IAM roles instead of stored keys.
Check your understanding
Pick an answer to see the explanation.
1. Your web server's security group allows inbound 443. Do you need an outbound rule for the replies?