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_accessibletofalse, 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 planfor 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
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
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
backendsubnet cannot be created with a/0range. - After: The two private address ranges must fit the VPC and be placed in different Availability Zones. Verify the resulting routes and access controls.