Use of locals variable

Hi everybody,

i’m trying to implement terraform on my cloudflare multiple zones (domain names) in order to create one security rule and apply it on my multiple zones (domain names). In order to implement this, i built a tf file with this
locals {
cloudflare_zones = {
myzone_com = {
zone_id = “XXX”
domain_name = “www.myzone.com
}
myzone_org = {
zone_id = “XXX”
domain_name = “www.myzone.org
}
}
}

resource "cloudflare_rate_limit" "default_rate_limit-hourly" {
  for_each = local.cloudflare_zones
  zone_id = each.value.zone_id
  threshold = 2400
  period = 3600
  match {
    request {
      url_pattern = "${each.value.domain_name}/*"
      schemes = ["_ALL_", ]
      methods = ["_ALL_", ]
    }
    response {
      statuses = []
      origin_traffic = true
    }
  }
  action {
    mode = "simulate"
    timeout = 3600
  }

  correlate {
    by = "nat"
  }

  disabled = true
  description = "Default rate limit - hourly"
} 

It did not find other solutions than use locals to iterate on multiple associative tables like these ones. This is working fine.

In order to architect my project fine, i wanted to put these local vars in a dedicated variable file, but when i do that , i get this error

terraform plan -var-file var-cloudflare.tfvars

Error: Missing attribute separator

  on var-cloudflare.tfvars line 1, in locals:
   1: locals {
  cloudflare_zones = {
    myzone_com = {
      zone_id = "XXX"
      domain_name = "www.myzone.com"
    }
	myzone_org = {
      zone_id = "XXX"
      domain_name = "www.myzone.org"
    }
  }
}


Expected a newline or comma to mark the beginning of the next attribute.

Thanks very much for any help

Hi,

You can’t use local variables inside a tfvars file, locals are intended to be temporary variables used when a script is processing.

Terraform 0.12 does support complex data structures in variables, you should just be able to define these as standard variables.

variable "cloudflare_zones" {}

then add to your tfvars like

cloudflare_zones = {
    myzone_com = {
      zone_id = "XXX"
      domain_name = "www.myzone.com"
    }
	myzone_org = {
      zone_id = "XXX"
      domain_name = "www.myzone.org"
    }
  }
}
1 Like

Hi Nicholas,

thanks very much for your answer. I tried your solution by defining this in my mainvar.tf file :
variable “cloudflare_zones” {}
cloudflare_zones = {
myzone_com = {
zone_id = “XXX”
domain_name = “www.myzone.com
}
myzone_org = {
zone_id = “XXX”
domain_name = “www.myzone.org
}
}
When i run with terraform plan, i get
terraform plan

Error: Unsupported argument

  on mainvar.tf line 2, in variable "cloudflare_zones":
   2:     myzone_com = {

An argument named "myzone_com" is not expected here.

I run terraform 0.12.10

Thanks again