Use default value of variable but modify one list entry

29 views
Skip to first unread message

mudasi...@gmail.com

unread,
Apr 27, 2021, 6:21:03 AMApr 27
to Terraform
Hi,

I have a variable type list with some defaults as shown below

variable "custom_parameters" {
  type = list(object({
    apply_method = string
    name         = string
    value        = string
  }))
  default = [
    { apply_method = "immediate", name = "log_connections", value = "1" },
    { apply_method = "immediate", name = "log_disconnections", value = "0" },
    { apply_method = "immediate", name = "log_lock_waits", value = "1" },
    { apply_method = "immediate", name = "log_hostname", value = "1" },
    { apply_method = "immediate", name = "log_temp_files", value = "0" },
  ]
  description = "Parameters"
}

I would like to know how can I use the defaults but at the same time change one item from the list. Let's say I want to set log_temp_files to 1 as shown below but at the same time want to use all the other default items and values of the list in my terraform module

{ apply_method = "immediate", name = "log_temp_files", value = "1" },

Regards,
Mudasir Mirza.

Leonardo Miranda

unread,
Apr 27, 2021, 12:11:10 PMApr 27
to terrafo...@googlegroups.com
Hi, you could probably use a mix of concat and distinct. 

When having lists as default values and at same time you want to make it flexible, I would suggest you to use locals instead of default in the input. With locals you can concat and distinct with passing the new variables. 

I hope it could help you.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/baf4ea4b-0d0c-4b8d-a6d7-fcfcd9662197n%40googlegroups.com.


--

Leonardo Miranda Nunes

Ian Norton

unread,
Apr 27, 2021, 1:23:41 PMApr 27
to Terraform
I do this slightly differently but get the desired result. I'd suggest a merge operation.
We use this for tags supplying a set of defaults but overriding locally as needed:

tags = merge(var.tags, {
  Name = "FooBarBaz",
})

First map argument to merge is your defaults, second for the values you wish to override.

Regards, Ian.



Leonardo Miranda

unread,
Apr 27, 2021, 5:21:23 PMApr 27
to terrafo...@googlegroups.com
Hi Ian, his case I think it's slightly different, he needs to do it with lists and not maps.

For maps I usually do as you said. 

var1 = {
my1 = var1
}

var2 = {
my2 = var2
}

var3 = merge(var.var1, var.var2)



--

Leonardo Miranda Nunes

Ian Norton

unread,
Apr 28, 2021, 3:39:43 AMApr 28
to terrafo...@googlegroups.com
On Tue, 27 Apr 2021 at 22:21, Leonardo Miranda <leonardomi...@gmail.com> wrote:
Hi Ian, his case I think it's slightly different, he needs to do it with lists and not maps.

For maps I usually do as you said. 

Ah, good spot, clearly not paying attention!

Thanks :)
Reply all
Reply to author
Forward
0 new messages