Sub topic 2
Where Security Groups Apply: Network Interfaces and VPCs
This chapter explains what a security group is attached to and where its boundaries lie. It assumes you know basic TCP/IP (IP addresses, CIDR notation, ports), have launched EC2 instances, understand what a VPC and a subnet are, and can run AWS CLI commands.
Security groups attach to network interfaces
A common mental model says "a security group is attached to an instance." That is convenient, but it is not what AWS does. A security group is associated with an elastic network interface (ENI). An ENI is a virtual network card with a private IP address, a MAC address, and optionally a public or Elastic IP address.
When you launch an instance, EC2 creates a primary ENI (eth0) for it. The security groups you choose at launch go onto that ENI. The console and CLI show them at the instance level because the primary interface is created and deleted with the instance.
Many other AWS resources also use ENIs, and the same security group mechanism filters their traffic:
- Application and Network Load Balancers
- RDS database instances
- Lambda functions attached to a VPC
- Interface VPC endpoints
- EFS mount targets
- NAT gateways and ECS tasks that use the
awsvpcnetwork mode
So a single security group can protect EC2 instances, a database and a load balancer. The filtering happens per interface, not per host.
Multiple interfaces, separate groups
An instance can have more than one ENI, and the number depends on the instance type. Each ENI has its own set of security groups. This lets you split roles. For example, eth0 can face the internet with a web security group, while eth1 sits in a management subnet with a stricter one.
Two details matter in practice:
- A security group applies to the interface, so traffic to the address on
eth1is checked againsteth1's groups only. The groups oneth0play no part. - Each ENI can have up to five security groups by default (a quota you can raise). Rules from all of them are combined. Chapter-level detail on how they combine comes later in this tutorial.
You can list the groups on each interface of an instance:
aws ec2 describe-network-interfaces \
--filters Name=attachment.instance-id,Values=i-0123456789abcdef0 \
--query 'NetworkInterfaces[].{ENI:NetworkInterfaceId,Index:Attachment.DeviceIndex,Groups:Groups[].GroupId}'To change the groups on a specific interface, modify the interface directly:
aws ec2 modify-network-interface-attribute \
--network-interface-id eni-0123456789abcdef0 \
--groups sg-0123456789abcdef0 sg-0aaaaaaaaaaaaaaaaThe --groups list replaces the existing list. If you leave out a group that was attached, it is removed. Changes take effect on the running interface without a restart, and existing tracked connections are not cut off just because a rule changed (connection tracking is covered in a later chapter).
Security groups belong to one VPC
Every security group is created inside exactly one VPC. You supply --vpc-id when you create it, and if you omit it, AWS uses the default VPC in that Region. The VPC cannot be changed afterward.
This produces a firm boundary:
- You can attach a security group only to ENIs in the same VPC.
- Rules can refer to security groups in the same VPC. Rules can also refer to security groups in a peered VPC, or in a VPC shared through a transit gateway with security group referencing enabled, but the reference rules are more restricted than same-VPC ones.
- A security group is not a Regional or global object. A group in
us-east-1does not exist ineu-west-1, and a group in one VPC cannot be used in another VPC in the same Region.
Security groups are not tied to a subnet or an Availability Zone. A single group can cover instances in several subnets across several Availability Zones in its VPC. This is a key difference from network ACLs, which attach to subnets.
What a security group does not filter
Because filtering sits at the ENI, some traffic never reaches a security group check. AWS documents these exceptions, including:
- Requests to the Amazon DNS server at the VPC's
.2address and the link-local169.254.169.253. - Requests to the instance metadata service (
169.254.169.254). - DHCP traffic.
- Windows license activation traffic.
- The default VPC router address.
- Traffic between an instance and the Amazon Time Sync Service.
So you cannot block DNS lookups or metadata access to the Amazon-provided services with a security group. Use other controls for those, such as the instance metadata options for IMDS, or Route 53 Resolver DNS Firewall for DNS.
Also, a security group looks only at traffic crossing the interface. Traffic between two processes on the same instance (for example, over localhost) never leaves the host network stack and is not filtered.
Traffic between instances in the same group
Being in the same security group does not mean two instances can talk to each other. A new security group has no inbound rule that allows its own members. If you want members to communicate, you must add a rule that names the group itself as the source. That technique, along with references between groups, gets its own chapter.
Default VPC behavior and what happens on delete
If you launch an instance without naming a security group, EC2 attaches the VPC's default security group. Later chapters cover its rules. For now, remember that "no group chosen" does not mean "no group attached." An ENI always has at least one.
An ENI must always have at least one security group. If you try to remove them all, EC2 refuses, or it quietly assigns the default group in some workflows. Check the result instead of assuming.
You also cannot delete a security group that is in use. The deletion fails with a DependencyViolation error while:
- Any ENI still uses the group, including ENIs belonging to services such as load balancers or Lambda.
- Another security group's rule references it.
To find what is holding a group, query the ENIs:
aws ec2 describe-network-interfaces \
--filters Name=group-id,Values=sg-0123456789abcdef0 \
--query 'NetworkInterfaces[].{ENI:NetworkInterfaceId,Type:InterfaceType,Description:Description}'The Description field often names the service that owns the interface, which helps when a group is blocked by an ENI you did not create yourself.
Worked example: two interfaces, two groups
Suppose a bastion host has a public-facing interface and a private management interface. You want SSH only on the management side.
- Create
bastion-public-sgandbastion-mgmt-sgin the same VPC. - Attach
bastion-public-sgtoeth0, with a rule for the one service it should expose. - Attach
bastion-mgmt-sgtoeth1, with SSH allowed only from the management CIDR. - Verify with
describe-network-interfacesthat each interface lists only its own group.
An SSH attempt against the public address on eth0 fails, because bastion-public-sg has no port 22 rule. The same attempt against the private address on eth1 succeeds from the management network. Same instance, different results, because the check follows the interface.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Assuming a group works across VPCs | Attachment fails with an error about a different VPC | Create an equivalent group in the target VPC |
| Expecting a group in one Region to exist in another | Group ID not found | Create and manage groups per Region |
| Modifying an ENI with `--groups` and listing only the new group | Existing groups are removed | List every group you want to keep |
| Trying to block metadata or DNS with a security group | Traffic still flows | Use IMDS options or DNS Firewall |
| Deleting a group that load balancers or Lambda still use | `DependencyViolation` | Find ENIs by `group-id` and detach or delete the owner first |
Exercise
A security group sg-A is attached to the primary ENI of instance X. Instance X also has a secondary ENI, eth1, with only sg-B attached. A client connects to the private IP address of eth1 on port 443. Port 443 is allowed by sg-A but not by sg-B. Is the connection allowed, and why?
Show answer
No. The security group check happens on the interface that receives the traffic. The connection goes to eth1, whose only group is sg-B, and sg-B has no rule for port 443. The rule in sg-A applies only to eth0.
Exercise
You try to delete a security group and get a DependencyViolation error, but no EC2 instance appears to use it. Name two other things that could be holding it, and give the CLI approach to find the first one.
Show answer
An ENI owned by another service (such as a load balancer, RDS database or VPC-attached Lambda function), or a rule in another security group that references it. To find ENIs, run aws ec2 describe-network-interfaces --filters Name=group-id,Values=<group-id> and read the InterfaceType and Description fields.
Exercise
Can you attach a security group created in VPC A to an instance in VPC B in the same Region? Can one group cover instances in two Availability Zones of the same VPC?
Show answer
No to the first question, because a security group belongs to one VPC and can be attached only to ENIs in that VPC. Yes to the second, because security groups are not tied to subnets or Availability Zones.