When attempting to delete the resource type AWS::ElasticLoadBalancingV2::TargetGroup from CloudFormation, the resource deletion fails with the error " Target group arn is currently in use by a listener or a rule ". This document address the resolution steps for these kind of resource dependency situations.
Issue:
When deleting the CloudFormation stack, the stack deletion fails with the below error on the resource type AWS::ElasticLoadBalancingV2::TargetGroup
Resource handler returned message: "Target group 'arn:aws:elasticloadbalancing:us-east-1:123456789123:targetgroup/MyTargetGroup/jsabsanbfsn' is currently in use by a listener or a rule (Service: ElasticLoadBalancingV2, Status Code: 400, Request ID: REQUEST_ID ) (SDK Attempt Count: 1)" (RequestToken: REQUEST_TOKEN, HandlerErrorCode: InvalidRequest)
This error occurs when attempting to delete an Elastic Load Balancing (ELB) target group that is currently in use by a listener or a rule. The target group cannot be deleted because it is associated with an active listener or rule configuration within the Elastic Load Balancing service.
Resolution:
AWS Console:
- Go to the Amazon EC2 console and navigate to the "Load Balancers" section.
- Select the Elastic Load Balancer associated with the TargetGroup resource that triggered the error.
You can find the ELB arn associated with the TargetGroup using the below CLI command.
aws elbv2 describe-target-groups --target-group-arns TargetGroupArn
- Under the "Listeners" tab, identify any listeners that are currently using the TargetGroup resource.
- For each listener using the TargetGroup, either remove the TargetGroup from the listener or delete the listener entirely.
- Once all dependencies on the TargetGroup have been removed, return to the CloudFormation console.
- Initiate the DELETE operation again for the CloudFormation stack containing the TargetGroup resource.
AWS CLI:
- Find the ELB associated with the TargetGroup using below command by providing your TargetGroup Arn from the error.
aws elbv2 describe-target-groups --target-group-arns TargetGroupArn
- Once we have the ELB Arn, we need to find the listeners which are using the TargetGroup
aws elbv2 describe-listeners --load-balancer-arn ELBArn
The above command will provide information on the listeners that are associated with the ELB. From the output, please get the listener's Arn that are associated with the concern TargetGroup in the error.
- Once we have listener's details, we can delete them using the below command.
aws elbv2 delete-listener --listener-arn ListernerArn
- Alternatively, if you want to remove the TargetGroup from the listener, you can achieve this using below command.
aws elbv2 modify-listener --listener-arn ListenerArn --default-actions Type=forward,TargetGroupArn= TargetGroupArn
Note: Replace TargetGroupArn, ELBArn, ListernerArn with your actual resource Arn's.
- Once the above changes are made, please initiate the stack deletion like below
aws cloudformation delete-stack --stack-name my-stack-name
This process should allow you to successfully delete the TargetGroup resource and the entire CloudFormation stack by first removing any dependencies on the TargetGroup from other AWS resources.