Sub topic 8
Referencing one security group from another
This chapter assumes you know CIDR notation, how a VPC and its subnets are laid out, and how to run AWS CLI commands. It also assumes you have read the earlier chapters on rule anatomy, allow-only rules and stateful filtering. Here you will learn how a rule can name another security group as its source. You will also learn what that reference does and does not match, and where it commonly goes wrong.
Why reference a group instead of an IP range
A rule normally names a source as a CIDR block, such as 10.0.1.0/24. That works while the set of clients is stable. In an Auto Scaling group or a container fleet, instances come and go, and their private IP addresses change. A CIDR rule must be wide enough to cover every address the clients might receive. That usually means the whole subnet, which also admits anything else placed in that subnet.
A security group reference solves this. Instead of an address range, the rule's source is the ID of another security group, for example sg-0123456789abcdef0. The rule then means: allow this traffic from any network interface that has that group attached. Membership is evaluated when traffic arrives. A new instance that gets the web group starts matching immediately. An instance that is terminated or has the group removed stops matching. You never edit the rule.
What the reference means
A reference is a statement about the sender's attachments. The rule matches when the source interface has the referenced group attached and the traffic arrives over the VPC's private addressing.
Three consequences follow:
- The rule does not copy the referenced group's rules. The web group's own inbound rules do not become part of the application group.
- The rule matches on the sender's private IP address. Traffic that reaches the target through a public IP or Elastic IP address does not match, because that traffic is not identified by the sender's group. To make a group reference work, clients should connect to the target's private address.
- The reference is directional. If the application group allows the web group on port 8080, the web group gains no right to be reached by the application tier.
The following example is taken from the topic's code samples. It allows TCP port 8080 into the application tier's group only from instances that have the web tier's group attached.
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0Here --group-id is the group that receives the rule, and --source-group is the group whose members are allowed in. Keeping those two roles straight prevents most mistakes with this feature.
Scope: which groups can be referenced
The group you name as a source must be reachable from the group's VPC. Valid references are:
- A group in the same VPC. This is the common case.
- A group in a peered VPC. You can reference it by its ID, and it can also include the owner account ID in cross-account setups.
- A group in a VPC shared with you through VPC sharing, where the participants are in the same organization's shared subnets.
A reference to a group in a peered VPC depends on the peering connection staying active. If the peering is deleted, the rule becomes stale. A stale rule remains in the group but no longer matches anything. You can list these with aws ec2 describe-stale-security-groups --vpc-id <vpc-id>. Clean them up so the rule set stays readable.
Cross-Region peering is a limit. Security group references do not work across Regions. In that case, use CIDR blocks.
Referencing yourself
A group can name itself as the source. The result is a group whose members can all talk to each other on the allowed ports, and no one else can talk to them. This is useful for clusters in which each node must reach its peers, such as a distributed cache or a database replica set.
aws ec2 authorize-security-group-ingress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--protocol tcp \
--port 6379 \
--source-group sg-0bbbbbbbbbbbbbbbbThe rule is added to sg-0bbbbbbbbbbbbbbbb and names the same ID as its source. Every instance with that group can now reach port 6379 on every other instance with it.
Note that the default security group already contains a self-reference that allows all inbound traffic from its own members. If you attach the default group to instances, those instances can reach each other on every port. The default group chapter covers this in detail. For your own groups, prefer an explicit, narrow self-reference.
Referencing in Terraform
In Terraform, the equivalent of --source-group is the referenced_security_group_id argument on a rule resource. Using one rule resource per rule keeps each rule independently changeable.
resource "aws_security_group" "web" {
name = "web-sg"
description = "Web tier"
vpc_id = var.vpc_id
}
resource "aws_security_group" "app" {
name = "app-sg"
description = "Application tier"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_ingress_rule" "app_from_web" {
security_group_id = aws_security_group.app.id
referenced_security_group_id = aws_security_group.web.id
ip_protocol = "tcp"
from_port = 8080
to_port = 8080
description = "App port from web tier"
}Because the rule refers to aws_security_group.web.id, Terraform creates the web group first and builds the dependency graph for you. A rule resource accepts one source type: a CIDR, a prefix list, or a referenced group. Setting more than one in the same resource is an error.
Referencing from the outbound side
The same mechanism works for egress rules. An outbound rule can name a group as its destination. For example, you can restrict the application tier so that it may only open connections to the database group on port 5432.
aws ec2 authorize-security-group-egress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--ip-permissions 'IpProtocol=tcp,FromPort=5432,ToPort=5432,UserIdGroupPairs=[{GroupId=sg-0cccccccccccccccc,Description="App to database"}]'A new security group starts with an outbound rule that allows all traffic to anywhere. If you add a narrow egress rule, the broad rule stays until you revoke it. Tightening egress therefore takes two steps: add the specific rules, then remove the allow-all rule. Do this in that order so you do not cut off traffic partway through.
How a referenced rule is evaluated
Recall that rules in all groups attached to an interface are combined, and that there is no ordering. A packet is allowed if any rule in any attached group matches. A referenced-group rule is just one more kind of match:
- The packet arrives at the target interface.
- AWS checks each inbound rule of each group attached to the target.
- For a group-reference rule, the check asks whether the sending interface has the named group attached, and whether protocol and port match.
- If any rule matches, the packet is allowed. Return traffic is allowed automatically because the connection is tracked.
- If no rule matches, the packet is dropped without a response.
Because the sender can carry several groups, adding a group to an instance can widen its reach. If someone attaches web-sg to an unrelated instance for convenience, that instance is now trusted by the application tier. Control who can modify group attachments, using IAM permissions on ModifyInstanceAttribute and ModifyNetworkInterfaceAttribute.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Connection times out although the rule names the right group | The client uses the target's public or Elastic IP address | Connect to the private IP or private DNS name |
| Rule created on the wrong group | `--group-id` and `--source-group` swapped | Revoke it and re-create it with the receiver as `--group-id` |
| `InvalidGroup.NotFound` when adding a rule | The referenced group is in a different VPC with no peering, or the ID is mistyped | Check the VPC and the ID |
| Rule shows as stale | The peering connection was deleted or the referenced group was removed | Remove it with `revoke-security-group-ingress` or recreate the peering |
| Terraform reports a cycle | Two groups reference each other inside inline rule blocks | Use separate rule resources, as in the example above |
| Traffic from an instance is refused although it has the group | The group was attached after a stale tracked state, or the instance uses another interface | Check which interface sends the traffic and which groups it carries |
The Terraform cycle deserves a note. If two groups must reference each other, inline ingress blocks in the group resources create a circular dependency: each group needs the other's ID before it exists. Separate rule resources break the cycle because both groups are created first and the rules are added afterward.
Quota effects
A rule that references a group counts as one rule, regardless of how many instances carry the group. That makes references much cheaper than listing many CIDR blocks. The number of groups per interface and the number of rules per group are limited, and the product of these two limits is bounded as well. Later in the tutorial, the quotas chapter explains how to plan for that.
Summary of decisions
Use a group reference when the clients are instances in your own VPC (or a peered one) that change over time, and when they can be identified by a role. Use a CIDR block when the clients are outside AWS, are in another Region, or reach the target through a public address. Use a prefix list when the same set of ranges is repeated across many rules.
Exercise
An application group app-sg has an inbound rule on TCP 8080 with source web-sg. A web instance with web-sg connects to the application instance's Elastic IP address and the connection times out. The application instance is healthy. What is the most likely cause, and what change fixes it?
Show answer
Traffic sent to an Elastic IP address is not identified as coming from a member of web-sg, so the group-reference rule does not match. Change the web instance to connect to the application instance's private IP address or private DNS name. Alternatively, add a CIDR rule for the web instance's public address, though that is less desirable.
Exercise
You want every node in a cache cluster to reach port 6379 on every other node, and nothing else to reach it. Which rule do you add, and to which group?
Show answer
Add an inbound rule to the cluster's own security group, with protocol TCP, port 6379 and the cluster's own group ID as the source (--source-group equal to --group-id).