Table of Contents
The Error
Error creating DB Instance: DBSubnetGroupNotFoundFault: DBSubnetGroup 'X' not found
What Causes This
The DB subnet group referenced in your RDS configuration doesn’t exist or hasn’t been created yet. RDS instances in a VPC require a DB subnet group containing subnets in at least two different availability zones.
How to Fix It
Solution 1: Create the DB Subnet Group
resource "aws_db_subnet_group" "main" {
name = "main-db-subnet-group"
subnet_ids = [
aws_subnet.private_a.id,
aws_subnet.private_b.id, # Must be in different AZs!
]
tags = { Name = "Main DB subnet group" }
}
resource "aws_db_instance" "main" {
identifier = "production-db"
engine = "postgres"
engine_version = "15.4"
instance_class = "db.t3.micro"
allocated_storage = 20
db_subnet_group_name = aws_db_subnet_group.main.name # Reference here
vpc_security_group_ids = [aws_security_group.db.id]
skip_final_snapshot = true
}
Solution 2: Ensure Multiple AZs
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index + 10}.0/24"
availability_zone = data.aws_availability_zones.available.names[count.index]
}
resource "aws_db_subnet_group" "main" {
name = "main"
subnet_ids = aws_subnet.private[*].id
}
Prevention Tips
- Pin provider versions — avoid surprise breaking changes
- Use CI/CD — catch errors before they hit production
- Test with
terraform plan— always review before applying - Keep Terraform updated — newer versions have better error messages
- Use
terraform validate— catches syntax errors early
Hands-On Courses
Learn to avoid these errors with interactive, project-based courses:
- Terraform for Beginners on CopyPasteLearn
- Terraform By Example — practical code examples
- Terraform Cheat Sheet — quick reference for all commands
Related Articles
- Terraform Troubleshooting - Common Errors and Solutions
- Terraform Enabling and Using Debugging
- Debugging with TFLint
Conclusion
This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.

