How to Create New Subnet Block Size in AWS VPC Using Terraform

amazon-web-servicesterraform

I want to use Terraform to create a new subnet for EKS. In the same account, the VPC has already been created and some subnets have been created.

locals {
  vpc_cidr_block = "10.148.52.0/22"

  public_subnets = [
    "10.148.52.0/27",
    "10.148.54.0/27",
  ]
  # ...
  private_subnets_3 = [
    "10.148.52.80/28",
    "10.148.54.80/28",
  ]
  subnets_4 = [
    "10.148.52.240/28",
    "10.148.54.240/28",
  ]
  eks_private_subnets = [
    "10.148.52.128/25",
    "10.148.54.128/25",
  ]
}

resource "aws_subnet" "eks_private" {
  count = length(local.eks_private_subnets)

  vpc_id            = aws_vpc.this.id
  cidr_block        = local.eks_private_subnets[count.index]
  availability_zone = local.azs[count.index]
}

When run the deployment, it got these errors:

Error: error creating subnet: InvalidSubnet.Conflict: The CIDR '10.148.54.128/25' conflicts with another subnet
    status code: 400, request id: 11111111111-111111-1111111-1111111111111

  on main.tf line 50, in resource "aws_subnet" "eks_private":
 50: resource "aws_subnet" "eks_private" {


Error: error creating subnet: InvalidSubnet.Conflict: The CIDR '10.148.52.128/25' conflicts with another subnet
    status code: 400, request id: 22222222222-222222-22222-222222222222222

  on network.tf line 50, in resource "aws_subnet" "eks_private":
 50: resource "aws_subnet" "eks_private" {

It seems the .128/25 size is conflicts with other subnet. But I want to create a /25 size subnet in this VPC, isn't it possible? Otherwise, may I need to create a new VPC to use?

Best Answer

To close this question off, the problem is you are trying to allocate the same CIDR range to two subnets. You need to allocate your subnet CIDR range out of the available range. Two tools you might find helpful

You might find this easier if you use more common CIDR block sizes, though that's not always possible. /16 VPC with /24 subnets are fairly common and easy to mentally work with.

Related Topic