terraformhashicorp

terraform only name in even numbers


I've been using terraform for a while but by no means am I experienced with code. I've been using the below block to generate descriptions for my hosts.

resource "vra_deployment" "test_instance" {
  count             = var.instance_count
  catalog_item_name = var.catalog_item_name
  description = "${var.hostname_prefix}${format("%02d", count.index+2)}-${replace(lower(var.region), "-","")}"

This will give me a host with a description like host02-region. At the moment I've only used this with the count variable set to 1.

I'd like to make it so that if I set the count variable to 2, it will build 2 hosts with the description only in even numbers, ie host02-region and host04-region.

Would anyone be able to show me how to do this please?


Solution

  • This is just a basic math question. I would change count.index+2 to (count.index + 1) * 2.

    (0 + 1) * 2 = 2
    (1 + 1) * 2 = 4
    (2 + 1) * 2 = 6
    (3 + 1) * 2 = 8