1
votes

I'm sure this is a quick one..

I'm trying to run through a resource creation with count, and name the resource accordingly. Here I have two problems.. one that I'm unsure if can be fixed, so I am guessing I'll have to use the following work around.

This is my instance creation, one for a disk, and another for a compute instance:

resource "google_compute_disk" "us-am-build" {
  count = "${var.us_am_count}"
  name  = "am${count.index}-disk"
  type  = "pd-standard"
  size  = "200"
  zone  = "us-east1-b"
}
resource "google_compute_instance" "us-am-build" {
  count                     = "${var.us_am_count}"
  project                   = "${var.gcp_project}"
  name                      = "am${count.index}"
  machine_type              = "n1-standard-1"
  zone                      = "us-east1-b"
  allow_stopping_for_update = "true"

  tags = "${var.am_instance_tags}"

  service_account {
    email = "${var.service_account}"
    scopes = [ "cloud-platform" ]
  }

  boot_disk {
    initialize_params {
      image = "${var.compute_image}"
    }
  }
  attached_disk {
    source      = "${google_compute_disk.us-am-build.self_link}"
    device_name = "${google_compute_disk.us-am-build.name}-1"
  }
  network_interface {
    subnetwork         = "${var.us-east-1-subnet}"
    subnetwork_project = "${var.gcp_project}"
    access_config      = {}
  }
  metadata {
    certname        = "am0-us.forgerock.com"
    shutdown-script = "${file("${path.module}/scripts/node_termination_publish.sh")}"
    startup-script  = "${file("${path.module}/scripts/startup.sh")}"
  }
}

The first issue is that I wanted to loop through a count of 4, 2 of which would be in us-east and the other 2 would be in eu-west. I couldn't think of a way to be able to loop through and have them in different regions.

The other problem is with the above demonstration, and the name of the instances between the disk and the compute instance.

count = "${var.us_am_count}"

Let's say the above is set to 2 (for US).

name  = "am${count.index}-disk"

Would the name of the first disk be am0-disk?

The error I'm getting it:

   * module.am-deploy.google_compute_instance.us-am-build[1]: Resource 'google_compute_disk.us-am-build' not found for variable 'google_compute_disk.us-am-build.self_link'
* module.am-deploy.google_compute_instance.us-am-build[0]: Resource 'google_compute_disk.us-am-build' not found for variable 'google_compute_disk.us-am-build.self_link'
1

1 Answers

2
votes

First issue, changing the zone attribute based off of a count. I see a couple of options (of which 1 sounds best to me):

1) Make this chunk of code a module with inputs of count and region. Instantiate 2 modules, one for each region, and count of 2 (either hard-coded or passed in).

2) Create a local with the number of regions, then use the mod operator to select which region mod the number of region.

locals {
  regions = ["us-east1-b", "eu-west-1b"]
}

zone = "${element{local.regions, count.index % length(local.regions)}"

3) Make a local with the region you want for each count (This is janky, you probably don't want to do this):

locals {
  region_per_count = ["us-east1-b", "us-east-1b", "eu-west-1b", "eu-west-1b"]
}

Then select out of this list

zone = "${element(local.region_per_count, count.index")

Second issue is caused because you're not picking out the particular google_compute_disk.us-am-build to get the self_link attribute off of. Do something like this:

source      = "${element(google_compute_disk.us-am-build.*.self_link, count.index)}"

The .*. puts that group of resources into list context, then you select out which resource with element.