How to generate cidr subnet array

65 views
Skip to first unread message

mic...@cyndx.net

unread,
Apr 24, 2018, 5:38:11 PM4/24/18
to Terraform
Hello,

I have:

variable "cidr_base" {

  description = "First 2 octets of the /21 network."

  default     = "10.10"

}


variable "cidr_block" {

  description = "Which block of /21 to use. cidr = [cidr_base].[cidr_block * 8].0/21"

  default     = 1

}


I'm able to use:


resource "aws_vpc" "default" {

  cidr_block = "${var.cidr_base}.${var.cidr_block * 8}.0/21"

...


This works great.


I have:


variable "cidr_blocks_pub" {

  description = "The CIDR blocks for the public subnets."

  default     = ["10.10.8.0/24", "10.10.9.0/24", "10.10.10.0/24"]

}


and 


resource "aws_subnet" "default-pub" {

  count                   = "${length(var.cidr_blocks_pub)}"

  cidr_block              = "${var.cidr_blocks_pub[count.index]}"


How can I modify this to use the cidr_base and cidr_block vars but still use count? Will locals work for this?


Thanks!


mic...@cyndx.net

unread,
Apr 24, 2018, 7:14:46 PM4/24/18
to Terraform


Locals worked:

 locals {

  cidr_blocks_pub = ["${var.cidr_base}.${var.cidr_block * 8}.0/24", "${var.cidr_base}.${var.cidr_block * 8 + 1}.0/24", "${var.cidr_base}.${var.cidr_block * 8 + 2}.0/24"]

  cidr_blocks_prv = ["${var.cidr_base}.${var.cidr_block * 8 + 4}.0/24", "${var.cidr_base}.${var.cidr_block * 8 + 5}.0/24", "${var.cidr_base}.${var.cidr_block * 8 + 6}.0/24"]

}

and then I replaced var. with local. (var.
cidr_blocks_pub -> local.cidr_blocks_pub , etc)

Reply all
Reply to author
Forward
0 new messages