Sub topic 9
The default security group and its defaults
This chapter assumes you know how security group rules are built and evaluated from the earlier chapters. That covers allow-only rules, statefulness, and the idea that a rule's source can be a CIDR block or another security group. Here you will see what AWS creates for you in every VPC, why its defaults can surprise you, and how to make it harmless.
What the default security group is
Every VPC has one security group named default. AWS creates it when the VPC is created, whether you made the VPC yourself or used the default VPC that comes with each Region. You do not ask for it, and you cannot remove it.
Three properties matter:
- It is a normal security group with an ID such as
sg-0abc.... All the rule mechanics you already know apply to it. - It is scoped to one VPC. Each VPC has its own
defaultgroup with a different ID, so "default" is a name, not one global object. - You cannot delete it or rename it. You can edit its rules, including removing all of them. It disappears only when its VPC is deleted.
The default rules
A new default security group ships with these rules:
| Direction | Protocol and ports | Source or destination | Effect |
|---|---|---|---|
| Inbound | All protocols, all ports | The default group itself | Members can send anything to each other |
| Outbound | All protocols, all ports | `0.0.0.0/0` | Members can reach any IPv4 address |
Depending on how the VPC was created, you may also see an outbound rule for ::/0 when IPv6 is in use. Check the actual rules instead of assuming (an example command appears below).
The inbound rule is a security group reference, the pattern from the previous chapter, and it points at the group itself. It does not mean "allow from anywhere". An instance outside the group, including one on the internet, matches no inbound rule and is denied by the implicit deny.
The outbound rule is permissive. Because the group is stateful, replies to allowed connections need no extra rules.
When the default group gets attached
The default group is attached whenever you do not choose another one. Typical cases:
- You launch an instance with the CLI or SDK and give no security group. EC2 attaches the VPC's default group.
- You create a network interface or another resource that needs a security group and do not specify one. Services that create network interfaces for you, such as load balancers, database instances, or Lambda functions in a VPC, may also fall back to it in some configurations.
- You remove every other group from an interface. An interface must always have at least one group, so EC2 does not leave it empty. Where the group list is replaced, check what remains attached.
The console launch wizard behaves differently. It usually proposes a new group, often named launch-wizard-N, so people who only use the console may never notice the default group in use.
Why the defaults deserve attention
The defaults are not dangerous from the internet side, but they have two weaknesses.
- Flat trust inside the group. Any instance in the default group can reach every other instance in it on every port. If three unrelated workloads end up there, a compromise of the weakest one exposes the others. This undoes the tiered design you built with group references, where each tier opens only the ports it needs to the tier before it.
- Unrestricted egress. A compromised instance can send data anywhere. This is also true of any group you create without touching egress, but the default group makes it the starting state.
The first weakness is the one people miss. Membership in the group is the only credential, and membership is easy to grant by accident, for example by leaving an instance on the default setting.
Inspecting the default group
Find the default group for a VPC by filtering on its name and VPC ID:
aws ec2 describe-security-groups \
--filters Name=vpc-id,Values=vpc-0123456789abcdef0 Name=group-name,Values=default \
--query 'SecurityGroups[0].GroupId' \
--output textList its rules with their rule IDs, which you will need for removal:
aws ec2 describe-security-group-rules \
--filters Name=group-id,Values=sg-0abc1234567890def \
--query 'SecurityGroupRules[].{Id:SecurityGroupRuleId,Egress:IsEgress,Proto:IpProtocol,Cidr:CidrIpv4,Ref:ReferencedGroupInfo.GroupId}' \
--output tableIn the output, a row with Egress false and Ref equal to the group's own ID is the self-referencing inbound rule. A row with Egress true and Cidr of 0.0.0.0/0 is the outbound rule. Protocol -1 means all protocols.
To find which interfaces use the group, filter network interfaces by group ID:
aws ec2 describe-network-interfaces \
--filters Name=group-id,Values=sg-0abc1234567890def \
--query 'NetworkInterfaces[].{Eni:NetworkInterfaceId,Desc:Description,Ip:PrivateIpAddress}' \
--output tableRun this before you change the rules. An empty result means nothing depends on the group today. A non-empty result lists the resources whose connectivity will change.
Locking down the default group
A common hardening step is to leave the default group in place but remove all its rules. An interface with only that group attached then has no inbound or outbound allowances, so a resource that lands there by accident cannot talk to anything. Mistakes become visible as failures, not as silent over-exposure.
Revoke both rules by rule ID:
aws ec2 revoke-security-group-ingress \
--group-id sg-0abc1234567890def \
--security-group-rule-ids sgr-0aaaaaaaaaaaaaaaa
aws ec2 revoke-security-group-egress \
--group-id sg-0abc1234567890def \
--security-group-rule-ids sgr-0bbbbbbbbbbbbbbbbReplace the IDs with the ones from describe-security-group-rules. After this, describe-security-groups shows empty IpPermissions and IpPermissionsEgress for the group.
Doing it in Terraform
Terraform normally creates new resources, but the default group already exists. The aws_default_security_group resource adopts it instead of creating one. Declaring it with no rule blocks tells Terraform to remove all rules.
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_default_security_group" "default" {
vpc_id = aws_vpc.main.id
# No ingress or egress blocks: Terraform strips all rules
# from the VPC's default group and keeps it empty.
}This has two advantages over a one-time CLI cleanup. New VPCs created by the same configuration get the same treatment, and any rule someone adds to the default group by hand is removed on the next apply. If you later add rules to it by using aws_vpc_security_group_ingress_rule resources that target the default group, Terraform will fight with the adopting resource. Pick one approach per group.
Detecting drift and enforcing the policy
An empty default group is a convention, so it can drift. AWS publishes a managed check for it. AWS Config has a rule, vpc-default-security-group-closed, that flags default groups with any rules. Security Hub has an equivalent control. Use one of them in each account and Region where you have VPCs, because each VPC, including the default VPC in Regions you never use, has its own default group.
Edge cases
- Default VPC. The default VPC in each Region also has a default security group with the standard rules. Instances launched there without a group inherit it. If you do not use the default VPC, either delete the VPC or empty its default group.
- Removing the last group. You cannot leave a network interface with no groups. If you detach the final group, EC2 attaches the default group in its place. This can quietly re-open the self-referencing rule if you have not emptied it.
- Cross-group references. Rules in other groups may reference the default group by ID. Emptying the default group does not remove those references, but traffic from default-group members to those resources still depends on the other group's rules, not on the default group's own rules.
- Peered VPCs. A rule that names a security group in a peered VPC refers to that VPC's own group. Do not assume the default group in one VPC is the same as the default group in another.
- Quotas. The default group counts toward your per-VPC group quota like any other group.
Quick reference
| Question | Answer |
|---|---|
| How many default groups per VPC? | Exactly one |
| Can it be deleted? | No, only by deleting the VPC |
| Can it be renamed? | No |
| Can its rules be changed? | Yes, including removing all of them |
| Who can reach members from outside the group? | No one, unless you add a rule |
| What is the default outbound behavior? | All traffic allowed |
Exercise
An instance is launched with the AWS CLI and no security group is specified. The VPC's default group still has its original rules. Another instance in the same VPC, attached only to a custom group, tries to connect to port 22 on the first instance. Does the connection succeed? What changes if you add the custom group to the first instance as a second group?
Show answer
The connection fails. The default group's only inbound rule allows traffic from members of the default group itself, and the second instance is not a member, so the implicit deny applies. If you attach the custom group to the first instance as well, the rules of both groups are combined. The connection then succeeds only if the custom group has an inbound rule that permits port 22 from that source, such as a self-reference to the custom group. The default group's self-reference alone would not match, because the second instance does not carry the default group.
Exercise
You want to ensure that any resource accidentally left in the default group cannot send or receive traffic. List the steps to do this with the CLI, and name the one check you should run first.
Show answer
First, run describe-network-interfaces with a group-id filter to see which resources use the default group, so you know what will lose connectivity. Then run describe-security-group-rules to get the rule IDs, and run revoke-security-group-ingress and revoke-security-group-egress with --security-group-rule-ids for the self-referencing inbound rule and the 0.0.0.0/0 outbound rule (plus any IPv6 egress rule). Finally, run describe-security-groups to confirm that IpPermissions and IpPermissionsEgress are empty.