Hi, I’m experiencing some issues with my infra. I use terraform to create load balancers, target groups etc… My issue is floating, I can’t say it rises every time I do the destroy procedure. But sometimes it ends up with an error:
Error: error deleting Listener (arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/app/forward-alb/e5a83c3fb5c0b858/caf1a2fe3e479773): ResourceInUse: Listener port ‘80’ is in use by registered target ‘arn:aws:elasticloadbalancing:us-east-1:299274019362:loadbalancer/app/forward-alb/e5a83c3fb5c0b858’ and cannot be removed.
│ status code: 400, request id: b8017cd7-47ee-4d25-9bfe-c4ef299a726b
And i have no idea why does it try to remove listener before it removes the target group. I have some dependencies in my code, but it seems that it doesn’t take an effect on.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only ‘yes’ will be accepted to confirm.Enter a value: yes
random_pet.rand_prefix: Destroying… [id=unified-mullet]
random_pet.rand_prefix: Destruction complete after 0s
aws_lb_target_group_attachment.NLB-to-ALB-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/NLB-to-ALB-80/b5d7df7356570933-20231210205843608300000006]
aws_appautoscaling_policy.instance: Destroying… [id=ecs-cpu-auto-scaling]
aws_security_group_rule.aibb-security-group-in: Destroying… [id=sgrule-2548336480]
aws_cloudwatch_log_group.logs[“jupyterhub”]: Destroying… [id=/aws/ecs/jupyterhub]
aws_security_group_rule.aibb-security-group-out: Destroying… [id=sgrule-1015643485]
aws_route.internet_gateway_route[“rtb-0f7b3ccb28a364ca1”]: Destroying… [id=r-rtb-0f7b3ccb28a364ca11080289494]
aws_iam_role_policy_attachment.AmazonSSMFullAccess: Destroying… [id=ecs-execution-role-production-20231210205540317100000002]
aws_cloudwatch_log_group.logs[“jupyterlab”]: Destroying… [id=/aws/ecs/jupyterlab]
aws_cloudwatch_log_group.logs[“aibb-deploy”]: Destroying… [id=/aws/ecs/aibb-deploy]
aws_lb_listener_rule.alb-listener-aibb-deploy-rule-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener-rule/app/forward-alb/e5a83c3fb5c0b858/b3a4e1aa36fa45c9/f0d78b3726edd177]
aws_cloudwatch_log_group.logs[“aibb-deploy”]: Destruction complete after 0s
aws_cloudwatch_log_group.logs[“jupyterlab”]: Destruction complete after 0s
aws_cloudwatch_log_group.logs[“jupyterhub”]: Destruction complete after 0s
aws_ecs_service.aibb-deploy-service: Destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service]
aws_lb_listener_rule.alb-listener-jupyterhub-rule-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener-rule/app/forward-alb/e5a83c3fb5c0b858/caf1a2fe3e479773/00838c16f7901462]
aws_lb_listener.nlb-https-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/net/ingress-nlb/32bbf10a8d8379a5/98204cb8be4dff3d]
aws_iam_role_policy_attachment.AmazonSSMFullAccess: Destruction complete after 0s
aws_lb_target_group_attachment.NLB-to-ALB-80: Destruction complete after 0s
aws_lb_listener_rule.alb-listener-cvat-rule-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener-rule/app/forward-alb/e5a83c3fb5c0b858/b3a4e1aa36fa45c9/033f6f52e807487c]
aws_ecs_task_definition.jupyterlab-ecs-task-definition: Destroying… [id=jupyterlab-ecs-task-definition]
aws_lb_listener_rule.alb-listener-aibb-deploy-rule-443: Destruction complete after 0s
aws_iam_role_policy_attachment.AmazonECSTaskExecutionRolePolicy: Destroying… [id=ecs-execution-role-production-20231210205540308100000001]
aws_ecs_task_definition.jupyterlab-ecs-task-definition: Destruction complete after 0s
aws_lb_listener_rule.alb-listener-jupyterhub-rule-80: Destruction complete after 0s
aws_lb_target_group_attachment.NLB-to-ALB-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/NLB-to-ALB-443/f32eb73a42694a2e-20231210205843739600000007]
aws_lb_listener.nlb-http-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/net/ingress-nlb/32bbf10a8d8379a5/06be728ddd65f71c]
aws_lb_listener.nlb-https-443: Destruction complete after 1s
aws_lb_target_group_attachment.cvat-to-tg-8080: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/cvat-tg-80/e68f29bb52f342b7-20231210205603239700000005]
aws_lb_listener_rule.alb-listener-cvat-rule-443: Destruction complete after 1s
aws_iam_role_policy_attachment.AmazonECSTaskExecutionRolePolicy: Destruction complete after 1s
aws_cloudwatch_log_group.logs[“aibb-django”]: Destroying… [id=/aws/ecs/aibb-django]
aws_lb_listener_rule.alb-listener-jupyterhub-rule-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener-rule/app/forward-alb/e5a83c3fb5c0b858/b3a4e1aa36fa45c9/639f8bbb64c5d714]
aws_lb_target_group_attachment.NLB-to-ALB-443: Destruction complete after 0s
aws_iam_instance_profile.EcsInstanceProfile: Destroying… [id=EcsInstanceProfile-production]
aws_cloudwatch_log_group.logs[“aibb-django”]: Destruction complete after 0s
aws_cloudwatch_log_group.logs[“aibb-django-gpu”]: Destroying… [id=/aws/ecs/aibb-django-gpu]
aws_security_group_rule.aibb-security-group-out: Destruction complete after 1s
aws_lb_target_group_attachment.cvat-to-tg-8080: Destruction complete after 0s
aws_lb_listener.alb-http-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/app/forward-alb/e5a83c3fb5c0b858/caf1a2fe3e479773]
aws_lb_listener.nlb-http-80: Destruction complete after 0s
aws_lb_target_group.NLB-to-ALB-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/NLB-to-ALB-443/f32eb73a42694a2e]
aws_cloudwatch_log_group.logs[“aibb-django-gpu”]: Destruction complete after 0s
aws_lb_listener_rule.alb-listener-jupyterhub-rule-443: Destruction complete after 0s
aws_lb_target_group.cvat-tg-8080: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/cvat-tg-80/e68f29bb52f342b7]
aws_appautoscaling_policy.instance: Destruction complete after 1s
aws_instance.cvat-ec2-instance: Destroying… [id=i-01f6bb9ede2cc0456]
aws_lb_target_group.NLB-to-ALB-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/NLB-to-ALB-80/b5d7df7356570933]
aws_route.internet_gateway_route[“rtb-0f7b3ccb28a364ca1”]: Destruction complete after 1s
aws_lb.ingress-nlb: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:loadbalancer/net/ingress-nlb/32bbf10a8d8379a5]
aws_lb_target_group.NLB-to-ALB-443: Destruction complete after 0s
aws_iam_instance_profile.EcsInstanceProfile: Destruction complete after 0s
aws_lb_listener.alb-http-443: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/app/forward-alb/e5a83c3fb5c0b858/b3a4e1aa36fa45c9]
aws_appautoscaling_target.instance: Destroying… [id=service/aibb-cluster/jupyterhub-service]
aws_lb_target_group.cvat-tg-8080: Destruction complete after 0s
aws_lb_target_group.NLB-to-ALB-80: Destruction complete after 0s
aws_lb_listener.alb-http-443: Destruction complete after 0s
aws_acm_certificate.lab-aibb-ai: Destroying… [id=arn:aws:acm:us-east-1:299274019362:certificate/c8adc157-1320-42bf-b841-d1be74a88a4d]
aws_appautoscaling_target.instance: Destruction complete after 0s
aws_ecs_service.jupyterhub-service: Destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service]
aws_security_group_rule.aibb-security-group-in: Destruction complete after 1s
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 10s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 10s elapsed]
aws_lb.ingress-nlb: Still destroying… [id=arn:aws:elasticloadbalancing:us-east-1:…ancer/net/ingress-nlb/32bbf10a8d8379a5, 10s elapsed]
aws_acm_certificate.lab-aibb-ai: Still destroying… [id=arn:aws:acm:us-east-1:299274019362:cert…e/c8adc157-1320-42bf-b841-d1be74a88a4d, 10s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 10s elapsed]
aws_acm_certificate.lab-aibb-ai: Destruction complete after 16s
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 20s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 20s elapsed]
aws_lb.ingress-nlb: Still destroying… [id=arn:aws:elasticloadbalancing:us-east-1:…ancer/net/ingress-nlb/32bbf10a8d8379a5, 20s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 20s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 30s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 30s elapsed]
aws_lb.ingress-nlb: Still destroying… [id=arn:aws:elasticloadbalancing:us-east-1:…ancer/net/ingress-nlb/32bbf10a8d8379a5, 30s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 30s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 40s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 40s elapsed]
aws_lb.ingress-nlb: Still destroying… [id=arn:aws:elasticloadbalancing:us-east-1:…ancer/net/ingress-nlb/32bbf10a8d8379a5, 40s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 40s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 50s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 50s elapsed]
aws_lb.ingress-nlb: Still destroying… [id=arn:aws:elasticloadbalancing:us-east-1:…ancer/net/ingress-nlb/32bbf10a8d8379a5, 50s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 50s elapsed]
aws_lb.ingress-nlb: Destruction complete after 57s
aws_eip.nlb-public-eip: Destroying… [id=eipalloc-0bc34ecef24dfc67a]
aws_eip.nlb-public-eip: Destruction complete after 0s
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m0s elapsed]
aws_instance.cvat-ec2-instance: Still destroying… [id=i-01f6bb9ede2cc0456, 1m0s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m0s elapsed]
aws_instance.cvat-ec2-instance: Destruction complete after 1m0s
aws_key_pair.ansible: Destroying… [id=ansible.softosource.com]
aws_key_pair.ansible: Destruction complete after 0s
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m10s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m10s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m20s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m20s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m30s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m30s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m40s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m40s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 1m50s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 1m50s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 2m0s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 2m0s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 2m10s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 2m10s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 2m20s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 2m20s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 2m30s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 2m30s elapsed]
aws_ecs_service.aibb-deploy-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/aibb-deploy-service, 2m40s elapsed]
aws_ecs_service.jupyterhub-service: Still destroying… [id=arn:aws:ecs:us-east-1:299274019362:service/aibb-cluster/jupyterhub-service, 2m40s elapsed]
aws_ecs_service.aibb-deploy-service: Destruction complete after 2m47s
aws_lb_target_group.aibb-deploy-tg-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/aibb-deploy-tg-80/7e346a526167845c]
aws_ecs_task_definition.aibb-deploy-ecs-task-definition: Destroying… [id=aibb-deploy-ecs-task-definition]
aws_ecs_task_definition.aibb-deploy-ecs-task-definition: Destruction complete after 0s
aws_lb_target_group.aibb-deploy-tg-80: Destruction complete after 0s
aws_ecs_service.jupyterhub-service: Destruction complete after 2m46s
aws_ecs_cluster.aibb-cluster: Destroying… [id=arn:aws:ecs:us-east-1:299274019362:cluster/aibb-cluster]
aws_lb_target_group.jupyterhub-tg-80: Destroying… [id=arn:aws:elasticloadbalancing:us-east-1:299274019362:targetgroup/jupyterhub-tg-80/b5b23ba45cbb31a7]
aws_ecs_task_definition.jupyterhub-task-definition: Destroying… [id=jupyterhub-ecs-task-definition]
aws_ecs_task_definition.jupyterhub-task-definition: Destruction complete after 0s
aws_ecs_task_definition.aibb-django-ecs-task-definition: Destroying… [id=aibb-django-ecs-task-definition]
aws_lb_target_group.jupyterhub-tg-80: Destruction complete after 0s
aws_ecs_task_definition.aibb-django-ecs-task-definition: Destruction complete after 0s
aws_iam_role.ecs_execution_role: Destroying… [id=ecs-execution-role-production]
aws_ecs_cluster.aibb-cluster: Destruction complete after 0s
aws_iam_role.ecs_execution_role: Destruction complete after 0s│ Error: error deleting Listener (arn:aws:elasticloadbalancing:us-east-1:299274019362:listener/app/forward-alb/e5a83c3fb5c0b858/caf1a2fe3e479773): ResourceInUse: Listener port ‘80’ is in use by registered target ‘arn:aws:elasticloadbalancing:us-east-1:299274019362:loadbalancer/app/forward-alb/e5a83c3fb5c0b858’ and cannot be removed.
> │ status code: 400, request id: b8017cd7-47ee-4d25-9bfe-c4ef299a726b
│
│
My actual terraform script contains:
resource “aws_lb_target_group” “NLB-to-ALB-80” {
name = “NLB-to-ALB-80”
target_type = “alb”
protocol = “TCP”
port = 80
vpc_id = data.aws_vpc.default.id
#deregistration_delay = 30
preserve_client_ip = “true”
proxy_protocol_v2 = “false”
slow_start = “30”stickiness {
cookie_duration = “0”
enabled = “false”
type = “source_ip”
}health_check {
enabled = “true”
healthy_threshold = “3”
interval = “30”
matcher = “200,403”
path = “/”
port = “traffic-port”
protocol = “HTTP”
timeout = “6”
unhealthy_threshold = “3”
}depends_on = [aws_lb.forward-alb]
}resource “aws_lb_target_group” “NLB-to-ALB-443” {
name = “NLB-to-ALB-443”
target_type = “alb”
protocol = “TCP”
port = 443
vpc_id = data.aws_vpc.default.id
#deregistration_delay = 30
preserve_client_ip = “true”
proxy_protocol_v2 = “false”
slow_start = “30”stickiness {
cookie_duration = “0”
enabled = “false”
type = “source_ip”
}health_check {
enabled = “true”
healthy_threshold = “3”
interval = “30”
matcher = “200,400-599”
path = “/”
port = “traffic-port”
protocol = “HTTP”
timeout = “6”
unhealthy_threshold = “3”
}depends_on = [aws_lb.forward-alb]
}resource “aws_lb_target_group” “jupyterhub-tg-80” {
name = “jupyterhub-tg-80”
target_type = “ip”
protocol = “HTTP”
protocol_version = “HTTP1”
port = 8000
vpc_id = data.aws_vpc.default.id
#deregistration_delay = 30
load_balancing_algorithm_type = “round_robin”
slow_start = “30”stickiness {
cookie_duration = “86400”
enabled = “false”
type = “lb_cookie”
}health_check {
enabled = “true”
healthy_threshold = “5”
interval = “30”
matcher = “200,302”
path = “/”
port = “traffic-port”
protocol = “HTTP”
timeout = “5”
unhealthy_threshold = “2”
}
}resource “aws_lb_target_group” “aibb-deploy-tg-80” {
name = “aibb-deploy-tg-80”
target_type = “ip”
protocol = “HTTP”
protocol_version = “HTTP1”
port = 8880
vpc_id = data.aws_vpc.default.id
#deregistration_delay = 30
load_balancing_algorithm_type = “round_robin”
slow_start = “30”stickiness {
cookie_duration = “86400”
enabled = “false”
type = “lb_cookie”
}health_check {
enabled = “true”
healthy_threshold = “5”
interval = “30”
matcher = “200,302”
path = “/”
port = “traffic-port”
protocol = “HTTP”
timeout = “5”
unhealthy_threshold = “2”
}
}resource “aws_lb_target_group” “cvat-tg-8080” {
name = “cvat-tg-80”
target_type = “ip”
protocol = “HTTP”
protocol_version = “HTTP1”
port = 8080
vpc_id = data.aws_vpc.default.id
#deregistration_delay = 30
load_balancing_algorithm_type = “round_robin”
slow_start = “30”stickiness {
cookie_duration = “86400”
enabled = “false”
type = “lb_cookie”
}health_check {
enabled = “true”
healthy_threshold = “5”
interval = “30”
matcher = “200,404”
path = “/”
port = “traffic-port”
protocol = “HTTP”
timeout = “5”
unhealthy_threshold = “2”
}
}resource “aws_lb_target_group_attachment” “NLB-to-ALB-80” {
target_group_arn = aws_lb_target_group.NLB-to-ALB-80.arn
target_id = aws_lb.forward-alb.arndepends_on = [aws_lb_target_group.NLB-to-ALB-80, aws_lb.forward-alb]
}resource “aws_lb_target_group_attachment” “NLB-to-ALB-443” {
target_group_arn = aws_lb_target_group.NLB-to-ALB-443.arn
target_id = aws_lb.forward-alb.arndepends_on = [aws_lb_target_group.NLB-to-ALB-443, aws_lb.forward-alb]
}
After the second run of terraform destroy - it finally cleans out all resources it created before.