I will create aws_lb and aws_lb_target_group by using Terraform, then create an ECS Service. Here are my codes:
resource "aws_lb" "my_lb" {
name = "my-lb"
internal = false
load_balancer_type = "application"
security_groups = [data.aws_security_group.existing_opensearch_sg.id]
subnets = var.public_subnet_ids
enable_deletion_protection = false
tags = {
component = "my"
env = terraform.workspace
team = "xxx"
}
}
resource "aws_lb_target_group" "my_target_group" {
name = "my-target-group"
port = 1234
protocol = "HTTP"
vpc_id = var.vpc_id
health_check {
path = "/health"
protocol = "HTTP"
}
tags = {
component = "my"
env = terraform.workspace
team = "xxx"
}
target_type = "ip"
}
resource "aws_ecs_service" "my_service" {
name = "my-service"
launch_type = "FARGATE"
desired_count = 1
cluster = aws_ecs_cluster.my_cluster.id
task_definition = aws_ecs_task_definition.my_task.arn
network_configuration {
subnets = var.private_subnet_ids
security_groups = [
data.aws_security_group.existing_opensearch_sg.id
]
assign_public_ip = false
}
load_balancer {
target_group_arn = aws_lb_target_group.my_target_group.arn
container_name = "my-container"
container_port = 1234
}
tags = {
component = "my"
env = terraform.workspace
team = "xxx"
}
}
**When I run terraform apply, it reports an error:
**
╷
│ Error: creating ECS Service (my-service): InvalidParameterException: The target group with targetGroupArn arn:***********(I hide my arn)********* does not have an associated load balancer.
│
│ with aws_ecs_service.my_service,
│ on my.tf line 87, in resource "aws_ecs_service" "my_service":
│ 56: resource "aws_ecs_service" "my_service" {
│
Then I go to AWS, it was found that both lb and target group were created, but the target group’s
Load Balance column shows “None associated”.
I’m a newbie to aws and terraform, can you all help me out?
Asked Gpt but got no reply