RDS configuration uses a /0 subnet CIDR

Use valid RDS subnet address ranges and review routing and access controls together. A /0 prefix is not a supported AWS subnet size.

Description

0.0.0.0/0 and ::/0 can be route destinations, but neither is a valid subnet CIDR. Allocate subnets within the VPC. A direct route to an internet gateway makes even a privately addressed subnet public.

Potential impact

An invalid CIDR prevents deployment. Public addressing and broad routing rules can expose a database to unwanted connection attempts. Reading or changing data still requires authentication and privileges.

Remediation

  • Choose supported, nonoverlapping subnet CIDRs in the same VPC and meet the DB subnet group’s Availability Zone requirements.
  • For an internal instance, use subnets without direct internet-gateway routes, set publicly_accessible to false, and restrict network ACLs and security groups to required application and admin traffic.
  • Moving an existing instance to another subnet group can cause downtime. Review terraform plan for replacement and connection changes, prepare backups and recovery, schedule maintenance and test client connections.

Examples

Supply vpc_id, set az_a and az_b to different Availability Zones, and specify a supported db_instance_class and final snapshot name. Configure routes and security groups and protect the RDS-managed password and Terraform state.

Before

hcl
resource "aws_db_instance" "public_subnet_example" {
  allocated_storage    = 20
  engine               = "mysql"
  instance_class       = var.db_instance_class
  db_name              = "mydb"
  username             = "foo"
  manage_master_user_password = true
  skip_final_snapshot  = false
  final_snapshot_identifier = var.final_snapshot_identifier
  publicly_accessible  = false
  db_subnet_group_name = aws_db_subnet_group.subnetGroup.name
}

resource "aws_db_subnet_group" "subnetGroup" {
  name       = "main"
  subnet_ids = [aws_subnet.frontend.id, aws_subnet.backend.id]
}

resource "aws_subnet" "frontend" {
  availability_zone = var.az_a
  vpc_id     = var.vpc_id
  cidr_block = "10.0.1.0/24"
}

resource "aws_subnet" "backend" {
  availability_zone = var.az_b
  vpc_id     = var.vpc_id
  cidr_block = "0.0.0.0/0"
}

After

hcl
resource "aws_db_instance" "public_subnet_example" {
  allocated_storage    = 20
  engine               = "mysql"
  instance_class       = var.db_instance_class
  db_name              = "mydb"
  username             = "foo"
  manage_master_user_password = true
  skip_final_snapshot  = false
  final_snapshot_identifier = var.final_snapshot_identifier
  publicly_accessible  = false
  db_subnet_group_name = aws_db_subnet_group.subnetGroup.name
}

resource "aws_db_subnet_group" "subnetGroup" {
  name       = "main"
  subnet_ids = [aws_subnet.frontend.id, aws_subnet.backend.id]
}

resource "aws_subnet" "frontend" {
  availability_zone = var.az_a
  vpc_id     = var.vpc_id
  cidr_block = "10.0.1.0/24"
}

resource "aws_subnet" "backend" {
  availability_zone = var.az_b
  vpc_id     = var.vpc_id
  cidr_block = "10.0.2.0/24"
}

Explanation:

  • Before: The backend subnet cannot be created with a /0 range.
  • After: The two private address ranges must fit the VPC and be placed in different Availability Zones. Verify the resulting routes and access controls.

References