I think my problem is similar to Suggestions to implement modules that avoid "keys derived from resource attributes" error
but I’m still struggling with it. The TF module I wrote accepts a var:
variable "sso_groups_supplemental" {
default = {}
### Example:
# {
# "sso-aws-mycorp-marketplace-pc-ace" : { # AD group name
# role : "okta-aws-pc-ace", # generally matches the first SAML role name
# saml_roles : ["okta-aws-pc-ace"] # probably the IAM role you want to use
# }
# }
description = "{ <okta_group_name>: { role: <okta_role_name>, saml_roles:[] } "
type = map(object({
role = string,
saml_roles = list(string)
}))
}
Because Okta only accepts a group_idin the resource okta_app_group_assignment I have to use a data call to get the group_ids from the group name passed in:
data "okta_group" "sso-groups" {
for_each = var.sso_groups_supplemental
name = each.key
}
I’m trying to import an unrelated resource, but Terraform doesn’t like this data:
Error: Invalid for_each argument
on .terraform/modules/okta-aws-admin/okta.tf line 44, in resource "okta_app_group_assignment" "sso-other-groups":
│ 44: for_each = data.okta_group.sso-groups
│ ├────────────────
│ │ data.okta_group.sso-groups will be known only after apply
│
The "for_each" map includes keys derived from resource attributes that cannot be determined until apply, and so Terraform cannot determine the full set of keys that will identify the instances of this resource.
When working with unknown values in for_each, it's better to define the map keys statically in your configuration and place apply-time results only in the map values.
As far as I can understand what’s going on, the group ids are not derived values except in the sense that I need to look up the group_id from the group name (which was passed in as a variable) which is the whole point of data?
(Here, no value is being passed into var.sso_groups_supplemental so it’s the default {})
I don’t know the keys (group_ids?) ahead of time to statically define them in the TF code. Any ideas what I’m missing here?