kubernetes/kops · error

failed to detach target groups from autoscaling group: %v

Error message

failed to detach target groups from autoscaling group: %v

What it means

A deferred cloud-deletion task failed to detach a target group from the autoscaling group (DetachLoadBalancerTargetGroups); the target group may remain attached after the apply, and the wrapped error is the AWS ELBV2/ASG API response.

Source

Thrown at upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go:1094

	d.autoScalingGroupName = autoScalingGroupName
	d.targetGroupARN = targetGroupARN
	return d
}

func (d *deleteAutoscalingTargetGroupAttachment) Delete(t fi.CloudupTarget) error {
	ctx := context.TODO()

	awsTarget, ok := t.(*awsup.AWSAPITarget)
	if !ok {
		return fmt.Errorf("unexpected target type for deletion: %T", t)
	}

	req := &autoscaling.DetachLoadBalancerTargetGroupsInput{
		AutoScalingGroupName: aws.String(d.autoScalingGroupName),
		TargetGroupARNs:      []string{d.targetGroupARN},
	}
	if _, err := awsTarget.Cloud.Autoscaling().DetachLoadBalancerTargetGroups(ctx, req); err != nil {
		return fmt.Errorf("failed to detach target groups from autoscaling group: %v", err)
	}

	return nil
}

func (d *deleteAutoscalingTargetGroupAttachment) TaskName() string {
	return "autoscaling-elb-attachment"

}
func (d *deleteAutoscalingTargetGroupAttachment) Item() string {
	return d.autoScalingGroupName + ":" + d.targetGroupARN
}

func (d *deleteAutoscalingTargetGroupAttachment) DeferDeletion() bool {
	return true
}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. If the wrapped error says the ASG or target group does not exist, the resource is already gone — re-run the delete/refresh state to clear the stale task.
  2. Verify IAM permissions include autoscaling:DetachLoadBalancerTargetGroups.
  3. Retry after backoff if throttled; deletion flows often race on AWS rate limits.
  4. Detach manually via `aws autoscaling detach-load-balancer-target-groups` if kops state is wedged, then re-run kops delete.
Defensive patterns

Strategy: retry

Validate before calling

// verify ASG and target group still exist before detaching
if _, err := svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: []string{asgName}}); err != nil {
  return nil // ASG already gone; nothing to detach
}
_, err := elbv2Svc.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{TargetGroupArns: []string{arn}})

Try / catch

if _, err := svc.DetachLoadBalancerTargetGroups(ctx, req); err != nil {
  if isNotFound(err) {
    klog.V(2).Infof("ASG/target group already removed; treating delete as done")
    return nil
  }
  if isThrottle(err) {
    return retryWithBackoff(req)
  }
  return fmt.Errorf("failed to detach target groups from autoscaling group: %v", err)
}

Prevention

When it happens

Trigger: deleteAutoscalingTargetGroupAttachment.Delete builds DetachLoadBalancerTargetGroupsInput with the ASG name and target group ARN and the AWS call fails: ASG not found (already deleted), target group not attached or ARN no longer exists, throttling, or missing autoscaling:DetachLoadBalancerTargetGroups permission.

Common situations: kops delete cluster where the ASG was already removed out-of-band; NLB/target group deleted first so detach is invalid; permission revoked; throttling during parallel teardown.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/046b8066057fe32d. Report an issue: GitHub.