我正在尝试使用 Terraform 使用应用程序负载均衡器设置 AWS ECS 集群。由于某种原因,我遇到了错误:
Error: InvalidParameterException: The load balancer example-production-alb does not exist. "example-production-service"
以下是我负责负载平衡的配置:
resource "aws_alb" "example-production-alb" {
name = "example-production-alb"
internal = false
load_balancer_type = "application"
subnets = var.SUBNETS_IDS
security_groups = [aws_security_group.example-alb-securitygroup.id]
tags = {
Name = "example-saas-production-alb"
}
}
resource "aws_alb_target_group" "example-production-tg" {
name = "example-production-tg"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = var.VPC_ID
}
resource "aws_alb_listener" "nginx-listeners" {
load_balancer_arn = aws_alb.example-production-alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_alb_target_group.example-production-tg.arn
}
}
我在服务地形定义中引用了我的负载均衡器:
resource "aws_ecs_service" "example-production-service" {
name = "example-production-service"
cluster = aws_ecs_cluster.example-production.id
task_definition = aws_ecs_task_definition.example-production-task-definition.arn
desired_count = 1
iam_role = aws_iam_role.ecs-service-role.arn
depends_on = [aws_iam_policy_attachment.ecs-service-attach]
load_balancer {
elb_name = aws_alb.example-production-alb.name
container_name = aws_ecs_task_definition.example-production-task-definition.family
container_port = 80
}
lifecycle {
ignore_changes = [task_definition]
}
}
我可以在 UI 中看到负载均衡器在那里。 我真的很感激任何帮助。我错过了什么?
我相信在 ALB 的情况下,您需要提供target_group_arn
而不是elb_name
:
load_balancer {
target_group_arn = aws_alb_target_group.example-production-tg.arn
container_name = aws_ecs_task_definition.example-production-task-definition.family
container_port = 80
}