Sub topic 2
Where security groups apply: network interfaces and VPCs
This chapter assumes you know TCP/IP basics (IP addresses, CIDR, ports), have launched EC2 instances, and understand what a VPC and a subnet are. You should also be able to run AWS CLI commands.
A security group belongs to a VPC
A security group is created inside one VPC and can only be used there. You can't attach a security group from one VPC to an instance in another VPC. The --vpc-id argument on create-security-group sets this scope, and it can't be changed afterward.
Security groups are not tied to a subnet or an Availability Zone. One group can cover instances in several subnets and zones of the same VPC. This differs from a network ACL, which is associated with subnets.
A security group attaches to a network interface
The firewall is applied to an elastic network interface (ENI). An ENI is a virtual network card with a private IP address, and it is what connects a resource to the VPC. When you launch an EC2 instance, AWS creates its primary network interface (eth0) and attaches your chosen security groups to it.
This has several consequences:
- Rules filter traffic at the interface, before it reaches the operating system.
- An instance with two network interfaces can use different security groups on each one.
- Many AWS services create ENIs in your VPC, including load balancers, RDS databases, Lambda functions in a VPC, and VPC endpoints. You assign security groups to these resources the same way.
- Changing the groups on an instance really changes the groups on its network interface.
Several groups per interface
You can attach more than one security group to an interface. AWS combines all their rules, and a later chapter covers exactly how. For now, remember that the groups are a set. They have no order, and you attach or remove them as a whole list.
Quotas apply here. By default, an interface can have up to 5 security groups. A later chapter on quotas covers the exact limits.
Viewing and changing attachments
To see which groups are attached to an interface, query the interface:
aws ec2 describe-network-interfaces \
--network-interface-ids eni-0123456789abcdef0 \
--query 'NetworkInterfaces[].Groups'To see which groups an instance uses, look at its interfaces:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[].Instances[].NetworkInterfaces[].[NetworkInterfaceId,Groups[].GroupId]'To replace the groups on an existing interface, pass the complete new list:
aws ec2 modify-network-interface-attribute \
--network-interface-id eni-0123456789abcdef0 \
--groups sg-0123456789abcdef0 sg-0aaaaaaaaaaaaaaaaThe --groups value replaces the whole set. Any group you leave out is detached. The change applies immediately, with no restart, and the groups must belong to the same VPC as the interface.
Common errors
InvalidGroup.NotFoundor a VPC mismatch error when attaching. The group is in a different VPC from the interface. Check the group'sVpcId.- A group name works in one place but not another. Names are unique only within a VPC. In a non-default VPC, use group IDs (
sg-...) instead of names to avoid ambiguity. - Rules appear to have no effect on a service. Confirm you attached the group to the correct interface. A load balancer's interface and an instance's interface have separate groups.
Exercise
An instance has two network interfaces in different subnets of the same VPC. Can the two interfaces use different security groups? Can one of them use a security group created in a different VPC?
Show answer
Yes to the first. Security groups attach per network interface, so each interface can have its own set of groups. No to the second. A security group is scoped to the VPC where it was created and can only be attached to interfaces in that VPC.