3

I am trying to create a terraform module that creates a compute instance. I want the resource to have an attached disk if and only if I have a variable attached_disk_enabled set to true during module invocation. I have this:

resource "google_compute_disk" "my-disk" {
  name  = "data"
  type  = "pd-ssd"
  size  = 20
  count = var.attached_disks_enabled ? 1 : 0
}

resource "google_compute_instance" "computer" {
  name = "computer"

  boot_disk {
    ...
  }

  // How do I make this disappear if attached_disk_enabled == false?
  attached_disk {
    source      = "${google_compute_disk.my-disk.self_link}"
    device_name = "computer-disk"
    mode        = "READ_WRITE"
  }
}

Variables have been declared for the module in vars.tf. Module invocation is like this:

module "main" {
  source                = "../modules/computer"
  attached_disk_enabled = false
  ...
}

I know about dynamic blocks and how to use for loop to iterate over a list and set multiple blocks, but I'm not sure how to exclude a block from a resource using this method:

dynamic "attached-disk" {
  for_each in var.disk_list
  content {
    source      = "${google_compute_disk.my-disk.*.self_link}"
    device_name = "computer-disk-${count.index}"
    mode        = "READ_WRITE"
  }
}

I want if in place of for_each. Is there a way to do this?

$ terraform version
Terraform v0.12.0 

1 Answer 1

4

Because your disk resource already has the conditional attached to it, you can use the result of that resource as your iterator and thus avoid specifying the conditional again:

dynamic "attached_disk" {
  for_each = google_compute_disk.my-disk
  content {
    source      = attached_disk.value.self_link
    device_name = "computer-disk-${attached_disk.key}"
    mode        = "READ_WRITE"
  }
}

To answer the general question: if you do need a conditional block, the answer is to write a conditional expression that returns either a single-item list or an empty list:

dynamic "attached_disk" {
  for_each = var.attached_disk_enabled ? [google_compute_disk.my-disk[0].self_link] : []
  content {
    source      = attached_disk.value
    device_name = "computer-disk-${attached_disk.key}"
    mode        = "READ_WRITE"
  }
}

However, in your specific situation I'd prefer the former because it describes the intent ("attach each of the disks") more directly.

Sign up to request clarification or add additional context in comments.

4 Comments

Any idea how it can be done in terraform 0.11.6? I don't think it has for_each ? Any help would be much appreciated.
Terraform v0.11 is obsolete and you should not expect to be able to do anything non-trivial with it.
I know it's obsolete but i am stuck to implement something like this in 0.11, Any clue how this could be done in that?
Terraform v0.11 designs, if they are possible at all, will typically be very different than what would be done in modern Terraform, and so not really practical for me to answer in a comment here. I would suggest starting a new top-level question on Stack Overflow and tagging it with terraform0.11 so that folks who know the older versions of Terraform can find it and hopefully help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.