Sub topic 7
Security groups: stateful instance-level firewalls
This chapter assumes you can read CIDR notation, know what TCP ports are, and have a VPC with subnets from the earlier chapters. You also need the AWS CLI configured. The commands use the $VPC_ID variable from the VPC creation step.
What a security group is
A security group is a virtual firewall attached to a network interface (ENI) in your VPC. An EC2 instance, an RDS database, and a load balancer each have at least one ENI, so each is protected by the security groups on its interface. The group is not attached to a subnet.
Two properties define its behavior:
- Stateful: If a request is allowed in, the reply is automatically allowed out, whatever the outbound rules say. The same holds in the other direction.
- Allow-only: You write rules that permit traffic. You cannot write a deny rule. Anything not allowed is dropped.
A new security group has no inbound rules, so it blocks all inbound traffic. Its default outbound rule allows all traffic.
Anatomy of a rule
Each rule has a protocol, a port or port range, and a source (for inbound) or destination (for outbound). The source can be one of these:
- A CIDR block, such as
0.0.0.0/0or203.0.113.0/24 - Another security group
- A managed prefix list
Referencing another security group is the most useful option. The rule then matches any interface that carries that group, so it keeps working as instances are added, removed, or replaced. It does not depend on their IP addresses.
Worked example: app tier and database tier
The commands below create two groups. The database accepts PostgreSQL traffic only from members of the app group, and the app group accepts HTTPS from anywhere.
# Assumes VPC_ID is already set to your VPC's ID.
APP_SG=$(aws ec2 create-security-group --group-name app-sg --description "App tier" --vpc-id "$VPC_ID" --query GroupId --output text)
DB_SG=$(aws ec2 create-security-group --group-name db-sg --description "Database tier" --vpc-id "$VPC_ID" --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id "$APP_SG" --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id "$DB_SG" --protocol tcp --port 5432 --source-group "$APP_SG"The database group has no CIDR rule at all. A database instance with only db-sg cannot be reached from the internet or from any other instance, even one in the same subnet. Replies to allowed queries flow back automatically because of statefulness.
How rules are evaluated
- All rules in a group are evaluated together, and there is no rule order.
- If an interface has several groups, their rules combine as a union. Traffic is allowed if any rule in any group allows it.
- Changes take effect immediately on existing connections' future packets. You do not restart anything.
- Security groups belong to one VPC. A group in one VPC cannot be attached to resources in another.
Common errors
- Expecting a deny rule. To block one address inside an otherwise open range, you must narrow the allowed range. A network ACL is the tool for explicit denies, covered in the next chapter.
- Using a public IP as a source. Traffic between instances in the same VPC arrives from private addresses. Reference the security group instead.
- Leaving the default group in use. The VPC's default security group allows traffic from its own members. Create purpose-specific groups instead.
- Opening SSH to the world. A rule for port 22 from
0.0.0.0/0invites scanning. Restrict it to a known CIDR, or use Session Manager and no port at all.
Exercise
An instance has a security group with an inbound rule allowing TCP 8080 from 10.0.1.0/24 and no outbound rules other than the default. A client in that range connects on port 8080. Do you need to add an outbound rule for the response to reach the client? Why?
Show answer
No. Security groups are stateful, so the reply to an allowed inbound connection is permitted automatically, independent of outbound rules.