-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Allow using lists and maps with conditionals #12453
Comments
BTW, I did find one workaround: subnets = ["${split(",", var.is_internal_elb ? join(",", var.private_subnet_ids) : join(",", var.public_subnet_ids))}"] I'm using |
This is addressed by hashicorp/hil#42. |
@apparentlymart Conditional values in lists was not addressed nor resolved in any of the recent releases. Please re-open this thread so that this can be properly tracked and implemented. |
@jsalbright this issue is already open, since as you noticed the referenced PR was not actually merged. This is one of the things on the docket for a holistic revamp of the configuration language. We're still in the early stages of figuring that out, but do intend to fix this along with many other weird quirks with how the interpolation language deals with lists and maps. |
It definitely requires some attention, because currently it takes more time to find some workarounds in terraform than to develop the infrastructure itself. |
Until this is solved, here it is more workaround suggestions: If arrays are of same length:
If arrays may be of different length:
|
I have to add that I just ran into the same use case. Please fix! |
Same issue here, please fix |
Posting here in case someone runs into a similar issue as I did. I had to create a conditional that would set After reading about various workaround, this code was required to make this happen: propagating_vgws = ["${compact(split(",", var.vpc_vpg == "true" && var.subnet_public_route_vpg == "true" ? join("", aws_vpn_gateway.vpg.*.id) : join("", list(""))))}"] Why?
~ module.dev_vpc.aws_route_table.public.0
propagating_vgws.#: "0" => "1"
propagating_vgws.4108050209: "" => "0" It seems crazy to me that I had to do this to make a simple conditional work, so if anyone has any suggestions for improvements / simplification, I would love to hear them. |
Here is a workaround to use conditionals with maps
😓 |
For use in modules that use a lot of conditionals + list variables. This module is a little more convenient than performing the workarounds needed to use conditionals with list values. See: hashicorp/terraform#12453
Just faced the same issue with public/private subnets for elb |
@modax your first suggestion crashes Terraform for me on 0.10.2
|
Would love to see this fixed. Any plans to do so? |
This would be super useful |
I've been fighting a few issues in this realm, trying to come up with concise, reusable patterns for parameterization and dynamic resource creation across environments. It seems like the core team is acutely aware of the shortcomings and are working to address them. In the meantime, perhaps it makes sense to explore / collaborate on solutions to these problems, where possible, in userspace? Inspired by this module -- which seemed like a simple, reusable pattern -- I started working on modular, (mostly) pure-terraform solutions to the obstacles I've been hitting. I plagiarized / extended the aforementioned module a hair, then hacked at [nested] map lookups / conditionals a bit over in this module. I thought I'd share the work, in case it's useful for anyone here (feedback is welcome, too -- this stuff was hacked out in a vacuum on a Sunday). Both patterns seemed relevant to this thread, and could be pretty easily extended to other use cases like checking for the existence of [nested] map keys, nested Is there an existing / open contrib space for these type of utility modules? I googled a bit and came up short, and the registry didn't seem quite appropriate. |
Would love to see this fixed. Any plans to do so? |
as long as you can manipulate any type to render that type as string you can work with pretty much all types within conditionals. No? let's assume you have a string you want to add to a lists wrapped in a conditional. something like this:
|
Terraform is a great product and they have no obligation to implement our feature requests. But let’s not pretend using string manipulation is an appropriate substitute for proper types. Refusing to use a shoe to hammer a nail does not make me “lazy.” |
I'm facing the same issue - I cannot conditionally assign tags to EC2 instances based on index count because neither lists nor maps are currently supported in conditions.
getting At column 8, line 5: conditional operator cannot be used with list values in: or At column 11, line 5: conditional operator cannot be used with map values in: It would be nice to have support for this rather than using quite |
Having same issue here. Does somebody know a workaround with non-string lists until this is fixed? |
I'd love this feature as well. One use case I have is I've got some IAM groups and policies and their names include an appended variable for environment ("dev" or "prod"). This way, and using terraform workspaces, I could deploy the same group and its attached policies but each with a different name, and have my test users attached to the dev ones in order to test out new IAM permissions before I deploy to prod. I wish the following code would work:
|
As this was blocking me from moving forward on a project, I just knocked up a quick and dirty conditional provider, inspired by the module linked by @davidquarles. The module was insufficient for me, as I need The provider is not at all thoroughly tested, and as such I've not tagged a release or provided any binaries yet, but feel free to build and try it out. From some brief testing, it's doing what I need it to for now. |
Workaround for 'conditional tags' (rather conditional map, where an empty map() is returned or a full map. This was tested on terraform 0.11.2; thanks to @nanoz for the inspiration and I hope this saves people some time.
|
We're encountering an issue with this as well, with a similar use case as the OP described. Rather than cluttering up our module(s) with dozens of additional extraneous inputs, or building permutations of the module, we'd like to just use conditionals with lists to try and keep the modules lean. In our case, we're trying to configure explicit egress rules for network traffic to maintain compliance with GDPR. If the appliance is in the EU, use the EU CIDR blocks, otherwise, use US CIDR blocks, etc. Right now the proposed work-around seems to function as expected: I'm open to better ways to handle this, but it seems like conditionals with lists are a reasonable approach. |
Exactly this! Hope TF 0.12 and HCL2 get released soon. |
…able due to conditionals not being supported for list values (hashicorp/terraform#12453)
When I saw this, it made me think it was safe to invest my time working on a module that makes use of lists within conditionals. Am I wrong?
|
Hi everyone, just in case anyone hasn't yet heard, HCL2 and Terraform 0.12 have been announced with a preview available https://www.hashicorp.com/blog/terraform-0-1-2-preview |
@cam-fulcrum Where do you see a preview available? |
@gaui In absence of an officially packaged preview you could probably |
Since I smell some confusion here: that article is itself the preview. In other words, it's a preview of the announcement text, not of the running code itself. There will be pre-releases of this once it is feature-complete, the first of which I expect will be called an "alpha", since we'll be building it mainly for our own testing and testing of module- and provider-authoring partners so we can get some early warning of any misses in the automatic upgrade tool and other compatibility problems. As @tdmalone mentioned, there is development going on in There is, however, enough implemented that we can see the situation relating to this issue:
|
Judging from the history this is going to come out soon. I just wanted to add that I stumbled into a use case when defining security group rules and network ACLs |
Noting that conditionals will work just fine with lists and maps in 0.12 and we blogged a preview of it: https://www.hashicorp.com/blog/terraform-0-12-conditional-operator-improvements |
Another workaround to use in Terraform 0.11.*: I needed to add a key conditionally to an existing map
|
@llibicpep 's workaround seems to be working. |
Hi all! In v0.12.0-alpha1 I've verified that the root problem here is now fixed: variable "whether" {
default = false
}
output "example" {
value = var.whether ? list("a") : list("b", "c")
}
There is a remaining problem #19180 which required me to use the old-style Since the remaining problem is covered by that other issue, I'm going to close this one now. Thanks for sharing the use-cases here, and thanks for the patience while we got the groundwork in place to generalize the conditional operator. |
another workaround (also silly) would be |
None of the workarounds are working for me. Can anyone suggest?
Error:
|
@ethicalmohit Solution would be a map of maps or map of lists, I wrote a short article how to do that: https://medium.com/@business_99069/terraform-conditional-assignment-of-a-map-to-attribute-9bb8471771cb |
what is |
This allows me to separate all the keys (element 0) from the values (element 1) |
Update the original `lb_listener_rules` module, that hasn't been used so far, to support a more specific case, where we want to add a target group and listener rule for that target based on host headers. Each target group has its own health check, so we can ensure traffic is only routing when the application is up and healthy. We are testing this module with the backend LB. We can choose when to use the application healthchecks and target groups with the `enable_lb_app_healthchecks` variable. By default the LBs continue using the default target group. When we set `enable_lb_app_healthchecks` to true, we are creating the LB forward rules from the service cnames variable. This expression: ``` rules_host = ["${compact(split(",", var.enable_lb_app_healthchecks ? join(",", var.backend_public_service_cnames) : ""))}"] ``` is required because we can't use a conditional with list variables: hashicorp/terraform#12453 This is fixed in Terraform 0.12, until then we need to implement a workaround.
I found a workaround to extend @brikis98 's solution to maps. It uses
|
I know this is closed, but I had issues finding examples of it.
|
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
I found out today that if you try to use a list or a map with a conditional, you get the error:
It turns out this is an explicit check built into the TypeCheck method.
The comment above the code says "for now this is simply prohibited because it doesn't seem to be a common enough case to be worth the complexity." I thought I'd toss out at least one use case where this would be handy:
is_internal_elb
, which is used to set theinternal
parameter of the ELB to true for internal ELBs and false for public ELBs.subnets
parameter, which is a list of subnet IDs to attach to the ELB. Whenis_internal_elb
is set to true, I'd like to set this to a list of private subnet IDs. Whenis_internal_elb
is false, I'd like to set it to a list of public subnet IDs.Rough pseudo code:
This seems like a fairly straightforward use case, but with the current
TypeCheck
method, it won't work.The text was updated successfully, but these errors were encountered: