title: ECS scale in with capacity provider to minimum capacity of ASG
body: <p>I have ECS cluster configured with target tracking policy on service and capacity provider which managing ASG autoscaling</p>
  <p>In my cluster count of minimum and maximum tasks  in service and minimum and maximum capacity in ASG the same .</p>
  <p>When performed scale in action, tasks decreased to minimum count. But ASG still have 1 or more unused ( task not placed on this EC2 instance ) ec2 instance</p>
  <p>How can i configure my cluster with capacity provider to perform scale in to minimum count ASG capacity ?</p>
  <pre><code>
  # CLUSTER
  resource &quot;aws_ecs_cluster&quot; &quot;default&quot; {
    name               = local.name
    capacity_providers = [aws_ecs_capacity_provider.asg.name]
    tags               = local.tags
  
    default_capacity_provider_strategy {
      base = 0
      capacity_provider = aws_ecs_capacity_provider.asg.name
      weight = 1
    }
  }
  
  # SERVICE
  resource &quot;aws_ecs_service&quot; &quot;ecs_service&quot; {
    name            = &quot;${local.name}-service&quot;
    cluster         = aws_ecs_cluster.default.id
    task_definition = aws_ecs_task_definition.ecs_task.arn
    health_check_grace_period_seconds = 60
  
    deployment_maximum_percent         = 50
    deployment_minimum_healthy_percent = 100
  
  
    load_balancer {
      target_group_arn = element(module.aws-alb-common-module.target_group_arns, 1)
      container_name   = local.name
      container_port   = 8080
    }
  
    lifecycle {
      ignore_changes = [desired_count, task_definition]
    }
  
  
  }
  
  # CAPACITY PROVIDER
  resource &quot;aws_ecs_capacity_provider&quot; &quot;asg&quot; {
    name = aws_autoscaling_group.ecs_nodes.name
  
    auto_scaling_group_provider {
      auto_scaling_group_arn         = aws_autoscaling_group.ecs_nodes.arn
      managed_termination_protection = &quot;DISABLED&quot;
  
      managed_scaling {
        maximum_scaling_step_size = 10
        minimum_scaling_step_size = 1
        status                    = &quot;ENABLED&quot;
        target_capacity           = 100
      }
    }
  }
  
  # SERVICE AUTOSCALING POLICY
  
  resource &quot;aws_appautoscaling_target&quot; &quot;ecs_target&quot; {
    max_capacity       = 20
    min_capacity       = 2
    resource_id        = &quot;service/${local.name}/${aws_ecs_service.ecs_service.name}&quot;
    scalable_dimension = &quot;ecs:service:DesiredCount&quot;
    service_namespace  = &quot;ecs&quot;
  }
  
  resource &quot;aws_appautoscaling_policy&quot; &quot;ecs_policy&quot; {
    name = &quot;${local.name}-scale-policy&quot;
    policy_type = &quot;TargetTrackingScaling&quot;
    resource_id = aws_appautoscaling_target.ecs_target.resource_id
    scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension
    service_namespace = aws_appautoscaling_target.ecs_target.service_namespace
  
    target_tracking_scaling_policy_configuration {
      predefined_metric_specification {
        predefined_metric_type = &quot;ECSServiceAverageCPUUtilization&quot;
      }
  
      target_value = 2
  
    }
  
  
  # ASG
  resource &quot;aws_autoscaling_group&quot; &quot;ecs_nodes&quot; {
    name_prefix           = &quot;${local.name}-node&quot;
    max_size              = 20
    min_size              = 2
    vpc_zone_identifier   = local.subnets_ids
    protect_from_scale_in = false
  
    mixed_instances_policy {
      instances_distribution {
        on_demand_percentage_above_base_capacity = local.spot
      }
      launch_template {
        launch_template_specification {
          launch_template_id = aws_launch_template.node.id
          version            = &quot;$Latest&quot;
        }
  
        dynamic &quot;override&quot; {
          for_each = local.instance_types
          content {
            instance_type     = override.key
            weighted_capacity = override.value
          }
        }
      }
    }
  
    lifecycle {
      create_before_destroy = true
    }
  
    tag {
      key                 = &quot;AmazonECSManaged&quot;
      propagate_at_launch = true
      value               = &quot;&quot;
    }
  }
  
  
  
  
  
  
  
  </code></pre>