-
Managing Conditional Creation of Terraform Resources Based on .tfvars Variables
In my Terraform project, I have defined resources in .tf files that are shared across multiple applications. To configure these resources, I use a .tfvars file where I specify various field values. Now, I need to conditionally exclude certain resources based on variables defined in the .tfvars file.
For instance, consider the following resource block:
resource "cloudflare_record" "record" { zone_id = "${data.cloudflare_zones.domain.zones[0].id}" name = "${var.subdomain}" value = "${var.origin_server}" type = "CNAME" ttl = 1 proxied = true }
However, if I declare
cloudflare = false
in my .tfvars file, I want to be able to exclude this entire resource, like so:if var.cloudflare { resource "cloudflare_record" "record" { zone_id = "${data.cloudflare_zones.domain.zones[0].id}" name = "${var.subdomain}" value = "${var.origin_server}" type = "CNAME" ttl = 1 proxied = true } }
I’ve explored dynamic blocks, but it seems they can only be used to modify fields and blocks within a resource. Is there a way to conditionally omit an entire resource based on variables in .tfvars?
Log in to reply.