Terraform - 如何将地图中的内部元素列表合并到具有设置类型值的地图中

问题描述 投票:0回答:1

您好,我正在尝试将一组定义如下的存储桶通知转换为映射:序列结构,以便我可以使用存储桶通知中的动态块迭代该序列。

buckets_with_events = [
  {
    "bucket" = "bucket1"
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".xml"
    "function" = "lambda1"
  },
  {
    "bucket" = "bucket1"
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".csv"
    "function" = "lambda1"
  },
  {
    "bucket" = "bucket2"
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".csv"
    "function" = "lambda1"
  }]

我需要将存储桶作为唯一键,并将所有事件合并为该键下的单个序列。我一直在尝试以下操作,但我似乎无法正确地将元组/集合/序列分配给地图键。

  bucket_events = flatten([
    for parent_obj in local.buckets_with_events:
        parent_obj.bucket => [
          for obj in local.buckets_with_events:
            {events = obj.events
             filter_suffix = obj.filter_suffix} if parent_obj.bucket == obj.bucket
    ]
  ])

我想要实现的输出如下:

bucket_events = {
  "bucket1" = [{
    
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".xml"
    "function" = "lambda1"
  },
  {
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".csv"
    "function" = "lambda1"
  }]
  "bucket2" = [{
    "events" = [
      "s3:ObjectCreated:*",
    ]
    "filter_prefix" = tostring(null)
    "filter_suffix" = ".csv"
    "function" = "lambda1"
  }]

通过这种方式,我可以使用 for_each 和动态轻松生成所需的所有存储桶通知,如下所示 --

resource "aws_s3_bucket_notification" "dynamic_s3_triggers" {
  for_each = var.bucket_events
  bucket = each.key
  dynamic "lambda_function" {

      content {
      lambda_function_arn = aws_lambda_function.functions[lambda_function.function].function].arn
      ...
       }
    }
}
dynamic terraform nested-loops local-variables
1个回答
0
投票

考虑以下代码:

locals {
  buckets_with_events = [
    {
      "bucket" = "bucket1"
      "events" = [
        "s3:ObjectCreated:*",
      ]
      "filter_prefix" = tostring(null)
      "filter_suffix" = ".xml"
      "function"      = "lambda1"
    },
    {
      "bucket" = "bucket1"
      "events" = [
        "s3:ObjectCreated:*",
      ]
      "filter_prefix" = tostring(null)
      "filter_suffix" = ".csv"
      "function"      = "lambda1"
    },
    {
      "bucket" = "bucket2"
      "events" = [
        "s3:ObjectCreated:*",
      ]
      "filter_prefix" = tostring(null)
      "filter_suffix" = ".csv"
      "function"      = "lambda1"
  }]

  buckets_notifications = {
    for bucket_event in local.buckets_with_events :
    bucket_event.bucket => [
      for be in local.buckets_with_events :
      {
        events        = be.events
        filter_prefix = be.filter_prefix
        filter_suffix = be.filter_suffix
        function      = be.function
      } if be.bucket == bucket_event.bucket
    ]...
  }
}

output "buckets_notifications" {
  value = local.buckets_notifications
}

跑步

terraform plan

Changes to Outputs:
  + buckets_notifications = {
      + bucket1 = [
          + [
              + {
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".xml"
                  + function      = "lambda1"
                },
              + {
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
          + [
              + {
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".xml"
                  + function      = "lambda1"
                },
              + {
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
        ]
      + bucket2 = [
          + [
              + {
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
        ]
    }
如果您不介意在每个数组元素中都有一个名为

buckets_notifications

 的额外属性,则可以稍微简化 
bucket

  buckets_notifications = {
    for bucket_event in local.buckets_with_events :
    bucket_event.bucket => [
      for be in local.buckets_with_events :
      be if be.bucket == bucket_event.bucket
    ]...
  }

跑步

terraform plan

Changes to Outputs:
  + buckets_notifications = {
      + bucket1 = [
          + [
              + {
                  + bucket        = "bucket1"
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".xml"
                  + function      = "lambda1"
                },
              + {
                  + bucket        = "bucket1"
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
          + [
              + {
                  + bucket        = "bucket1"
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".xml"
                  + function      = "lambda1"
                },
              + {
                  + bucket        = "bucket1"
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
        ]
      + bucket2 = [
          + [
              + {
                  + bucket        = "bucket2"
                  + events        = [
                      + "s3:ObjectCreated:*",
                    ]
                  + filter_prefix = null
                  + filter_suffix = ".csv"
                  + function      = "lambda1"
                },
            ],
        ]
    }

参见 如何在嵌套 for 循环中使用省略号 (...)

© www.soinside.com 2019 - 2024. All rights reserved.