Search code examples
terraformdatabricksazure-databricksterraform-provider-databricks

It is possible to use the outputs of a for in another for? [TERRAFORM]


it turns out that I have a module in my company to create WorkSpaces of databricks, it turns out that to create the pools and attach them we do it resource by resource and that results in many lines of code. Would it be possible to do it in some way to only have to create two resources and go through them?

I am trying something like this:

resource "databricks_instance_pool" "pool_size" {
  for_each                              = { for pool in var.instance_pool : pool.name => pool }
  instance_pool_name                    = pool.name
  min_idle_instances                    = pool.min_instances
  max_capacity                          = pool.max_instances
  node_type_id                          = pool.node_type
}

resource "databricks_permissions" "can_attach_pool" {
  instance_pool_id = databricks_instance_pool.pool_size[*].id
  access_control {
    group_name       = "users"
    permission_level = "CAN_ATTACH_TO"
  }
}

Solution

  • Since you are using for_each in the first resource, you could use resource chaining with for_each [1] in the second resource:

    resource "databricks_instance_pool" "pool_size" {
      for_each                              = { for pool in var.instance_pool : pool.name => pool }
      instance_pool_name                    = pool.name
      min_idle_instances                    = pool.min_instances
      max_capacity                          = pool.max_instances
      node_type_id                          = pool.node_type
    }
    
    resource "databricks_permissions" "can_attach_pool" {
      for_each         = databricks_instance_pool.pool_size
      instance_pool_id = each.value.id
      access_control {
        group_name       = "users"
        permission_level = "CAN_ATTACH_TO"
      }
    }
    

    [1] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each#chaining-for_each-between-resources