Terraform 0.12.0 conditional blocks

1,165 views
Skip to first unread message

Andrey Tuzhilin

unread,
May 31, 2019, 11:22:45 AM5/31/19
to Terraform
Hi!

With terraform out it is now easy to make dynamic blocks. But what if I want to make a conditional block. E.g.: I have a module with bool variable:

variable "ext_ip" {
default = false
type = bool
description = "assign external ip to the instance"
}

Now I need to add an empty block to resource config depending on its value:

resource "google_compute_instance" "vm" {
network_interface {
    access_config {} // <- this block is optional
  }
}

The I came out with two ways of doing (both are ugly as hell):

resource "google_compute_instance" "vm" {
network_interface {
dynamic "access_config" {
for_each = var.ext_ip ? [""] : []
content {}
}
}
}

resource "google_compute_instance" "vm" {
network_interface {
dynamic "access_config" {
for_each = [""]
content {}
      if var.ext_ip
    }
}
}

Is there any other non-ugly way?

Stuart Clark

unread,
May 31, 2019, 3:03:56 PM5/31/19
to terrafo...@googlegroups.com, Andrey Tuzhilin
On 31/05/2019 16:22, Andrey Tuzhilin wrote:
Hi!

With terraform out it is now easy to make dynamic blocks. But what if I want to make a conditional block. E.g.: I have a module with bool variable:

variable "ext_ip" {
  default = false
  type    = bool
  description = "assign external ip to the instance"
}

          
Now I need to add an empty block to resource config depending on its value:

          
resource "google_compute_instance" "vm" {
  network_interface {
    access_config {} // <- this block is optional
  }
}
The I came out with two ways of doing (both are ugly as hell):
resource "google_compute_instance" "vm" {
  network_interface {
    dynamic "access_config" {
      for_each = var.ext_ip ? [""] : []
      content {}
    }
  }
}


I posted something similar a few days ago as did someone else. This one (above) is the one we've gone with



resource "google_compute_instance" "vm" {
  network_interface {
    dynamic "access_config" {
      for_each = [""]
      content {}
      if var.ext_ip
    }
  }
}
Is there any other non-ugly way?
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/ec18a5bd-b095-49a2-9c68-b26824f2285d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Stuart Clark
Reply all
Reply to author
Forward
0 new messages