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
Using count and for_each together #31565
Comments
Hi @paololazzari, Objects in terraform can be expanded into a number of instances, keyed by either a numeric index, or a string key; there is no way to use both as an index into the same structure. If there are multiple sources of key values for a resource, they can be combined into a single We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help. Thanks! |
Mine wasn't a question, it was an enhancement request which is why I opened it with with the "enhancement" tag. Can you reopen it please? |
Hi @paololazzari, Could you elaborate a bit on the points from the issue template, like what the intended use case is, and what the proposal would look like? The literal interpretation of using If multiple nested levels of configuration are needed, forcing that to be only 2 levels deep and consisting of an ordered level and a map level is somewhat arbitrary and limiting. More flexibility can be gained by generating a flat data structure containing all of the desired instances without such limitations. |
Hey @paololazzari, I think I know what you are trying to achieve, correct me if I'm wrong. You want to create a resource with the locals {
workspace = terraform.workspace
for_each_set = toset(["1", "2", "3"])
}
resource "random_string" "conditional_for_each_resource" {
for_each = local.workspace == "staging" ? local.for_each_set : []
length = 10
} workspace default $ terraform workspace list
* default
staging
$ terraform apply
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed. workspace staging $ terraform workspace select staging
Switched to workspace "staging".
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
[…]
random_string.conditional_for_each_resource["3"]: Creating...
random_string.conditional_for_each_resource["1"]: Creating...
random_string.conditional_for_each_resource["2"]: Creating...
random_string.conditional_for_each_resource["1"]: Creation complete after 0s [id=TYl*-%z5#*]
random_string.conditional_for_each_resource["3"]: Creation complete after 0s [id=BU3jTP6RQ]]
random_string.conditional_for_each_resource["2"]: Creation complete after 0s [id=83RzgTf[t5]
Apply complete! Resources: 3 added, 0 changed, 0 destroyed. |
In addition to @bastiandg's suggestion, there's a more general form of this which allows more detailed conditions that conditionally exclude only a subset of the elements in input map or set: resource "example" "example" {
for_each = {
for k, v in var.something : k => v
if arbitrary_condition_here
}
} The In the simple case where you just want to disable something entirely based on a global value, you can write a condition that doesn't refer to the element key or element value, like this: variable "enable_thing" {
type = bool
}
resource "example" "example" {
for_each = {
for k, v in var.something : k => v
if var.enable_thing
}
} In any |
Hello, Since there has been no further updates about original topic, I'm going to close the issue. If you have more questions, feel free to start a discussion in the community forum. 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. |
When using both together:
What's the reason for this limitation? This seems like such a weird thing.
My use case is identical to this one #30030
The text was updated successfully, but these errors were encountered: