-
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: Splat for all resources of a type #19931
Comments
I have a need for the same use case as described above. In addition, I'd like this 'resource type splat' feature to be usable in conjunction with the feature described in issue #9067 (and/or the upcoming for/for_each with optional filtering via an if qualifier) to enable me to create a single output variable whose value is a list of some or all of the resources (entire resources, not just IDs, or names) of a given type, without specifying or knowing the names or IDs of the included resources. The need for this is based on my use of code generation tools to generate HCL dynamically. I want separate parts of my code generation to be able to generate HCL that will be combined with each other and applied as a single set of TF configuration, but also to have my separate code generation components to remain encapsulated and unaware of each other. But I'd like to have 1 component that generates output variables that collect all or some of the resources (and/or specific computed/exported attributes of them) into named output variables. For example:
Similar use cases exist for aws_lb.dns_name, aws_s3_bucket, and many other resources where the HCL configuration may contain a dynamic number of a specific type of resource (but not count-based repetition; each resource of the same type is explicitly declared separately, distinctly, and independently, as each resource of the same type has unique properties and configuration) and computed or exported attributes that aren't known until |
A further (related but distinct) use case is that output variables similar to the examples above would (in many cases) be useful as 'reusable' HCL code, and could be included (without requiring any changes to the output variable HCL nor any configuration via input variables, etc.) in almost any arbitrary TF configuration that includes the referenced providers and resource types. This would be useful to people that manage multiple TF configurations as a way of providing standardized (i.e. consistent across TF configurations, but with custom content and format determined by the output variables' reusable HCL definition) outputs. This 'reusable outputs' use case applies equally well regardless of whether the separately managed TF configurations are manually generated or generated by code or by some other means. |
Also, given that this change probably requires a change to HCL itself, it'd be great if this could be included in the initial v0.12 release. Is there still a chance of that? |
One more use case: I am creating N identical dynamodb tables in a module.
|
yes need this for output vars - different customized vms (windows and linux) created by module as different resources the same type! |
I was recently working on getting some of the less common resources tagged and some kind of ability like this would have been really useful to avoid needing to maintain a list of resources: locals {
resources_tags = setproduct(
concat(
values(aws_efs_mount_target.a)[*].network_interface_id,
values(aws_efs_mount_target.b)[*].network_interface_id,
values(aws_efs_mount_target.c)[*].network_interface_id,
values(aws_efs_mount_target.d)[*].network_interface_id,
values(aws_efs_mount_target.e)[*].network_interface_id,
module.vpc.vpc_endpoint_efs_network_interface_ids,
module.vpc.private_route_table_ids,
module.vpc.public_route_table_ids,
),
[for key, value in local.tags :
[key, value]
]
)
}
resource "aws_ec2_tag" "all" {
count = length(local.resources_tags)
resource_id = local.resources_tags[count.index][0]
key = local.resources_tags[count.index][1][0]
value = local.resources_tags[count.index][1][1]
} |
Here we are, approaching 3 years after this was opened. Is this at all likely to happen? |
I need this to For now, I've quickly worked around it using an external program to generate the list from the terraform state for any given resource type. The https://github.com/HariSekhon/DevOps-Bash-tools and working Terraform code using it can be found here: https://github.com/HariSekhon/Terraform in the |
Jumping on this bandwagon, for called modules within a workspace. I need to output certain metadata values for each system built by a module, and I can't use Example: This is currently the only way I got it working. This is NOT manageable at scale at all. Each new server built requires the sys admins to also add to the output value... which people have already forgotten, causing failed builds.
All we really need is the ability for "outputs" to iterate over the built resources and modules in state, instead of needing to know the exact resource id ahead of time. |
Adding my use case: Currently creating a bunch of aws_apigatewayv2_domain_name's to be later used in aws_apigatewayv2_api_mappings Would be nice to have something similar to the following:
then a local similar to this:
to then be referenced with:
|
A use case in Azure: creating diagnostics settings for a bunch of resources of a type resource "azurerm_monitor_diagnostic_setting" "keyvaults" {
for_each = toset(azurerm_key_vault.*.id) # <-- or something like that
name = "storage-export"
target_resource_id = each.key
storage_account_id = azurerm_storage_account.diag_logs.id
log {
category = "AuditEvent"
enabled = true
retention_policy {
enabled = true
days = 0
}
}
log {
category = "AzurePolicyEvaluationDetails"
enabled = true
retention_policy {
enabled = true
days = 0
}
}
} |
Piling on the use case list, this would be great for setting dynamic resource dependencies when you want a resource to depend on all resources of another type: resource "type" "name" {
depends_on = trigger_resource_type.*
...
} |
Useful for working with the Okta provider, specifically when using
|
A similar problem led me here too. What I ended up doing was putting the group constituents into YAML files so I could then use fileset to consolidate in more creative ways in the terraform files. |
Can you expand on this? I will likely need to end up doing similar as a workaround until this feature has been implemented. Did the files just contain the individual groups? How did you reference the files when building your list for okta_group_role? I am not familiar with fileset. |
Mine is for Azure DevOps and I specifically wanted a file per set of groups because of how our code review process works, but I also wanted a distinct list of all users in all defined groups. The idea should allow more creative groups of things in general though. I made a locals that looks something like:
|
To add another use case on a (almost) 3 year old issue, I am trying to create a cloudwatch dashboard in aws with the alarm widget. The cloudwatch widget only accepts arns. Would be super useful if I could make my code look something like this:
|
Can we get insights from the developers if this is worked on or why this is being withheld? |
@mBlomsterberg It is not being worked on. It is currently at # 33 of the most-upvoted issues. We use upvotes as a method to help feed our product backlog. Thanks for your question! |
Got another use case for making it easier to access lambdas in a template file
|
Summary: > This stack resolves issues encountered when setting up fresh staging AWS account with Terraform. This diff resolves an issue when running terraform plan on plain fresh AWS account. The `aws_dynamodb_table` data doesn't resolve to anything because the DDB table isn't yet created. Resources from inside module aren't globally exposed, so I created a `outputs.tf` file in the shared module and iterated over explicitly-specified table resources to expose them. > I really wanted to do it in a more automated way, but TF has no good mechanism for iterating over all resources yet. There's an [[ hashicorp/terraform#19931 | open issue ]] for that where people share other usecases for such feature. Depends on D8714 Test Plan: Production `terraform plan` with no changes. Staging plan no longer fails. Reviewers: jon, varun Reviewed By: jon Subscribers: ashoat, tomek Differential Revision: https://phab.comm.dev/D8715
Any progress - would be very handy! |
Here's a workaround I found:
just adding all the resources you need (addressed by name) |
I thought this was a very basic syntax that should be supported Getting the list of the same resource type is very useful |
would love this feature |
Adding another +1 on this feature would be great to have. |
This is something our organization really needs. Please prioritize this as it also seems that others have need for this feature as well 🚀 |
For sure this is exactly what I would like to have so that I could do something like this for dynamically defined outputs: resource "coralogix_webhook" "opsgenie" {
for_each = local.opsgenie_team_keys
name = "opsgenie-${each.key}"
opsgenie = {
url = "https://api.opsgenie.com/v1/json/?apiKey=${each.value.api_key}"
}
}
resource "coralogix_webhook" "jira" {
for_each = local.jira_team_keys
name = "jira-${each.key}"
jira = {
url = "https://redacted.atlassian.net"
project_key = each.value.project_key
}
}
#
# Then with this type of output we would be able to define the following
#
output "coralogix_webhooks" {
value = { for hook in coralogix_webhook.* : hook.name => hook.id }
} |
Current Terraform Version
Use-cases
I'd like to be able to use
resource_type.*.id
to get a list of theid
of every resource of a type; currently, you can only do this for resources that have acount
attribute (or eventuallyfor_each
, I guess). Sometimes, you have a bunch of resources you want to declare explicitly because they don't necessarily have very much in common, but you still want to refer to them all from another resource.My explicit use-case is the third-party
auth0
provider. I've got a bunch ofauth0_client
resources that all have their own custom configuration. Then, I've got anauth0_connection
resource which represents our actual identity provider (an LDAP server). In creating theauth0_connection
resource, I need to pass a list ofauth0_client
ids that that connection will be used for. And I'd love to get that list of ids by simply writingauth0_client.*.client_id
. But that's not possible.Attempted Solutions
This didn't work:
I got this error:
A similar attempt with
random_id[*].hex
also failed.The text was updated successfully, but these errors were encountered: