You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform aws_alb_target_group资源故障,如何修复?

问题解决指南

核心错误修复

你的报错根源是在aws_alb_target_group资源中错误指定了provider = http——这个资源属于AWS Provider范畴,和http Provider完全无关,直接删除这一行即可解决"不支持资源类型"的错误。

其他配置问题修正

除了核心错误,你的配置还有几处不兼容/语法问题,一并修正如下:

  1. 负载均衡资源类型不匹配
    你用aws_elb创建的是经典负载均衡(CLB),但后续搭配的是应用负载均衡(ALB)专属的目标组和监听器,两者架构不兼容。需要将aws_elb替换为aws_lb(ALB对应的资源类型):
# 应用负载均衡(ALB)
resource "aws_lb" "main" {
  name               = "constructor-io-elb-tf"
  internal           = false
  load_balancer_type = "application"
  subnets            = aws_subnet.public.*.id
  security_groups    = [aws_security_group.lb.id]
}
  1. 资源引用错误
    监听器中引用负载均衡ARN的写法有误,aws.alb.main.id需改为aws_lb.main.id:
load_balancer_arn = aws_lb.main.id
  1. 健康检查字段类型错误
    健康检查中的healthy_threshold、interval等字段为数字类型,不要加引号,修正后:
health_check {
  healthy_threshold   = 2
  unhealthy_threshold = 1
  interval            = 20
  protocol            = "HTTP"
  matcher             = "200"
  timeout             = 5
  health_check_path   = var.health_check_path
}
  1. (可选)统一资源命名
    AWS Provider新版本中,aws_alb_target_group已被aws_lb_target_group替代(旧别名仍可用,但推荐使用新命名),可同步修改:
resource "aws_lb_target_group" "tg" {
  name        = "constuctor-target-group-tf"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = aws_vpc.main.id
  target_type = "ip"

  # 健康检查配置...
}

最终修正后的完整配置

# 应用负载均衡(ALB)
resource "aws_lb" "main" {
  name               = "constructor-io-elb-tf"
  internal           = false
  load_balancer_type = "application"
  subnets            = aws_subnet.public.*.id
  security_groups    = [aws_security_group.lb.id]
}

# 创建HTTP目标组
resource "aws_lb_target_group" "tg" {
  name        = "constuctor-target-group-tf"
  port        = 80
  protocol    = "HTTP"
  vpc_id      = aws_vpc.main.id
  target_type = "ip"

  health_check {
    healthy_threshold   = 2
    unhealthy_threshold = 1
    interval            = 20
    protocol            = "HTTP"
    matcher             = "200"
    timeout             = 5
    health_check_path   = var.health_check_path
  }
}

# ALB监听器:转发流量到目标组
resource "aws_lb_listener" "listener" {
  load_balancer_arn = aws_lb.main.id
  port              = var.app_port
  protocol          = "HTTP"

  default_action {
    target_group_arn = aws_lb_target_group.tg.id
    type             = "forward"
  }
}

执行步骤

  1. 保存修正后的配置文件
  2. 执行terraform init -upgrade确保Provider版本适配
  3. 执行terraform plan验证配置合法性
  4. 执行terraform apply完成资源部署

内容的提问来源于stack exchange,提问作者Nitin k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 01:25:25