fbpx

Learn Build5Nines Forum

Find answers, ask questions, and connect with our
global community of IT professionals.

Forums HashiCorp Terraform Managing Conditional Creation of Terraform Resources Based on .tfvars Variables

  • Managing Conditional Creation of Terraform Resources Based on .tfvars Variables

    Posted by Joshua Falken on September 22, 2023 at 11:27 am

    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?

    Joshua Falken replied 2 months, 2 weeks ago 1 Member · 1 Reply
  • 1 Reply
  • Joshua Falken

    Member
    September 22, 2023 at 11:28 am

    In Terraform, you can conditionally create or exclude resources based on variables defined in your .tfvars file by using the count argument within the resource block. To achieve this, you can modify your Terraform configuration as follows:

    resource "cloudflare_record" "record" {

    count = var.cloudflare ? 1 : 0 # Conditionally create the resource

    zone_id = data.cloudflare_zones.domain.zones[0].id

    name = var.subdomain

    value = var.origin_server

    type = "CNAME"

    ttl = 1

    proxied = true

    In the above code, we use the count argument to determine whether the cloudflare_record resource should be created. If the var.cloudflare variable is true, Terraform will create one instance of the resource; otherwise, it will skip its creation entirely.

    By using this approach, you can dynamically control the inclusion or exclusion of resources based on the value of var.cloudflare in your .tfvars file. If var.cloudflare is set to true, the resource will be created; if it’s set to false, the resource will be omitted.

    This method allows you to achieve conditional resource creation while keeping your configuration clean and concise. It’s a powerful way to tailor your infrastructure deployments to specific requirements based on variables defined in your configuration files.

Log in to reply.