Search code examples
amazon-web-servicesamazon-vpcsubnetcidr

CIDR blocks for AWS VPC and its subnets


I have a CIDR block of 172.30.0.0/21 and need to create 2 private subnets and then 2 public subnets for it.

At first I created: for private: ["172.30.8.0/21", "172.30.16.0/21"] for public: ["172.30.0.0/22", "172.30.8.0/22"]

but then I got the following errors:

Error: Error creating subnet: InvalidSubnet.Range: The CIDR '172.30.8.0/22' is invalid.
10:59:12    status code: 400, request id: 9***
10:59:12  
10:59:12    on .terraform/modules/vpc-us-west-2/main.tf line 335, in resource "aws_subnet" "public":
10:59:12   335: resource "aws_subnet" "public" {
10:59:12  
10:59:12  
10:59:12  
10:59:12  Error: Error creating subnet: InvalidSubnet.Range: The CIDR '172.30.16.0/21' is invalid.
10:59:12    status code: 400, request id: d**
10:59:12  
10:59:12    on .terraform/modules/vpc-us-west-2/main.tf line 363, in resource "aws_subnet" "private":
10:59:12   363: resource "aws_subnet" "private" {
10:59:12  
10:59:12  
10:59:12  
10:59:12  Error: Error creating subnet: InvalidSubnet.Range: The CIDR '172.30.8.0/21' is invalid.
10:59:12    status code: 400, request id: d**
10:59:12  
10:59:12    on .terraform/modules/vpc-us-west-2/main.tf line 363, in resource "aws_subnet" "private":
10:59:12   363: resource "aws_subnet" "private" {

So NOW:-

  • I created:

for private:["172.30.0.0/21", "172.30.4.0/21"] for public: ["172.30.0.0/22", "172.30.2.0/22"]

are they fine? will they work and fix the errors?

  • This is part of my code:
# List of private subnets to create in the environment, e.g. ["172.18.0.0/21", "172.18.8.0/21"]
variable "private_subnets-west-2" {
  type    = list(string)
  default = ["172.30.0.0/22", "172.30.6.0/23"]
}

# List of public subnets to create in the environment, e.g. ["172.18.168.0/22", "172.18.172.0/22"]
variable "public_subnets-west-2" {
  type    = list(string)
  default = ["172.30.0.0/22", "172.30.4.0/23"]
}

(the values currently in the subnets, I am not sure they work)

and these are the subnets I need to create. Again my CIDR is 172.30.0.0/21

Update: my private subnets work fine:

# List of private subnets to create in the environment, e.g. ["172.18.0.0/21", "172.18.8.0/21"]
variable "private_subnets-west-2" {
  type    = list(string)
  default = ["172.30.0.0/22", "172.30.6.0/23"]
}

However, I have been trying different subnets for my public one's since an hour now, and cannot find the proper ones:


# List of public subnets to create in the environment, e.g. ["172.18.168.0/22", "172.18.172.0/22"]
variable "public_subnets-west-2" {
  type    = list(string)
  default = ["172.30.0.64/26", "172.30.0.128/26"]
}

keep on getting errors for conflict or invalid:

Error: Error creating subnet: InvalidSubnet.Conflict: The CIDR '172.30.0.128/26' conflicts with another subnet
        status code: 400, request id:

  on .terraform/modules/vpc-us-west-2/main.tf line 335, in resource "aws_subnet" "public":
 335: resource "aws_subnet" "public" {

Error: Error creating subnet: InvalidSubnet.Conflict: The CIDR '172.30.0.64/26' conflicts with another subnet
        status code: 400, request id: 

  on .terraform/modules/vpc-us-west-2/main.tf line 335, in resource "aws_subnet" "public":
 335: resource "aws_subnet" "public" {

I have been stuck for long, if someone can help me and tell me exactly which 2 subnets I can use, it would be great help!


Solution

  • The 172.30.0.0/21 CIDR goes from 172.30.0.0 to 172.30.7.255.

    If you want to divide this equally into 4 subnets, you can use:

    • 172.30.0.0/23, which goes from 172.30.0.0 to 172.30.1.255
    • 172.30.2.0/23, which goes from 172.30.2.0 to 172.30.3.255
    • 172.30.4.0/23, which goes from 172.30.4.0 to 172.30.5.255
    • 172.30.6.0/23, which goes from 172.30.6.0 to 172.30.7.255

    @jordanm gave this link to a great CIDR calculator: http://jodies.de/ipcalc?host=172.30.0.0&mask1=21&mask2=23