Sub topic 3
Reading and writing a rule
This chapter assumes you know CIDR notation, TCP and UDP ports, and how to run AWS CLI commands. It also assumes you know what a security group is and that it attaches to network interfaces. Here you will learn to read each field of a rule and write rules from the CLI.
The parts of a rule
Every security group rule is built from the same fields. Read them left to right as a sentence: "allow this protocol, on these ports, to or from this peer."
| Field | Meaning | Example |
|---|---|---|
| Direction | Inbound rules match traffic arriving at the interface. Outbound rules match traffic leaving it. | Inbound |
| Protocol | `tcp`, `udp`, `icmp`, `icmpv6`, a protocol number, or `-1` for all protocols | `tcp` |
| Port range | A start and end port. For ICMP, these fields hold the type and code. | 443 to 443 |
| Source or destination | Where the traffic comes from (inbound) or goes to (outbound) | `0.0.0.0/0` |
| Description | Optional text that records why the rule exists | `HTTPS from anywhere` |
The peer is a CIDR block, a prefix list, or another security group. The next chapters cover choosing among them. For now, remember that inbound rules use a source and outbound rules use a destination.
Writing a rule with the CLI
First create the group inside a VPC. Then add one inbound rule that 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/0--port 443 sets both ends of the range to 443. To open a range, write --port 8000-8100. The /0 in 0.0.0.0/0 means every IPv4 address. That is appropriate for a public web server on 443 and risky for almost anything else.
Adding a description
The simple flags cannot set a description. Use --ip-permissions when you want one. This rule allows SSH from a single administrator address, written as a /32 so it matches exactly one IP.
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"}]'Here you must give FromPort and ToPort yourself. A description saves time later, when someone asks why port 22 is open.
Reading and removing a rule
List a group's rules, then remove one by repeating the values it was created with.
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0In the describe-security-groups output, inbound rules are under IpPermissions and outbound rules under IpPermissionsEgress.
Common mistakes
- Using
--cidr 203.0.113.25without/32. The CLI rejects it as a malformed CIDR. - Confusing the group ID with the group name. The
--group-idoption takes thesg-identifier. - Opening
0.0.0.0/0on port 22 or 3389 for convenience. Use a narrow CIDR instead. - Forgetting that
-1as the protocol opens all ports, regardless of any port values you give.
Exercise
You want to allow UDP port 53 into a group from the network 10.0.0.0/16, and you want the rule to carry the description "Internal DNS". Which CLI option must you use, and why can't you use --protocol, --port and --cidr alone?
Show answer
Use --ip-permissions, for example 'IpProtocol=udp,FromPort=53,ToPort=53,IpRanges=[{CidrIp=10.0.0.0/16,Description="Internal DNS"}]'. The simple --protocol, --port and --cidr flags have no way to set a description.