Using Terraform resource okta_group_assignments resource with Okta Terraform provider to loop through both apps and group ids

26 views Asked by At

I need help with the Okta Terraform provider to clean up my code. I want to map which apps have been created to the groups it needs for resource okta_app_group_assignments. My code is the following:

vars.tf

variable "app_group_assignments" {
  description = "Mapping of app names to groups"
  type        = list(object({
    app_name  = string
    groups    = list(string)
  }))
  default = [
    {
      app_name = "cisco_meraki_app",
      groups   = ["foo"]
    },
    {
      app_name = "freshworks_app",
      groups   = ["group1", "eng"]
    },
  ]
}

group_assignments.tf

resource "okta_app_group_assignments" "dynamic_assignments" {
  for_each = { for assignment in var.app_group_assignments : assignment.app_name => assignment }

  app_id = okta_app_saml[each.key].id

  dynamic "group" {
    for_each = { for group_name in each.value.groups : group_name => group_name }
    content {
      id       = okta_group.group[group.key].id
      priority = index(each.value.groups, group.key) + 1
    }
  }
}

I expected this to work for myself but I always got this error:

│ Error: Invalid reference
│ 
│   on group_app_assignments.tf line 4, in resource "okta_app_group_assignments" "dynamic_assignments":
│    4:   app_id = okta_app_saml[each.key].id
│ 
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

Is my best bet to either loop through my apps and assign the group or reference the app_id and loop through which groups to assign to? Would appreciate any help.

0

There are 0 answers