Sub topic 8
Referencing one security group from another
This chapter assumes you know CIDR notation, ports, and how to attach a security group to an EC2 instance. It also assumes you have read the earlier chapters on rule anatomy and stateful filtering.
Why reference a group instead of an IP range
An application tier often has many instances. Their private IP addresses change when instances are replaced or when Auto Scaling adds capacity. A rule based on a CIDR range must either be wide, which allows more than you intend, or be updated constantly.
A security group rule can instead name another security group as its source. The rule then means: allow traffic from any network interface that has that group attached. Membership is the condition, not the address. When a new instance joins the source group, it is allowed immediately. No rule changes.
How the reference works
Suppose the web tier uses sg-0123456789abcdef0. The application tier uses sg-0aaaaaaaaaaaaaaaa. You want the application tier to accept TCP 8080 only from the web tier.
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0The rule lives in the application group. The web group is only named as the source. The match is based on the private IP address of the traffic's source interface. A request from an instance in the web group is allowed. A request from an instance with no web group attached is not, even if it sits in the same subnet.
Things to know
- Return traffic. Security groups are stateful, so replies to an allowed connection need no extra rule.
- Direction. The reference only opens the direction where you wrote the rule. The web group does not gain access to the application group's other ports, and the application group gains no access to the web group.
- Private addresses only. The match uses private IP addresses. Traffic that reaches the target through a public IP address or an Elastic IP address is not matched by a group reference, unless the traffic stays on the private path.
- Same group as source. A group can reference itself. This lets members of a cluster talk to each other, for example on a node-to-node port, without opening it to anyone else.
- Other VPCs. You can reference a group in a peered VPC or one shared through a transit setup only where the service supports it. Peering supports it for VPCs in the same Region. Check the current documentation for your connection type.
- Dependencies. A group that another group references cannot be deleted until the rule is removed. Infrastructure as code tools handle this order for you, but manual cleanup may fail with a dependency error.
Common errors
- Using the instance's group ID in the wrong place. The
--group-idis the group that receives the rule. The--source-groupis the group that is allowed in. Swapping them opens the wrong tier. - Forgetting to attach the source group. The rule exists, but the client instance lacks the web group, so connections time out.
- Expecting a public-address connection to match. Test from the private address of the client.
- Referencing a group from a different VPC without peering. The rule cannot be created, or it does not match.
Exercise
The database group db-sg should accept TCP 5432 only from application instances that carry app-sg. Which group ID goes in --group-id, which goes in --source-group, and why does the rule keep working when Auto Scaling replaces application instances?
Show answer
--group-id is the ID of db-sg, because the rule is stored in the group that receives the traffic. --source-group is the ID of app-sg, the group allowed in. The rule matches on group membership, not on IP addresses, so any replacement instance with app-sg attached is allowed automatically.