[英]Terraform - Referencing loop resources in for_each loop
我正在使用我的自定义模块创建 ECS 服务,该模块是for_each
循环的一部分。 我想修改每个服务中的安全组,以允许来自特定服务的通信。
考虑以下 ECS 服务 map:
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 限制。
有没有我可以实施的解决方法,它不涉及为每个服务手动创建模块?
无论你在代码中有什么错误,你都不能做你想做的事。 有两个原因:
ecs_service
模块的所有实例,而不是连续创建。 所以你不能在创建之前引用不存在的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.