Sub topic 1
What a security group does
Prerequisites
This chapter assumes you know the following:
- Basic TCP/IP: IP addresses, CIDR notation, ports and protocols.
- What an Amazon EC2 instance is.
- What a VPC and a subnet are.
- How to run AWS CLI commands with working credentials.
The working model
A security group is a virtual firewall that controls which network traffic can reach a resource and which traffic that resource can send. It belongs to one VPC. You attach it to the elastic network interfaces (ENIs) of resources such as EC2 instances. Later chapters cover attachment in detail. Here, it is enough to know that the group filters traffic at the interface, before packets reach your operating system.
A security group holds two lists of rules:
- Inbound rules describe traffic allowed to enter.
- Outbound rules describe traffic allowed to leave.
Each rule names a protocol, a port range, and a source (for inbound) or destination (for outbound). Three properties define how a group behaves:
- Allow-only. Rules can only permit traffic. You cannot write a rule that denies. Traffic that no rule permits is dropped.
- Stateful. If a rule allows a connection in one direction, the reply traffic is allowed automatically. You do not need a matching rule for the return path.
- Additive. A resource can have several groups. AWS combines all their rules, and traffic is allowed if any rule in any attached group permits it.
A first example
The commands below create a group in a VPC and allow HTTPS from any IPv4 address. Replace the IDs with your own. The first command prints the new group's ID, and you use that ID in the second command.
aws ec2 create-security-group \
--group-name web-sg \
--description "Web tier" \
--vpc-id vpc-0123456789abcdef0
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0The rule says: allow TCP on port 443 from 0.0.0.0/0. Here 0.0.0.0/0 means every IPv4 address. Once an instance has this group attached, clients anywhere can open HTTPS connections to it. Nothing else is allowed in. Port 22, port 80, and every other port stay closed, because no rule opens them.
A new group starts with no inbound rules and one outbound rule that allows all traffic. The instance can therefore make outbound connections, but nobody can connect to it until you add inbound rules.
What a security group does not do
Knowing the limits helps you pick the right tool and avoid common errors.
- It cannot deny specific addresses. To block one hostile IP while allowing everyone else, you need a network ACL, which is covered in a later chapter.
- It does not filter traffic inside the instance. Host firewalls such as
iptablesstill apply on top of it. - It does not span VPCs by itself. A group belongs to the VPC where you created it.
- It does not log traffic. Use VPC Flow Logs when you need a record of accepted and rejected connections.
Common mistakes
- Expecting a rule to block something. Removing a rule closes access. Adding a rule never closes it.
- Writing a return-path rule for replies. Stateful tracking already handles this.
- Opening
0.0.0.0/0on administrative ports such as 22 or 3389. Later chapters show safer sources.
Exercise
An instance has one security group with a single inbound rule allowing TCP 443 from 0.0.0.0/0. A client connects on port 443. Does the instance's reply need its own rule, and can the client connect on port 80?
Show answer
No, the reply needs no rule because security groups are stateful and the allowed connection's return traffic is permitted automatically. The client cannot connect on port 80, because no rule allows it and traffic that is not allowed is dropped.
Exercise
You want to block traffic from one specific IP address while keeping HTTPS open to everyone else. Can a security group do this?
Show answer
No. Security groups only have allow rules, so they cannot deny a single address. You would use a network ACL for an explicit deny.