在使用嵌套映射在 Terraform 中分配 GCS IAM 权限时出现错误。

huangapple go评论44阅读模式
英文:

Error in assigning gcs IAM permissions using nested map in terraform

问题

我正在尝试使用Terraform为不同的成员分配多个角色,但遇到了错误。这是为了在GCP中分配IAM权限。

main.tf看起来像这样:

locals {
  data_access = flatten([
    for bkt_key, bkt_value in var.buckets_data : [
      for user, roles in var.data_access : [
        for role in roles : {
          member = user
          bkt    = bkt_key
          role   = role
        }
      ]
    ]
  ])
}

resource "google_storage_bucket_iam_member" "buckets_data_access" {
  for_each = {
    for access in local.data_access : "${access.bkt}_${access.member}" => access...
  }
  bucket = google_storage_bucket.tf_buckets_data[each.value.bkt].name
  role   = each.value.role
  member = each.value.member
}

terraform.tfvars看起来像这样,请注意我在main.tf的嵌套映射中使用了两个不同的变量:

buckets_data = {
  "landing" = { 
    region          = "nane1",
    storage_class   = "COLDLINE",
    versioning      = "false",
    data_tier       = "raw",
    lifecycle_rules = ["retention-2years"],
    external_access = []
  },
  "dftemp" = {
    region        = "nane1",
    storage_class = "STANDARD"
  },
  "curated" = {
    region        = "nane1",
    storage_class = "STANDARD"
  }
}

data_access = {
  "group:GCP-npe@bell.ca" = ["roles/storage.objectViewer", "roles/Browser"]
}

我在终端中收到的错误如下:

$ terraform plan
╷
│ Error: Unsupported attribute
│
│   on main.tf line 29, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   29:   bucket = google_storage_bucket.tf_buckets_data[each.value.bkt].name
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
...

这个错误表明在main.tf中的google_storage_bucket_iam_member资源的29行,30行31行中,each.value是一个包含两个元素的元组,因此它没有任何属性。您可能需要检查main.tfterraform.tfvars文件中的数据结构,以确保它们与资源的预期属性匹配。

英文:

Im trying to assign multiple roles to different members using terraform but im running into an error.This is for assigning iam permission in GCP.
Use a combination of nested map. But the nested map became complex since Im using two different variables and use them in creating resources.

main.tf looks like this

locals {
 
  data_access = flatten([
    for bkt_key, bkt_value in var.buckets_data : [
      for user,roles in var.data_access : [
        for role in roles:{
        member = user
        bkt  = bkt_key
        role   = roles
      }]
    ]
  ])
}



resource "google_storage_bucket_iam_member" "buckets_data_access" {
  for_each = { for access in local.data_access : "${access.bkt}_${access.member}" => access... }
  bucket   = google_storage_bucket.tf_buckets_data[each.value.bkt].name
  role     = each.value.role
  member   = each.value.member
}

terraform.tfvars looks like this, Please note I'm using two different variables in the nested map of main.tf.

buckets_data                      = {
  "landing"                         = { 
    region                            = "nane1",
    storage_class                     = "COLDLINE",
    versioning                        = "false",
    data_tier                         = "raw",
    lifecycle_rules                   = ["retention-2years"],
    external_access                   = []
  },
  "dftemp"                          = {
    region                            = "nane1",
    storage_class                     = "STANDARD"
  },
  "curated"                         = {
    region                            = "nane1",
    storage_class                     = "STANDARD"
  }
}

data_access                           = {

"group:GCP-npe@bell.ca"= ["roles/storage.objectViewer","roles/Browser"]

}

error I received in my terminal

$ terraform plan
╷
│ Error: Unsupported attribute
│
│   on main.tf line 29, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   29:   bucket   = google_storage_bucket.tf_buckets_data[each.value.bkt].name
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 29, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   29:   bucket   = google_storage_bucket.tf_buckets_data[each.value.bkt].name
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 30, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   30:   role     = each.value.role
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 30, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   30:   role     = each.value.role
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 31, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   31:   member   = each.value.member
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on main.tf line 31, in resource "google_storage_bucket_iam_member" "buckets_data_access":
│   31:   member   = each.value.member
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.

答案1

得分: 1

如果我理解你的意思正确,以下的扁平化更好:

locals {  
  data_access = merge(flatten([
    for bkt_key, bkt_value in var.buckets_data : [
      for user,roles in var.data_access : {
        for role in roles: 
        "${bkt_key}-${user}-${role}" =>  {
          member = user
          bkt  = bkt_key
          role   = role
      }}
    ]
  ])...)  # 请不要移除这些省略号
}

然后

resource "google_storage_bucket_iam_member" "buckets_data_access" {
  for_each = local.data_access 
  bucket   = google_storage_bucket.tf_buckets_data[each.value.bkt].name
  role     = each.value.role
  member   = each.value.member
}
英文:

If my understanding is correct of what you are trying to do, the following flattening is better:

locals {  
  data_access = merge(flatten([
    for bkt_key, bkt_value in var.buckets_data : [
      for user,roles in var.data_access : {
        for role in roles: 
        "${bkt_key}-${user}-${role}" =>  {
          member = user
          bkt  = bkt_key
          role   = role
      }}
    ]
  ])...)  # please do NOT remove the dots
}

then

resource "google_storage_bucket_iam_member" "buckets_data_access" {
  for_each = local.data_access 
  bucket   = google_storage_bucket.tf_buckets_data[each.value.bkt].name
  role     = each.value.role
  member   = each.value.member
}

huangapple
  • 本文由 发表于 2023年2月8日 08:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定