Terraform - 在 for_each 循环中引用循环资源

Terraform - Referencing loop resources in for_each loop

我正在使用我的自定义模块创建 ECS 服务,这是 for_each 循环的一部分。 我想修改每个服务中的安全组,以允许来自特定服务的通信。

考虑以下 ECS 服务映射:

locals {
  ecs_services = {
    "hello-world" = {
      container_image = local.ecr_repository_url_map["hello-world"]
      container_port  = "5050"
    }
    "hello-world2" = {
      container_image = local.ecr_repository_url_map["hello-world2"]
      container_port  = "8080"
      allowed_serivces = ["hello-world"] 
    }
  }
}

以下是我不成功的尝试,导致:

Error: Cycle: module.ecs_service.module.this_ecs_service_task (close), module.ecs_service.module.this_ecs_service_task.var.allowed_security_group_ids (expand), module.ecs_service.module.this_ecs_service_task.module.ecs_service_security_group.var.ingress_with_source_security_group_id (expand), module.ecs_service.module.this_ecs_service_task.module.ecs_service_security_group.aws_security_group_rule.ingress_with_source_security_group_id, module.ecs_service.module.this_ecs_service_task.module.ecs_service_security_group (close), module.ecs_service (close), module.ecs_service.var.allowed_security_group_ids (expand)
module "ecs_service" {
  source  = "./ecs-service"

  for_each = {
    for name, service in local.ecs_services : name => service
  }

  name = each.key

  container_image = each.value.container_image
  container_port  = each.value.container_port


  allowed_security_group_ids = [for k, v in lookup(each.value, "allowed_services", []) : module.ecs_service[v].security_group_id]
  ...
}

在我看来,这种构造是可能的,因为资源本身实际上并不相互引用,并且可以创建一个不会产生任何冲突的顺序。但显然存在一些 Terraform 限制。

有没有我可以实施的解决方法,它不涉及为每个服务手动创建模块?

无论您在代码中有什么错误,您都不能做您想做的事。有两个原因:

  1. TF 将 同时 创建您的 ecs_service 模块的所有实例,而不是连续创建。所以你不能在创建之前引用不存在的资源。
  2. 地图迭代基于字典顺序,而不是地图元素插入顺序。在您的简单示例中,顺序与字典顺序一致,但依赖于此是以后出现问题的完美方式。