Terraform用for_each部署多ECS服务时遇ALB属性错误求助
Terraform错误排查:Unsupported attribute 'alb_target_groups'
错误信息
│ Error: Unsupported attribute │ │ on main.tf line 42, in module "ecs": │ 42: alb_target_groups = module.alb.alb_target_groups │ ├──────────────── │ │ module.alb is a object │ │ This object does not have an attribute named "alb_target_groups".
相关配置代码
main.tf
module "alb" { source = "./modules/alb" vpc_id = module.network.vpc_id security_group_alb = module.security.security_group_alb public_subnets = module.network.public_subnets customer_env = local.customer_env env = var.env alb_target_groups = local.alb_target_groups listeners = var.alb_config.listeners } module "ecs" { source = "./modules/ecs" family_name = var.family_name env = var.env ecs_service_name = var.ecs_service_name service_config = var.customer_config alb_target_groups = module.alb.alb_target_groups # alb_target_groups = local.alb_target_groups }
env.tfvars
ecs_service_name = ["customer"] alb_config = { name = "Public-Alb" listeners = { "HTTP" = { listener_port = 80 listener_protocol = "HTTP" } } } customer_config = { "customer" = { name = "customer" is_public = true container = 80 host_port = 80 cpu = 256 memory = 512 desired_count = 1 alb_target_group = { port = 80 protocol = "HTTP" path_pattern = ["/*"] health_check_path = "/health" priority = 1 } } }
modules/ecs/ecs.tf
resource "aws_ecs_service" "main" { for_each = var.service_config name = "${each.value.name}-Service-${var.env}" cluster = aws_ecs_cluster.main.id load_balancer { #target_group_arn = var.target_group_arn target_group_arn = var.alb_target_groups[each.key].arn container_name = var.container_name container_port = var.alb_container_port } }
locals.tf
locals { alb_target_groups = { for service, config in var.customer_config : service => config.alb_target_group } }
问题根源
module.alb 模块并未对外输出 alb_target_groups 属性,导致在ecs模块中引用时找不到该属性。虽然你已经将local.alb_target_groups传递给alb模块作为输入,但模块默认不会自动输出输入变量,必须显式定义输出。
解决方案
1. 在alb模块中添加输出配置
在modules/alb/outputs.tf文件中添加以下输出,将创建的Target Group ARNs按服务名称映射输出:
output "alb_target_groups" { type = map(object({ arn = string })) description = "Map of service names to their corresponding ALB Target Group ARNs" value = { for service_key, tg in aws_lb_target_group.main : service_key => { arn = tg.arn } } }
2. 确保alb模块的Target Group使用for_each创建
检查modules/alb/alb.tf中的Target Group资源,必须基于var.alb_target_groups使用for_each创建,才能和服务名称一一对应:
resource "aws_lb_target_group" "main" { for_each = var.alb_target_groups name = "${each.value.name}-${var.env}" port = each.value.port protocol = each.value.protocol vpc_id = var.vpc_id target_type = "ip" # 根据你的ECS部署模式调整,Fargate用ip,EC2用instance health_check { path = each.value.health_check_path protocol = each.value.protocol matcher = "200" interval = 30 timeout = 5 healthy_threshold = 2 unhealthy_threshold = 2 } depends_on = [aws_lb_listener.main] }
3. 验证ecs模块的变量定义
在modules/ecs/variables.tf中确保alb_target_groups变量类型正确:
variable "alb_target_groups" { type = map(object({ arn = string })) description = "Map of service names to ALB Target Group ARNs" }
完成以上修改后,重新执行terraform init和terraform plan,错误即可解决。
内容的提问来源于stack exchange,提问作者Vamsi
相关产品推荐
相关产品推荐

