Table of Contents

The Error

Error creating subnet: InvalidSubnet.Conflict: The CIDR block conflicts with another subnet

What Causes This

The CIDR block you specified overlaps with an existing subnet in the VPC. Subnet CIDRs within a VPC must not overlap. This is a common issue when adding subnets to an existing VPC.

How to Fix It

Solution 1: Check Existing Subnets

aws ec2 describe-subnets --filters "Name=vpc-id,Values=vpc-xxx" \
  --query 'Subnets[*].[SubnetId,CidrBlock,AvailabilityZone]' \
  --output table

Solution 2: Plan CIDR Blocks Carefully

# VPC: 10.0.0.0/16 (65,536 IPs)
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

# Public subnets:  10.0.0.0/24, 10.0.1.0/24, 10.0.2.0/24
# Private subnets: 10.0.10.0/24, 10.0.11.0/24, 10.0.12.0/24
# Database:        10.0.20.0/24, 10.0.21.0/24, 10.0.22.0/24

locals {
  public_subnets  = ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24"]
  private_subnets = ["10.0.10.0/24", "10.0.11.0/24", "10.0.12.0/24"]
  database_subnets = ["10.0.20.0/24", "10.0.21.0/24", "10.0.22.0/24"]
}

Solution 3: Use cidrsubnet Function

resource "aws_subnet" "public" {
  count             = 3
  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)
  availability_zone = data.aws_availability_zones.available.names[count.index]
  # Creates: 10.0.0.0/24, 10.0.1.0/24, 10.0.2.0/24
}

resource "aws_subnet" "private" {
  count             = 3
  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(aws_vpc.main.cidr_block, 8, count.index + 10)
  availability_zone = data.aws_availability_zones.available.names[count.index]
  # Creates: 10.0.10.0/24, 10.0.11.0/24, 10.0.12.0/24
}

Prevention Tips

  1. Pin provider versions — avoid surprise breaking changes
  2. Use CI/CD — catch errors before they hit production
  3. Test with terraform plan — always review before applying
  4. Keep Terraform updated — newer versions have better error messages
  5. Use terraform validate — catches syntax errors early

Hands-On Courses

Learn to avoid these errors with interactive, project-based courses:

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.