Description
aws_default_security_group manages the VPC’s existing default group. On first adoption, Terraform removes its inbound and outbound rules before applying the rules you declare. Identify affected workloads before applying.
Potential impact
All resources attached to the group receive its permissions. Whether traffic reaches a service also depends on routing, addressing and the listening port.
Remediation
Move workloads to dedicated groups that allow their required traffic. Check each ingress source, egress destination, protocol and port against expected connections. Test new connections after applying. Replies to allowed connections are permitted automatically.
Examples
Supply aws_vpc.mainvpc and replace both CIDRs with approved ranges. 2001:db8:1234::/64 is reserved for documentation.
Before
resource "aws_default_security_group" "default_sg" {
vpc_id = aws_vpc.mainvpc.id
ingress {
protocol = -1
self = true
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
Ingress permits every protocol from all IPv4 addresses and from resources in the same group. self = true adds a source. No egress rule is declared.
After
resource "aws_default_security_group" "default_sg" {
vpc_id = aws_vpc.mainvpc.id
ingress {
protocol = -1
self = true
from_port = 0
to_port = 0
cidr_blocks = ["10.1.0.0/16"]
ipv6_cidr_blocks = ["2001:db8:1234::/64"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.1.0.0/16"]
ipv6_cidr_blocks = ["2001:db8:1234::/64"]
}
}
The second example narrows ingress to group members and the two CIDRs. It also adds egress to both CIDRs. Both directions still allow every protocol. Restrict service ports where possible.