Terraform - could not delete loadbalancer when its part of a endpoint service

Viewed 1952

After making some changes to end_point service like for example adding a new tag, network load balancer gets attempted to deleted first when running terraform apply and it doesn't succeed since NLB is associated with endpoint_service.

Endpoint service should be the first to get deleted so the network loadbalancer should get deleted later.

Is there a way to set which should get deleted first?

module.Tester_vpc.data.aws_instances.webservers: Refreshing state...

Error: Error deleting LB: ResourceInUse: Load balancer 'arn:aws:elasticloadbalancing:ap-south-1:123456:loadbalancer/net/myNLB/123456' cannot be deleted because it is currently associated with another service
        status code: 400, request id: 25944b2d-49c7-1234-a32c-faeb6e2e7c7f

Here is the NLB resources.

resource "aws_vpc_endpoint_service" "nlb_service" {

  count = var.create_lb ? 1 : 0
  acceptance_required = false
  network_load_balancer_arns = [aws_lb.myNLB[0].arn]
}

resource "aws_vpc_endpoint" "service_consumer" {

  count = var.create_lb ? 1 : 0
  vpc_id = data.aws_vpc.vpc_id.id
  subnet_ids = data.aws_subnet_ids.private_subnet_ids.ids
  security_group_ids = [data.aws_security_group.sG_myVPC.id]
  vpc_endpoint_type   = "Interface"
  private_dns_enabled = false
  service_name        = aws_vpc_endpoint_service.nlb_service[0].service_name

  tags = {
    Name = "tester_service" # When adding a tag, NLB attemps get deleted first and fails.
  }
}
1 Answers
Related