Sub topic 3
Reading and writing a rule
This chapter assumes you know CIDR notation, TCP and UDP ports, and what a VPC is. You should also be able to run AWS CLI commands against a test account. Earlier chapters covered what a security group is and where it attaches. Here you learn to read a single rule and write one correctly.
What a rule is made of
A security group holds a list of rules. Each rule has the same few parts, whether you view it in the console, the CLI or Terraform.
| Part | What it means | Example |
|---|---|---|
| Direction | Inbound rules match traffic arriving at the interface. Outbound rules match traffic leaving it. | Inbound |
| Protocol | TCP, UDP, ICMP, ICMPv6, another IP protocol number, or all protocols (`-1`). | `tcp` |
| Port range | The destination ports the rule covers. For ICMP it holds a type and a code. | 443 to 443 |
| Source or destination | Where the traffic comes from (inbound) or goes to (outbound). | `0.0.0.0/0` |
| Description | Optional free text, up to 255 characters. | `Admin workstation` |
Every rule also gets an identifier of the form sgr-0123456789abcdef0. You need that ID when you modify a single rule or delete it by ID.
Direction decides what the last field means
The same field is called the source on an inbound rule and the destination on an outbound rule. An inbound rule says who may start a connection to the instance. An outbound rule says where the instance may start connections to. Both use the same kinds of value: a CIDR block, a prefix list or another security group. The next two chapters cover prefix lists and security group references in detail.
The port range always describes the destination port of the traffic. For an inbound rule that is the port on your instance. For an outbound rule it is the port on the remote system. A rule never matches on the client's source port.
Protocol and port range
For TCP and UDP, the port range is a start and an end. A single port such as HTTPS has both set to 443. A range such as 8000 to 8100 allows every port in that span.
Two special cases surprise people.
- All protocols. Protocol
-1means every protocol. It also means every port, and the port range is ignored. A rule that allows all traffic from0.0.0.0/0opens the instance completely. - ICMP. ICMP has no ports. The two numbers in the range hold the ICMP type and code. Type 8 is echo request, which is what
pingsends, and code-1means all codes. If you want all ICMP types, use type-1, and the code must also be-1.
A protocol that you name by number, such as 47 for GRE, has no port range either.
Choosing the source or destination value
The common values are these:
- An IPv4 CIDR block such as
10.0.0.0/16. A/32is exactly one address. - An IPv6 CIDR block such as
2001:db8::/32. - A managed prefix list ID such as
pl-0123456789abcdef0. - A security group ID such as
sg-0123456789abcdef0.
0.0.0.0/0 means every IPv4 address, and ::/0 means every IPv6 address. They are separate values. A rule for 0.0.0.0/0 does not cover IPv6 clients. If your service listens on IPv6, add a second rule for ::/0.
Write CIDR blocks in canonical form. If you enter 100.68.0.18/18, AWS stores it as 100.68.0.0/18. The stored rule is wider than a single host, so check the result when you expect a narrow match.
Writing your first rule from the CLI
This pair of commands creates a group in a VPC and allows HTTPS from any IPv4 address:
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/0Replace the VPC and group IDs with your own. create-security-group prints the new group ID, and you use that value in the second command.
The simple flags --protocol, --port and --cidr describe one rule. For a single port, --port 443 sets both ends of the range. They cannot express a description, and they are awkward for ranges. For those, use --ip-permissions, which takes a structure with IpProtocol, FromPort, ToPort and a list of sources:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=203.0.113.25/32,Description="Admin workstation"}]'This allows SSH from one administrator address. The description is stored on the rule, so a reviewer six months later can tell why port 22 is open.
To allow ping from inside your VPC, set FromPort to the ICMP type and ToPort to the code:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=icmp,FromPort=8,ToPort=-1,IpRanges=[{CidrIp=10.0.0.0/16,Description="Ping from VPC"}]'A source group instead of a CIDR
The same command can name a security group as the source. The --source-group flag replaces --cidr:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0This rule allows port 8080 only from interfaces that have the web group attached. The anatomy is unchanged: inbound, TCP, port 8080, with a group as the source.
Reading rules back
Use describe-security-groups to see the rules grouped under the group. Use describe-security-group-rules when you want one row per rule, with the rule IDs:
aws ec2 describe-security-group-rules \
--filters Name=group-id,Values=sg-0123456789abcdef0In the output, IsEgress is false for an inbound rule and true for an outbound rule. The fields IpProtocol, FromPort and ToPort hold the protocol and range. Exactly one of CidrIpv4, CidrIpv6, PrefixListId or ReferencedGroupInfo holds the source or destination. When you read output, find that field first, because it tells you what kind of rule you are looking at.
Changing and removing rules
A rule is identified by its exact contents. You cannot edit a rule's protocol or port in place with the simple commands. To remove a rule, give revoke-security-group-ingress the same protocol, port and source you used to create it:
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0If any value differs, the call does not match the rule and nothing is removed. Outbound rules use authorize-security-group-egress and revoke-security-group-egress, which take the same arguments.
To change an existing rule, modify-security-group-rules takes the rule ID and the new rule definition. It replaces the rule rather than patching it. Specify every field you want to keep, not only the one you are changing.
Defaults and common errors
A new group created in a VPC starts with no inbound rules and one outbound rule that allows all traffic to 0.0.0.0/0. The allow-only chapter explains what that means for traffic. When you write rules, remember that the outbound default exists. A group you create in Terraform with the aws_security_group resource has that default outbound rule removed, so it needs explicit outbound rules.
These errors come up most often:
InvalidPermission.Duplicate. A rule with the same protocol, port range and source already exists. Rundescribe-security-group-rulesand check. A rule that differs only in its description still counts as a duplicate, so edit the existing rule's description instead.InvalidParameterValueon the CIDR. The value is not valid CIDR notation, or it mixes IPv4 and IPv6 in a field that expects one.- The rule exists but traffic still fails. Check that the port range covers the destination port and that the rule is inbound. A frequent mistake is an IPv4 rule for a client that connects over IPv6.
- A revoke that appears to do nothing. The arguments did not match any rule exactly. Revoke by rule ID with
revoke-security-group-ingress --security-group-rule-ids, or copy the values from the describe output.
Exercise
You run aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 443 --cidr 0.0.0.0/0. IPv4 clients can reach HTTPS, but IPv6 clients time out. What is missing, and which value would you use?
Show answer
The rule covers only IPv4. Add a second inbound rule for IPv6 with the same protocol and port, using the source ::/0. IPv4 and IPv6 sources are separate values.
Exercise
A rule has protocol icmp, FromPort=8 and ToPort=-1. What traffic does it allow, and what do the two numbers mean?
Show answer
It allows ICMP echo requests (ping), with any code. For ICMP, FromPort holds the ICMP type (8 is echo request) and ToPort holds the code (-1 means all codes). They are not port numbers.
Exercise
You created a rule with --protocol tcp --port 8080 --cidr 10.0.0.0/16. You then run revoke with --port 8081 and the same other values. What happens, and how do you remove the rule?
Show answer
Nothing is removed, because the arguments do not match an existing rule exactly. Run revoke with --port 8080, or revoke by rule ID after finding it with describe-security-group-rules.