title: Getting error while using Terraform for_each for App Registration
body: <p>I am trying to do app registration and have few app roles that i would like to assign.
  My code is given below</p>
  <pre><code>resource &quot;random_uuid&quot; &quot;prod&quot; {}
  
  resource &quot;azuread_application&quot; &quot;app_prod&quot; {
    display_name    = format(&quot;app-%s-%s&quot;, var.project.name, var.project.environment.name)
    owners          = [data.azuread_client_config.default.object_id]
    identifier_uris = [format(&quot;https://contoso.onmicrosoft.com/%s-%s&quot;, var.project.name, var.project.environment.name)]
    api {
      oauth2_permission_scope {
        for_each                   = toset(local.oauth2_permissions)
        admin_consent_description  = each.value.admin_consent_description
        admin_consent_display_name = each.value.admin_consent_display_name
        enabled                    = true
        id                         = random_uuid.prod.result
        type                       = each.value.type
        value                      = each.key
      }
    }
  
    app_role {
      for_each             = toset(local.app_roles)
      allowed_member_types = each.value.allowed_member_types
      description          = each.value.description
      display_name         = each.value.display_name
      enabled              = true
      id                   = random_uuid.prod.result
      value                = each.key
    }
  
    web {
      logout_url    = format(&quot;https://app-%s-%s&quot;, var.project.name, var.project.environment.name)
      redirect_uris = []
  
      implicit_grant {
        access_token_issuance_enabled = true
        id_token_issuance_enabled     = true
      }
    }
  
    required_resource_access {
      resource_app_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
  
      resource_access {
        id   = data.azuread_service_principal.msgraph.app_role_ids[&quot;User.Read.All&quot;]
        type = &quot;Role&quot;
      }
    }
  }
  
  
  locals {
    app_roles = {
      application-administrator = {
        display_name         = &quot;Application administrator&quot;
        description          = &quot;Application administrators have the ability to administer the application.&quot;
        allowed_member_types = [&quot;User&quot;, &quot;Application&quot;]
      }
      BusinessAdmin = {
        display_name         = &quot;BusinessAdmin&quot;
        description          = &quot;Business Administrator&quot;
        allowed_member_types = [&quot;User&quot;]
      }
      mulesoft-integration = {
        display_name         = &quot;Mulesoft Integration&quot;
        description          = &quot;Allows MuleSoft Integration to talk to the APIs.&quot;
        allowed_member_types = [&quot;Application&quot;]
      }
    }
    oauth2_permissions = {
      read-and-write = {
        user_consent_description   = &quot;read-and-write&quot;
        admin_consent_display_name = &quot;Read and write data&quot;
        admin_consent_description  = &quot;Allows the app to read and write data&quot;
        user_consent_display_name  = &quot;Allows the app to read and write data&quot;
        type                       = &quot;User&quot;
      }
    }
  }
  
  data &quot;azuread_application_published_app_ids&quot; &quot;well_known&quot; {}
  
  data &quot;azuread_service_principal&quot; &quot;msgraph&quot; {
    application_id = data.azuread_application_published_app_ids.well_known.result.MicrosoftGraph
  }
  </code></pre>
  <p>The error that i get while doing terraform apply is :</p>
  <pre><code>Error: each.value cannot be used in this context
  │ 
  │   on resources.appreg.tf line 24, in resource &quot;azuread_application&quot; &quot;app_prodstats&quot;:
  │   24:     description          = each.value.description
  │ 
  │ A reference to &quot;each.value&quot; has been used in a context in which it
  │ unavailable, such as when the configuration no longer contains the value in
  │ its &quot;for_each&quot; expression. Remove this reference to each.value in your
  │ configuration to work around this error.
  ╵
  ╷
  │ Error: each.value cannot be used in this context
  │ 
  │   on resources.appreg.tf line 25, in resource &quot;azuread_application&quot; &quot;app_prodstats&quot;:
  │   25:     display_name         = each.value.display_name
  │ 
  │ A reference to &quot;each.value&quot; has been used in a context in which it
  │ unavailable, such as when the configuration no longer contains the value in
  │ its &quot;for_each&quot; expression. Remove this reference to each.value in your
  │ configuration to work around this error.
  ╵
  ╷
  │ Error: Reference to &quot;each&quot; in context without for_each
  │ 
  │   on resources.appreg.tf line 28, in resource &quot;azuread_application&quot; &quot;app_prodstats&quot;:
  │   28:     value                = each.key
  │ 
  │ The &quot;each&quot; object can be used only in &quot;module&quot; or &quot;resource&quot; blocks, and
  │ only when the &quot;for_each&quot; argument is set.
  ╵
  
  </code></pre>