An EC2 security group is a virtual firewall attached to an instance's network interface that controls which traffic may reach it and leave it. You write rules that allow traffic by protocol, port and source or destination. Anything not explicitly allowed inbound is blocked, and because security groups are stateful, replies to allowed traffic are let back automatically.
Security groups are the first layer of network protection for EC2, and a wrong rule is the most common reason a new server is unreachable, or exposed to the whole internet.
Security groups follow a few rules worth remembering:
| Field | Meaning | Example |
|---|---|---|
| Type or protocol | Traffic kind | SSH, HTTPS, TCP, ICMP |
| Port range | Port or range | 22, 443, 8000-8100 |
| Source (inbound) or destination (outbound) | Who may connect | 203.0.113.25/32, 0.0.0.0/0, another security group |
| Description | A note for humans | Office SSH |
The source can be a CIDR block, a prefix list or another security group. 0.0.0.0/0 means any IPv4 address on the internet, and ::/0 means any IPv6 address.
A small web server usually needs:
203.0.113.25/32.0.0.0.0/0 and ::/0.0.0.0.0/0 and ::/0.Open SSH to the world only for a quick test and remove the rule after. Bots scan port 22 constantly.
authorize-security-group-ingress adds inbound rules and revoke-security-group-ingress removes them. The matching -egress commands manage outbound rules.
Instead of IP addresses, a rule's source can be a security group. This is the standard way to link tiers. For a web and database setup, the database group allows MySQL only from the web group:
Any instance in the web group can now reach the database on port 3306, whatever its IP address, and nobody else can. This keeps working as instances are added or replaced.
| Feature | Security group | Network ACL |
|---|---|---|
| Level | Instance network interface | Subnet |
| State | Stateful | Stateless |
| Rules | Allow only | Allow and deny, evaluated in order |
| Default | Deny inbound, allow outbound | Default ACL allows all |
Use security groups for almost everything. Network ACLs are an optional extra layer, for example to block a specific address range at the subnet.
/32 rule.ss -tlnp.No. It only has allow rules. To block a specific address, use a network ACL.
No. Rule changes apply immediately.
Yes. The rules of all attached groups are merged, and traffic is allowed if any rule permits it.
For a public website, yes, because the service is meant to be public. It is not safe for SSH, databases or admin panels.
A security group is a stateful, allow-only firewall for EC2 network interfaces. Inbound traffic is blocked until you add a rule, outbound is open by default, and changes take effect at once. Restrict SSH to your own address, open web ports only as needed, chain tiers by referencing groups, and use network ACLs only for subnet-level blocking.