Sub topic 1
What a Security Group Does
Prerequisites
Assume you know IP addresses, CIDR notation, TCP ports, EC2 instances, VPCs, and subnets. You should also be able to run AWS CLI commands in the correct account and Region.
What a Security Group Does
A security group is a virtual, stateful firewall for resources in an Amazon VPC. It controls whether network traffic may reach or leave a resource. Security groups contain allow rules. They do not contain explicit deny rules.
A security group does not protect an entire subnet. It applies to the network interfaces attached to resources. For an EC2 instance, the elastic network interface (ENI) receives the security groups. An instance can have multiple groups, and one group can apply to multiple interfaces.
Where Security Groups Apply
A security group belongs to one VPC. You cannot attach a group from one VPC to an interface in another VPC. The group filters traffic at the interface, regardless of whether the interface is in a public or private subnet.
Some AWS services create interfaces on your behalf. Their traffic is still evaluated through the security groups attached to those interfaces.
Reading and Writing a Rule
A rule describes a direction, protocol, port range, and traffic source or destination. Inbound rules control traffic entering an interface. Outbound rules control traffic leaving it. For example, TCP port 443 from 0.0.0.0/0 permits HTTPS from every IPv4 address.
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0The source can be a CIDR range, an AWS-managed prefix list, or another security group. Use a narrow CIDR range when the set of clients is known.
Allow-Only Rules and Implicit Deny
AWS permits traffic when at least one applicable rule allows it. If no rule allows the traffic, the default result is an implicit deny. Rules are not evaluated as an ordered list, so adding an allow rule cannot override a deny rule because security groups have no deny rules.
Security groups are stateful: return traffic for an allowed connection is permitted automatically.
Exercise
An interface has no inbound rule for TCP port 8080. What happens to a new connection on that port?
Show answer
The connection is implicitly denied.