Terraform创建ALB双目标组报错:未声明aws_alb_target_group资源
Terraform ALB模块中Target Group资源未声明错误排查
我使用Terraform模块化编写脚本,想要为ALB创建前端、后端两个不同的Target Group,但执行时却报错提示这些资源未在module.alb中声明。以下是相关代码、变量配置、根模块调用代码、报错信息及目录结构,请求协助排查问题:
alb/main.tf
resource "aws_lb" "main" { name = "${var.name}-alb-${var.environment}" internal = false load_balancer_type = "application" security_groups = var.alb_security_groups subnets = var.subnets.*.id enable_deletion_protection = false tags = { Name = "${var.name}-alb-${var.environment}" Environment = var.environment } } resource "aws_alb_target_group" "frontend" { name = "${var.name}-tg-${var.environment}" port = 3000 protocol = "HTTP" vpc_id = var.vpc_id target_type = "ip" health_check { healthy_threshold = "3" interval = "30" protocol = "HTTP" matcher = "200" timeout = "3" path = var.health_check_front unhealthy_threshold = "2" } tags = { Name = "${var.name}-tg-${var.environment}" Environment = var.environment } } resource "aws_alb_target_group" "backend" { name = "${var.name}-tg-${var.environment}" port = 5000 protocol = "HTTP" vpc_id = var.vpc_id target_type = "ip" health_check { healthy_threshold = "3" interval = "30" protocol = "HTTP" matcher = "200" timeout = "3" path = var.health_check_back unhealthy_threshold = "2" } tags = { Name = "${var.name}-tg-${var.environment}" Environment = var.environment } } resource "aws_alb_listener" "frontend_listener" { load_balancer_arn = aws_lb.main.id port = 443 protocol = "HTTP" default_action { type = "forward" target_group_arn = aws_lb_target_group.frontend.arn } } resource "aws_alb_listener" "backend_listener" { load_balancer_arn = aws_lb.main.id port = 444 protocol = "HTTPS" ssl_policy = "ELBSecurityPolicy-2016-08" certificate_arn = var.alb_tls_cert_arn default_action { type = "forward" target_group_arn = aws_lb_target_group.backend.arn } } output "aws_alb_target_group_front" { value = aws_alb_target_group.frontend.arn } output "aws_alb_target_group_back" { value = aws_alb_target_group.backend.arn } output "alb_arn" { value = aws_lb.main.id }
alb/variables.tf
variable "name" { description = "g4gov external ALB" default = "g4gov" } variable "environment" { description = "dev" } variable "subnets" { description = "Public CIDR's list" default = ["10.0.16.0/24", "10.0.48.0/24"] } variable "vpc_id" { description = "VPC ID" } variable "alb_security_groups" { description = "Comma separated list of security groups" } variable "alb_tls_cert_arn" { description = "The ARN of the certificate that the ALB uses for https" default = "arn:aws:acm:eu-central-1:932935596778:certificate/ff8059b6-f9f3-4dec-893g-addgbc5ad74" } variable "health_check_front" { description = "Path to check if the service is healthy" default = "/" } variable "health_check_back" { description = "Path to check if the service is healthy" default = "/health" }
根目录main.tf
module "alb" { source = "./alb" name = var.name vpc_id = module.vpc.id subnets = module.vpc.public_subnets environment = var.environment # target_groups = [var.aws_alb_target_group_front, var.aws_alb_target_group_back] alb_security_groups = [module.security_groups.alb] alb_tls_cert_arn = var.tsl_certificate_arn health_check_front = var.health_check_front health_check_back = var.health_check_back }
报错信息
│ Error: Reference to undeclared resource │ on alb\main.tf line 71, in resource "aws_alb_listener" "frontend_listener": │ 71: target_group_arn = aws_lb_target_group.frontend.arn │ A managed resource "aws_alb_target_group" "frontend" has not been declared in module.alb.
│ Error: Reference to undeclared resource │ on alb\main.tf line 86, in resource "aws_alb_listener" "backend_listener": │ 86: target_group_arn = aws_lb_target_group.backend.arn │ A managed resource "aws_alb_target_group" "backend" has not been declared in module.alb.
目录结构
├───.terraform │ ├───modules │ │ └───route53_public_zone.public_hosted_zone │ │ └───examples │ │ ├───private-zone │ │ └───public-zone │ └───providers │ └───registry.terraform.io │ └───hashicorp │ └───aws │ └───3.65.0 │ └───windows_amd64 ├───alb ├───ecr ├───ecs ├───grafana ├───graylog ├───rds ├───route53 ├───secrects ├───security-groups └───vpc └main.tf └variables.tf └terraformtfvars.tf
排查方向及解决方法
文件与路径校验
- 确认
alb目录下的main.tf确实存在aws_alb_target_group.frontend和aws_alb_target_group.backend的完整定义,无文件丢失、重命名或拼写错误。 - 检查根模块中
source = "./alb"的路径是否正确,确保指向包含目标资源的目录。
- 确认
资源引用一致性
- 核对监听器中引用的
aws_lb_target_group.frontend.arn、aws_lb_target_group.backend.arn与资源定义的名称完全一致,无大小写或拼写偏差。
- 核对监听器中引用的
清理缓存并重初始化
- 执行以下命令清理Terraform缓存并重新初始化模块,解决缓存导致的加载异常:
rm -rf .terraform terraform init
- 执行以下命令清理Terraform缓存并重新初始化模块,解决缓存导致的加载异常:
变量与依赖校验
- 检查
aws_alb_target_group中的变量(如var.vpc_id)是否在根模块中正确传入,未正确赋值的变量可能导致资源创建失败,间接引发引用错误。 - 修正
aws_lb的subnets配置:若module.vpc.public_subnets返回的是子网ID字符串列表,需将subnets = var.subnets.*.id改为subnets = var.subnets,避免格式错误导致资源创建失败。
- 检查
版本兼容性检查
- 当前使用的AWS Provider版本为3.65.0,确认该版本支持
target_type = "ip"配置,若存在兼容性问题,可在versions.tf中指定合适的Provider版本后重新初始化。
- 当前使用的AWS Provider版本为3.65.0,确认该版本支持
内容的提问来源于stack exchange,提问作者lyubakaa
相关产品推荐
相关产品推荐

