Skip to Content

Security groups

The stateful firewall around every EC2 instance, how its rules work, referencing other security groups, and the ports to know.

What it is

A security group is a virtual firewall for an EC2 instance (strictly, for its network interfaces). It decides which traffic may reach the instance and which traffic may leave it. It's the most basic piece of network security in AWS, and every instance has at least one.

A security group is just a list of allow rules:

  • Inbound rules control traffic coming to the instance: which protocol, which port, and from which source.
  • Outbound rules control traffic going from the instance to other places.
  • A source or destination can be an IP range (IPv4 or IPv6 CIDR), another security group, or a managed prefix list.

There are no deny rules. Anything not allowed is blocked.

How it works

A security group allows port 22 from the admin's computer, blocks it from another computer, and allows all outbound traffic A security group allows port 22 from the admin's computer, blocks it from another computer, and allows all outbound traffic

Inbound filtered by rules; outbound open by default.
  1. The admin's computer connects to the instance on port 22 (SSH). An inbound rule allows port 22 from that IP address, so the traffic gets through.
  2. Another computer tries the same port. No rule names its IP, so the security group drops the traffic. The instance never sees it.
  3. The instance connects out to the internet, for example to download updates. The default outbound rule allows all traffic, so it works.

Security groups are stateful: if a request is allowed in, the reply is allowed out automatically, and the other way round. Return traffic needs no rules.

These rules come from a real security group. A table is the clearest way to show them, because that's how the console shows them too:

RuleSourceWhy
HTTP 800.0.0.0/0public website
SSH 22203.0.113.25/32admin's IP
TCP 45670.0.0.0/0Java app

All three rules use TCP. 0.0.0.0/0 means every IPv4 address; /32 means exactly one address.

Good to know

  • One security group can be attached to many instances, and one instance can have several security groups (5 per network interface by default, up to 16). The rules of all of them are combined.
  • A security group belongs to one VPC in one Region. It can also be associated with other VPCs in the same Region, or share it through a shared VPC.
  • It lives outside the instance. Blocked traffic never reaches the operating system, so the instance can't log or see it.
  • A new security group has no inbound rules (everything in is blocked) and one outbound rule allowing all traffic.
  • Every VPC's default security group allows inbound traffic from instances in the same group and all outbound traffic. Instances launched without choosing a group get it.
  • Keep a separate security group for SSH access, so admin access can be reviewed and removed in one place.
  • A timeout means a security group problem. If connecting just hangs, a rule is missing.
  • "Connection refused" means the traffic arrived. The application isn't running or isn't listening on that port.
  • Quotas: 60 inbound and 60 outbound rules per security group by default.
  • Free: security groups have no charge.

Updated since the slides

The slides say a security group is locked to one Region and VPC. Since late 2024, Security Group VPC Associations allow a security group to be associated with other VPCs in the same Region and account (not the default security group or default VPC). It still can't cross Regions. For the exam, "a security group belongs to a VPC" remains the safe answer.

Referencing other security groups

A rule's source can be another security group instead of an IP range. Then any instance that has that security group attached is allowed, whatever its IP address.

The target instance's security group allows port 123 from security groups A and B; instances in A or B get through, an instance in C is blocked The target instance's security group allows port 123 from security groups A and B; instances in A or B get through, an instance in C is blocked

Rules that name security groups, not IPs.
  1. The target instance has security group A, whose inbound rules allow port 123 from security group A and from security group B.
  2. An instance with security group B connects: allowed.
  3. Another instance with security group A connects: allowed. A group can reference itself, so members can talk to each other.
  4. An instance with security group C connects: blocked, because no rule names group C.

Why it matters: instances come and go and their IPs change, especially with Auto Scaling. Referencing groups keeps the rules correct without listing IPs. The classic design is a chain:

  • The load balancer's security group allows 443 from the internet.
  • The web servers' security group allows 80 only from the load balancer's security group.
  • The database's security group allows 3306 only from the web servers' security group.

Nobody on the internet can reach the web servers or database directly.

Ports to know

  • 22, SSH (Secure Shell): log in to a Linux instance.
  • 21, FTP (File Transfer Protocol): upload files to a file share.
  • 22, SFTP (SSH File Transfer Protocol): upload files over SSH.
  • 80, HTTP: unencrypted websites.
  • 443, HTTPS: encrypted websites.
  • 3389, RDP (Remote Desktop Protocol): log in to a Windows instance.

Database ports such as 3306 (MySQL) and 5432 (PostgreSQL) come up in the Databases group.

Real-world examples

  • A three-tier web app uses the chain above, so the database accepts connections only from the app servers.
  • An admin team keeps one ssh-from-office security group with the office's IP range, attached only to the servers that need it.

Exam tips

  • "Instance must accept traffic only from the load balancer" → web servers' security group references the load balancer's security group.
  • "Connection times out" → security group (or network ACL) rule missing. "Connection refused" → the application.
  • Security groups are stateful and allow-only. Need to deny a specific IP? That's a network ACL.
  • "Allow SSH only from the company network" → inbound rule on port 22 with the company's CIDR, not 0.0.0.0/0.

Common traps

  • "Add a deny rule to the security group." There are none; use a network ACL.
  • "Add an outbound rule for the response traffic." Not needed: security groups are stateful.
  • "A security group is attached to a subnet." It's attached to instances (network interfaces). Network ACLs are the subnet-level firewall.

Hands-on lab: timeout vs connection refused

Goal: change security group rules and see the two kinds of failure for yourself.

Costs money

Uses the saa-web instance from the EC2 basics lab, about $0.02 an hour. If you terminated it, launch it again the same way.

Break the website

Open EC2 → Security Groups → saa-web-sg → Inbound rules → Edit inbound rules. Delete the HTTP rule and save. Reload http://<public IP> in a private browser window: it spins and then times out.

Fix it

Add the rule back: type HTTP, source Anywhere-IPv4 (0.0.0.0/0). The page loads again straight away, with no restart: rule changes apply immediately.

Try a port with nothing listening

Open http://<public IP>:8080. It times out: no rule allows 8080. Now add an inbound rule Custom TCP, port 8080, source My IP and try again. This time the browser reports connection refused almost instantly: the traffic reached the instance, but no program listens on 8080.

Look at the outbound rules

Open the Outbound rules tab: one rule allows all traffic to 0.0.0.0/0. That's why the user data script could download the web server.

Expected result: a timeout when a rule is missing, and a fast connection refused when the port is open but nothing is listening.

Clean up: delete the port 8080 rule. Keep saa-web for the Connecting to EC2 lab, or terminate it.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt