Sub topic 6
Controlling traffic with security groups
This chapter assumes you know CIDR notation, ports, and basic SSH, and that you have a VPC with at least one subnet. A security group is a virtual firewall attached to an instance's network interface. It decides which traffic may reach the instance and which traffic may leave it.
The working model
Security groups follow a few rules that explain most of their behavior:
- Deny by default for inbound. A new group allows no inbound traffic until you add a rule.
- Allow rules only. You cannot write a deny rule. If no rule matches, the traffic is dropped.
- Stateful. If you allow a request in, the reply goes out automatically, whatever the outbound rules say. The reverse also holds.
- Outbound allows everything by default. A new group has one rule that permits all outbound traffic.
- Rules are combined. An instance can have several groups. AWS merges all their rules into one set.
A group belongs to one VPC, so you must name the VPC when you create it.
Creating a group and adding a rule
The commands below create a group and allow SSH (TCP port 22) only from one example range.
aws ec2 create-security-group \
--group-name web-sg \
--description "Web server access" \
--vpc-id vpc-0123456789abcdef0
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.0/24The first command prints a group ID such as sg-0123456789abcdef0. Use that ID in the second command. Each rule has a protocol, a port or port range, and a source. The source is usually a CIDR block, but it can also be another security group.
Choosing sources carefully
A source of 0.0.0.0/0 means the whole internet. That is reasonable for a public web server on ports 80 and 443. It is a poor choice for SSH, because bots scan port 22 constantly. Restrict administrative ports to your own address range.
A rule can also name another security group as its source. For example, a database group can accept port 5432 only from the application group. This keeps working when instances are replaced or their IP addresses change, so it is usually better than listing IP addresses.
Common errors
- Connection times out. A timeout usually means a security group dropped the packets. Check that a rule covers the right port and that its source includes your current public IP. A refused connection means the traffic arrived but nothing is listening.
- Wrong VPC. You cannot attach a group from one VPC to an instance in another. Launching fails with an error about the group not existing.
- Expecting deny rules. To block one address while allowing a wider range, narrow the allowed range instead. Network ACLs, which work at the subnet level, are the tool that supports explicit denies.
- Changes seem delayed. Rule changes apply to running instances immediately. You do not need to restart them.
Exercise
A web server must accept HTTPS from anyone and SSH only from your office range 198.51.100.0/24. Which inbound rules do you add, and do you need an outbound rule for the replies?
Show answer
Add TCP 443 from 0.0.0.0/0 and TCP 22 from 198.51.100.0/24. You do not need an outbound rule for the replies, because security groups are stateful and responses to allowed inbound traffic are permitted automatically.