EC2-Classic DB security group allows all IPv4 addresses

A DB security group allowing all IPv4 sources can admit unnecessary connection attempts. Use supported VPC security groups to permit only required sources and database ports.

Description

0.0.0.0/0 admits every IPv4 source that can reach the database port. For RDS in a VPC, use a VPC security group. Terraform AWS provider 5.0 removed the EC2-Classic aws_db_security_group resource.

Potential impact

A reachable port allows unwanted login attempts. Weak credentials or excessive database privileges can then expose data or allow unauthorized changes.

Remediation

  • Allow the database port from approved application security groups or the client CIDRs used by your applications and administrators. Check private ranges for unrelated hosts.
  • Set aws_db_instance.vpc_security_group_ids to include the new group. Review creation, deletion and dependencies in terraform plan. Check the deployed attachments, routes and other groups for wider access. Test allowed and blocked connections, and verify database authentication separately.

Examples

The second excerpt requires var.vpc_id and var.db_port. Replace 10.0.0.0/25 with the approved client range.

Before

hcl
resource "aws_db_security_group" "public_db_sg" {
  name = "rds_sg"

  ingress {
    cidr = "0.0.0.0/0"
  }
}

After

hcl
resource "aws_security_group" "db" {
  name   = "rds_sg"
  vpc_id = var.vpc_id
}

resource "aws_vpc_security_group_ingress_rule" "db_client" {
  security_group_id = aws_security_group.db.id
  cidr_ipv4         = "10.0.0.0/25"
  ip_protocol       = "tcp"
  from_port         = var.db_port
  to_port           = var.db_port
}

The excerpt creates the group and its TCP rule. The RDS instance association must be configured separately.

References