【发布时间】:2026-01-06 03:25:01
【问题描述】:
我正在 ec2 实例 中进行开发,我刚刚使用 terraform 将 负载均衡器 添加到 ec2 但是现在,当我尝试访问 负载平衡器 dns 地址时,我在浏览器上收到 504 Gateway Time-out 错误消息,我还注意到 目标组 不健康,因此运行状况检查失败。
我有以下load balancer 配置:
resource "aws_lb" "alb" {
name = "backend-lb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.lb_sg.id]
idle_timeout = 60
subnets = [element(aws_subnet.public.*.id, 0), element(aws_subnet.public.*.id, 1)]
}
resource "aws_lb_target_group" "alb_target_group" {
name = "backend-tg"
port = 8000
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.main.id
health_check {
enabled = true
path = "/"
port = "8000"
protocol = "HTTP"
healthy_threshold = 3
unhealthy_threshold = 2
interval = 90
timeout = 20
matcher = "200"
}
depends_on = [aws_lb.alb]
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_target_group.arn
}
}
resource "aws_lb_target_group_attachment" "one" {
target_group_arn = aws_lb_target_group.alb_target_group.arn
target_id = aws_instance.ec2.private_ip
port = 8000
}
请注意我在aws_lb_target_group_attachment target_id 中使用了.private_ip,因为我在尝试使用.id 或.arn 时遇到了错误Error: Error registering targets with target group: ValidationError: The IP address '....foo id or arn' is not a valid IPv4 address。
我在这里做错了什么,将 负载均衡器 添加到 ec2 实例 是否合适?
【问题讨论】:
标签: amazon-web-services amazon-ec2 terraform cloud load-balancing