Reputation: 2652
Given this resource:
resource "google_compute_instance" "instance" {
...
network_interface {
...
access_config {
...
}
}
}
I'd like to conditionally define the access_config
property based on a condition.
resource "google_compute_instance" "instance" {
...
network_interface {
...
dynamic "access_config" {
for_each = var.condition ? [1] : []
content {
...
}
}
}
}
Upvotes: 3
Views: 4371
Reputation: 1039
Similar to what Josep Nadal
mentioned, but change count
to for_each
like
dynamic "access_config" {
for_each = var.conditional_on ? ["1"] : []
content {
nat_ip = null
}
}
Upvotes: 5
Reputation: 112
I am not really familiar, but I think you can accomplish this using the count parameter.
First you need to add a variable into your variables.tf file:
variable "conditional_on" {
description = "enable or disable"
type = bool
}
Then you can add the count parameter with the variable:
resource "google_compute_instance" "instance" {
...
network_interface {
...
dynamic "access_config" {
count = var.conditional_on ? 1 : 0
content {
...
}
}
}
}
There is more information on the Terraform Documentation: https://www.terraform.io/docs/configuration-0-11/interpolation.html#conditionals
Upvotes: -1