重写数据null_data_source为当地人 - 将计数重写为for_each

发布于 2025-01-18 22:25:23 字数 826 浏览 0 评论 0原文

data "aws_iam_role" "extra_iam_role" {
  count = length(var.s3_permission_extra_roles)
  name  = var.s3_permission_extra_roles[count.index]
}

data "null_data_source" "extra_iam" {
  count = length(var.s3_permission_extra_roles)
  inputs = {
    role_id = "${data.aws_iam_role.extra_iam_role[count.index].unique_id}:*"
  }
}

I want to rewrite that code like this:

data "aws_iam_role" "extra_iam_role" {
  for_each = var.s3_permission_extra_roles
  name  = each.value    
}

# is that correct ?


data "null_data_source" "extra_iam" {
  for_each = var.s3_permission_extra_roles
  inputs = {
    role_id = data.aws_iam_role.extra_iam_role????????
    #what code should be in previous line
  }
}

我在这里一无所知....请帮助

我想摆脱那个 count.index 因为当地人不能使用它...

我不知道如何将 count.index 重写为 for_each 相似性

data "aws_iam_role" "extra_iam_role" {
  count = length(var.s3_permission_extra_roles)
  name  = var.s3_permission_extra_roles[count.index]
}

data "null_data_source" "extra_iam" {
  count = length(var.s3_permission_extra_roles)
  inputs = {
    role_id = "${data.aws_iam_role.extra_iam_role[count.index].unique_id}:*"
  }
}

I want to rewrite that code like this:

data "aws_iam_role" "extra_iam_role" {
  for_each = var.s3_permission_extra_roles
  name  = each.value    
}

# is that correct ?


data "null_data_source" "extra_iam" {
  for_each = var.s3_permission_extra_roles
  inputs = {
    role_id = data.aws_iam_role.extra_iam_role????????
    #what code should be in previous line
  }
}

i am clueless here....please help

i want to get rid of that count.index because that cant be used by locals...

i am not sure how to rewrite that count.index into for_each similarity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2025-01-25 22:25:23

@kovokilla我相信'var.s3_permission_extra_roles'是类型集,在这种情况下,在第一个代码中,您应该做类似于下面的类似的操作

data "aws_iam_role" "extra_iam_role" {
  for_each = toset(var.s3_permission_extra_roles)
  name  = each.value    
}

,您可以为第二块类似地写一些类似的东西,

data "null_data_source" "extra_iam" {
  for_each = toset(var.s3_permission_extra_roles)
  inputs = {
    role_id = data.aws_iam_role.extra_iam_role[each.key].unique_id
  }
}

请记住以定义您的's3_permission_extra_roles'为类型在您的变量中设置

variable "s3_permission_extra_roles" {
  description = "s3 permission roles"
  default     = ["role1","role2"]
  type        = set(string)
}

@kovokilla I believe 'var.s3_permission_extra_roles' is of type set and in that case, in your first code you should do something similar to below

data "aws_iam_role" "extra_iam_role" {
  for_each = toset(var.s3_permission_extra_roles)
  name  = each.value    
}

and for second block, you can write something similar to below

data "null_data_source" "extra_iam" {
  for_each = toset(var.s3_permission_extra_roles)
  inputs = {
    role_id = data.aws_iam_role.extra_iam_role[each.key].unique_id
  }
}

Remember to define your 's3_permission_extra_roles' as type set in your variables.tf file

variable "s3_permission_extra_roles" {
  description = "s3 permission roles"
  default     = ["role1","role2"]
  type        = set(string)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文