7
votes

New to terraform. Using terraform 0.12. I am trying to terraformize our Cloudflare settings.

Need to create multiple resources of the same type (cloudflare provider page_rule resource) and pass values to the resource "template" using config.tfvars.

I've declared a variable of list(object({...})) type.

Need some of the object parameters to be optional, so if the entries are not specified in config.tfvars for some of the list elements, resources are created without them.

I've read about terraform's 0.12 null default variable values, but I am not sure there is a way to specify default value for terraform object parameters. All examples I've seen only specify the type of parameters.

Code example:

variables.tf

variable "example_page_rule"{
      type = list(object({
            cache_level = string,
            ssl = string,
            target = string    
      }))   
}

main.tf

resource "cloudflare_page_rule" "page_rule" {
        count = length(var.example_page_rule)
    cache_level = var.example_page_rule[count.index].cache_level
    ssl = var.example_page_rule[count.index].ssl
    target = var.example_page_rule[count.index].target
}

config.tfvars

page_rules = [
{
   target = "www.target-url.com",
   ssl = "flexible",
   cache_level = "simplified",
},

{
   target = "www.target-url.com",
   cache_level = "simplified"
}
]

When trying to plan using the above configuration error occurs: "ssl" value is required.

If I change the config.tfvars to the following, all work as expected config.tfvars, but I'd like to avoid entering null values if possibe.

page_rules = [
{
   target = "www.target-url.com",
   ssl = "flexible",
   cache_level = "simplified",
},

{
   target = "www.target-url.com",
   ssl = null,
   cache_level = "simplified"
}
]
1

1 Answers

3
votes

This is a feature request and is being tracked here: https://github.com/hashicorp/terraform/issues/19898. Follow that link and give a :+1: (thumbs up emoji) on the original post so their dev team prioritizes it.