-
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
Add a function to find the index of an element in a list. #2704
Conversation
👍 this is a much needed feature. The original syntax for this is super obfuscated, would be much nicer with this index function. |
FYI this works around #1336 |
Looks good on first glance! Will give it a closer review and a merge within the next few days. 👍 |
❤️ that's good to hear, we've already started programming against this and FWIW it's working well for us, solving the fundamental 'can't pass maps between modules' problem, albeit in a fairly clumpsy way. Perhaps we could consider adding another function, something like 'lookup(list, list, key)' that would build a map from two parallel lists? This is a very common pattern for us, and I think others from what I hear in IRC and from reading through issues. |
any update on merge? |
Can we get this merged? All checks have passed and it's up to date with the base. |
Sorry for the delay on this - folks! LGTM, merging now. |
Add a function to find the index of an element in a list.
Awesome! Thanks so much guys! |
Ahhh, with our own build including this commit I pretty much forgot about the PR. Thanks for merging it in, @phinze! |
I'm running into this issue, but I'm not clear on how this can be used. Would someone be able to provide an example? I have one variable which is a map, but the citation in comment 0 references 3 variables.
If I have the following map:
How could I just pass just us-east-1a and us-east-1b? |
You have two options: 1) you can get rid of the map and replace it with two lists, or 2) maybe try using the undocumented |
@thegedge 👍 thanks! |
I am trying to do the same thing. But I am getting syntax parse error. example.0 = "value1" I am trying to pass it to a module so that I can use it in the template file as shown below. resource "aws_instance" "exp" { In the module function: module "sample_module" { Any suggestion would be really helpful :) |
@Shruti29 in your case, since the keys are just integers coming from the In your module:
In your tf config that instantiates that module:
|
@thegedge Of course! This works! Thank you. 👍 |
@thegedge Using a list instead of maps made it less readable. Since the list can go on to contain 20-30 values.
So the workaround I did was something like this
And in the main.tf
This works perfectly well. But I am hardcoding the string delimeter Cant seem to think of a cleaner way to use interpolation and create a comma separated string(?list) of keys and value. Any better way that I am missing here ? |
@Shruti29 You should be able to just do, for example, P.S. I highly recommend checking out the #terraform-tool IRC channel on Freenode. A lot of helpful people in there to help with these kinds of questions 😄 |
@thegedge I agree :) thanks |
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. |
We've hacked around not being able to pass maps as inputs to modules with interpolations akin to the following (newlines and comments added for easier reading):
In essence, we chomp everything in a comma-separated key list starting from the key we're searching for, split the result, find the length of that list minus one, and use that as the index into the comma-separated values list. With an index function we can at least get rid of the replace madness and make our intent clearer, with the above now looking something like this: