简体   繁体   English

Terraform - Append 到 for_each 中 object 的字符串

[英]Terraform - Append a string to an object in a for_each

How do I go about appending the /* to the end of an object using for_each ?我如何 go 关于使用for_each/*附加到 object 的末尾? The goal is to have Terraform to go through the list of resource_arns and add /* to the end.目标是通过resource_arns列表将 Terraform 到 go 并在末尾添加/* However, I'm currently getting the error, "invalid template interpolation value".但是,我目前收到错误“无效的模板插值”。

If I have resources = each.value.resource_arns , then Terraform is able to create the resource, but it would just be without /* , which is not desired.如果我有resources = each.value.resource_arns ,那么 Terraform 能够创建资源,但它只是没有/* ,这是不希望的。

The desired outcome would be to have the resource created as:期望的结果是将资源创建为:

+ Action   = [
    + "s3:PutObject",
    + "s3:GetObject",
    + "s3:DeleteObject",
  ]
+ Effect   = "Allow"
+ Resource = "arn:aws:s3:::my-bucket-here/*"

Error错误

╷
│ Error: Invalid template interpolation value
│ 
│   on account-iam-policy/module/main.tf line 168, in data "aws_iam_policy_document" "this":
│  168:       resources = ["${each.value.resource_arns}/*"]
│     ├────────────────
│     │ each.value.resource_arns is list of string with 1 element
│ 
│ Cannot include the given value in a string template: string required.
╵

terragrunt.hcl terragrunt.hcl

inputs = {
  service_accounts = {
    "aws-s3-bucket" = {
      name          = "my-s3-bucket"
      policy = {
        "s3-rw" = {
          resource_arns = ["arn:aws:s3:::my-bucket-here"] 
          policy_keys = ["aws_s3_rw_policy"]
        }
      }
    }
}

main.tf主文件

data "aws_iam_policy_document" "this" {
  for_each = var.policy
  dynamic "statement" {
    for_each = contains(each.value.policy_keys, "aws_s3_rw_policy") ? ["apply"] : []
    content {
      effect = "Allow"

      actions = [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject"
      ]
 
      resources = ["${each.value.resource_arns}/*"]

    }
  }
}

variables.tf变量.tf

variable "service_accounts" {
  type = map(object({
    name      = string

    policy = map(object({
      resource_arns = list(string)
      policy_keys   = list(string)
    }))

  }))
}

You have to iterate over resource_arns :您必须遍历resource_arns

resources = [for arn in each.value.resource_arns: "${arn}/*"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM