I’m not 100% sure I’m understanding the question, but if you are wanting the equivalent of the code at the bottom of your post while using the local variable given then something like this would work:
I think dynamic labels isn’t the right solution to your current problem, because it’s for creating blocks that have label strings after the block type. In other words, the example you showed with aws_lb_listener_rule would (if I’m reading your partial example correctly) generate something like the following structure:
Guessing a bit from the particular label values you used here, and referring to the documentation for this resource type, I think your intent is to generate separate host_header and path_header blocks based on the input. However, your condition local value is just a single object rather than a list, and likewise you only have one host_header object and one path_pattern object in there, so it doesn’t seem like you need dynamic blocks at all here:
You only need to use dynamic if the number of blocks to be included is dynamically-decided; if you only need one block of each type then you can just write them out, like I did above.
On the dynamic labels feature, I am confused with what it does and can’t find an example or valid use case. The docs (Dynamic Blocks - Configuration Language - Terraform by HashiCorp) does not make it clearer to me. Would you mind sharing a practical example of its usage?
A dynamic block with labels would be useful only for a resource type whose schema includes a block type that expects labels. There aren’t many examples of that, so in practice labels doesn’t see much use.
Block types with labels are so rare in providers that I had to think a bunch to remember one to use as an example! And my example happens to be a community provider I published as part of experimenting with testing in Terraform, which has a data source called testing_assertions which has a block type equal which expects one label to uniquely name each of the assertions.
Now I want to reinforce that I’m only using this resource type as an example because it was the one I thought of while trying to remember an instance of a block type expecting a label, and I don’t think this would actually be a useful thing to do in practice, but here’s how you could in principle write the above equal blocks using dynamic:
The goal that I was trying to achieve with the dynamic block was two condition block with either a host_header or path_pattern block in the condition block depending on what’s in the local and var as such:
I am impressed that you still get what I was trying to achieve despite how confusing my question was!
Yup, this is one of the solution that I went with seeing that the labels part is not how I assumed it should work HAHA.
I was wondering if there was a way to only use one nested dynamic block like how I described and ended up here trying to avoid writing ONE more dynamic block.
I thought that I could achieve it with labels in the dynamic block to override the dynamic host_header with either host_header or path_pattern in labels:
Here I cannot use labels as it accepts list, but in this case there can be only 1 authentication block (aws, saml or oidc ) per resource, kindly suggest.
FWIW: was looking into how to use lables and stumbled upon this thread and it looks like Martin’s example should have all occurrences of each replaced with equal to match dynamic block label name (e.g. each.key should be equal.key, each.value.content should be equal.value.content, so on).