Me and my colleague are setting up a new code and sort of template for an Application Gateway in Azure.
We mostly use locals and dynamic blocks, because there will be a lot of similar/repeatative objects.
Below it’s a block for backend settings and cookie_based_affinity is there manually defined as “Disabled”. However I would need this setting to have a “Default value” which can be “Disabled” and then to have an option to override the value to “Enabled”.
If we could make it working within the dynamic block that would be preferred. However going somehow around it using locals or even variable.tf is also an option.
dynamic “backend_http_settings” {
for_each = local.site_backends
content {
name = "${backend_http_settings.key}-be-htst"
cookie_based_affinity = "Disabled"
port = backend_http_settings.value.backend_settings_port
protocol = backend_http_settings.value.protocol
request_timeout = 30
path = backend_http_settings.value.backend_settings_override_backend_path
host_name = backend_http_settings.value.backend_settings_override_backend_host
pick_host_name_from_backend_address = backend_http_settings.value.backend_settings_pick_host_name_from_backend_address
probe_name = "${backend_http_settings.key}-probe"
}
}
P.S. please go slow on me, I’m a Terraform newbie.
I think you have some options here. It may depend how you want to do the override, and what the input looks like, but I think you should be able to do something like
# In this case, you're using a boolean within that data structure to set or unset it
cookie_based_affinity = backend_http_settings.value.cookie_based_affinity ? "Enabled" : "Disabled"
If this works for your use case, I think that’s the simplest and also the cleanest, since you’re using a boolean to control it.
You might also look at using try()
in certain situations, like
try(backend_http_settings.value.cookie_based_affinity, "Disabled")
(in this case, I think it would have to be the value “Enabled” vs. a boolean).
Or you can merge two data structures (i.e., merge defaults with your list of objects) in such a way that you can have default settings.
It’s been a little while since I’ve done this, but
hashicorp - How to merge a default list of Object with values from tfvars (Terraform) - Stack Overflow has some strategies for this.
Thank you, I will try it for sure. Tho’ not sure whether that’s going to work, because when I change it on a dynamic block level, it will use the same value for each local. Which is not what I would like to use. Or at least from those adjustments that I tried it worked like that. But I think I didn’t try any similar to yours.
Currently I solved it like this:
within locals I can use
cookie_based_affinity = "Disabled"
affinity_cookie_name = null
or
cookie_based_affinity = "Enabled"
affinity_cookie_name = "BlaBla"
then the dynamic block below works as expected
dynamic “backend_http_settings” {
for_each = local.site_backends
content {
name = "${backend_http_settings.key}-be-htst"
cookie_based_affinity = backend_http_settings.value.cookie_based_affinity
affinity_cookie_name = backend_http_settings.value.affinity_cookie_name
port = backend_http_settings.value.backend_settings_port
protocol = backend_http_settings.value.protocol
request_timeout = 30
path = backend_http_settings.value.backend_settings_override_backend_path
host_name = backend_http_settings.value.backend_settings_override_backend_host
pick_host_name_from_backend_address = backend_http_settings.value.backend_settings_pick_host_name_from_backend_address
probe_name = "${backend_http_settings.key}-probe"
}}
See the SO link - it should be possible to define defaults if you want. One way would be to merge the defaults with each item in the list, but I think there should be a cleaner way too.
Is it possible to set attribute defaults in a list of objects? - #4 by jmdoman is another thread you could look at.
If you show an example of how the data structure you’re passing into that looks, someone might be able to provide a little more help as to how else you could do it.