How to create valid CIDR block values for subnets in Terraform

Viewed 285

Hi I'm working with a Terraform project to allow my Lambda function to access the internet + an RDS DB. I've built everything manually in the console & it's working and now I'm in the process of making the same structure in Terraform.

I'm stuck on figuring out what to put for the cidr_blocks for my public & private subnets, as well as what to put for a private_ip for the EIP I'm creating. Again, I manually created valid values for those parameters by fiddling around in the console, but surely there's a programmatic way to do it as well ?

resource "aws_default_vpc" "jacobs_vpc_tf" {

}

resource "aws_subnet" "jacobs_public_subnet" {
  vpc_id     = aws_default_vpc.jacobs_vpc_tf.id
  cidr_block = aws_default_vpc.jacobs_vpc_tf.cidr_block # idk what to put here or how to make it automatically select a valid cidr block
  map_public_ip_on_launch = true

}

resource "aws_subnet" "jacobs_private_subnet" {
  vpc_id     = aws_default_vpc.jacobs_vpc_tf.id
  cidr_block = aws_default_vpc.jacobs_vpc_tf.cidr_block # idk what to put here or how to make it automatically select a valid cidr block


}
resource "aws_internet_gateway" "jacobs_gw" {
  vpc_id = aws_default_vpc.jacobs_vpc_tf.id

}

resource "aws_nat_gateway" "jacobs_nat_gw" {
  allocation_id = aws_eip.jacobs_eip.id
  subnet_id     = aws_subnet.jacobs_public_subnet.id


  depends_on = [aws_internet_gateway.jacobs_gw]
}

resource "aws_network_interface" "jacobs_network_interface" {
  subnet_id       = aws_subnet.jacobs_public_subnet.id
  private_ips     = ["10.0.0.50"] # idk what to put here or how to make it automatically select a valid IP

  attachment {
    instance     = aws_nat_gateway.jacobs_nat_gw.id
    device_index = 1
  }

}

resource "aws_eip" "jacobs_eip" {
  vpc = true
  network_interface = aws_network_interface.jacobs_network_interface.id
  depends_on                = [aws_internet_gateway.jacobs_gw]
}

### route tables & associations - these cidr block values should be correct and don't need to be changed

resource "aws_route_table" "jacobs_private_route_table" {
  vpc_id = aws_default_vpc.jacobs_vpc_tf.id
  nat_gateway_id = aws_nat_gateway.jacobs_nat_gw.id

  route = [
    {
      cidr_block = "0.0.0.0/0"
      gateway_id = aws_nat_gateway.jacobs_nat_gw.id
    }
  ]

}

resource "aws_route_table" "jacobs_public_route_table" {
  vpc_id = aws_default_vpc.jacobs_vpc_tf.id
  gateway_id = aws_internet_gateway.jacobs_gw.id

  route = [
    {
      cidr_block = "0.0.0.0/0"
      gateway_id = aws_internet_gateway.jacobs_gw.id
    }
  ]

}

resource "aws_route_table_association" "jacobs_private_route" {
  subnet_id      = aws_subnet.jacobs_private_subnet.id
  route_table_id = aws_route_table.jacobs_private_route_table.id


}

resource "aws_route_table_association" "jacobs_public_route" {
  subnet_id      = aws_subnet.jacobs_public_subnet.id
  route_table_id = aws_route_table.jacobs_public_route_table.id


}

I don't think i care what the values are for the cidr_blocks & private ip, I just need some valid value to put in there so I can get access. If anyone has a solution or can point me to some relevant resources I'd appreciate it!

1 Answers

I modified the code to create custom VPC (not to re-create a default VPC) with everything set automatically. For CIDR ranges you can use cidrsubnet:


resource "aws_vpc" "jacobs_vpc_tf" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "jacobs-vpc"
  }
}

resource "aws_subnet" "jacobs_public_subnet" {
  vpc_id     = aws_vpc.jacobs_vpc_tf.id
  cidr_block = cidrsubnet(aws_vpc.jacobs_vpc_tf.cidr_block, 8, 1)
  map_public_ip_on_launch = true
}

resource "aws_subnet" "jacobs_private_subnet" {
  vpc_id     = aws_vpc.jacobs_vpc_tf.id
  cidr_block = cidrsubnet(aws_vpc.jacobs_vpc_tf.cidr_block, 8, 2)
}

resource "aws_internet_gateway" "jacobs_gw" {
  vpc_id = aws_vpc.jacobs_vpc_tf.id
}


resource "aws_eip" "jacobs_eip" {
  vpc = true
  #network_interface = aws_network_interface.jacobs_network_interface.id
  depends_on        = [aws_internet_gateway.jacobs_gw]
}


resource "aws_nat_gateway" "jacobs_nat_gw" {
  allocation_id = aws_eip.jacobs_eip.id
  subnet_id     = aws_subnet.jacobs_public_subnet.id

  #depends_on = [aws_internet_gateway.jacobs_gw]
}

### route tables & associations - these cidr block values should be correct and don't need to be changed

resource "aws_route_table" "jacobs_private_route_table" {
  vpc_id = aws_vpc.jacobs_vpc_tf.id
  #nat_gateway_id = aws_nat_gateway.jacobs_nat_gw.id

  route  {
      cidr_block = "0.0.0.0/0"
      nat_gateway_id = aws_nat_gateway.jacobs_nat_gw.id
    }
}

resource "aws_route_table" "jacobs_public_route_table" {
  vpc_id = aws_vpc.jacobs_vpc_tf.id
  #gateway_id = aws_internet_gateway.jacobs_gw.id

  route  {
      cidr_block = "0.0.0.0/0"
      gateway_id = aws_internet_gateway.jacobs_gw.id
    }
}

resource "aws_route_table_association" "jacobs_private_route" {
  subnet_id      = aws_subnet.jacobs_private_subnet.id
  route_table_id = aws_route_table.jacobs_private_route_table.id


}

resource "aws_route_table_association" "jacobs_public_route" {
  subnet_id      = aws_subnet.jacobs_public_subnet.id
  route_table_id = aws_route_table.jacobs_public_route_table.id
}
Related