# ScalableTaskCount

The scalable attribute representing task count.

Extends: [Construct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Construct.html) › [BaseScalableAttribute](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_applicationautoscaling.BaseScalableAttribute.html) › ScalableTaskCount
Implements: [IEnvironmentAware](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.IEnvironmentAware.html), [IScalableTargetRef](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_applicationautoscaling.IScalableTargetRef.html), [IConstruct](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IConstruct.html), [IDependable](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IDependable.html)
Returned by: [BaseService.autoScaleTaskCount](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.BaseService.html#autoscaletaskcount)

- Source: [`aws-ecs/lib/base/scalable-task-count.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ecs/lib/base/scalable-task-count.ts#L16)

## Example

TypeScript:

```ts
declare const cluster: ecs.Cluster;
const loadBalancedFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'Service', {
  cluster,
  memoryLimitMiB: 1024,
  desiredCount: 1,
  cpu: 512,
  taskImageOptions: {
    image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
  },
  minHealthyPercent: 100,
});

const scalableTarget = loadBalancedFargateService.service.autoScaleTaskCount({
  minCapacity: 1,
  maxCapacity: 20,
});

scalableTarget.scaleOnCpuUtilization('CpuScaling', {
  targetUtilizationPercent: 50,
});

scalableTarget.scaleOnMemoryUtilization('MemoryScaling', {
  targetUtilizationPercent: 50,
});
```

Python:

```python
# cluster: ecs.Cluster

load_balanced_fargate_service = ecs_patterns.ApplicationLoadBalancedFargateService(self, "Service",
    cluster=cluster,
    memory_limit_mi_b=1024,
    desired_count=1,
    cpu=512,
    task_image_options=ecsPatterns.ApplicationLoadBalancedTaskImageOptions(
        image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")
    ),
    min_healthy_percent=100
)

scalable_target = load_balanced_fargate_service.service.auto_scale_task_count(
    min_capacity=1,
    max_capacity=20
)

scalable_target.scale_on_cpu_utilization("CpuScaling",
    target_utilization_percent=50
)

scalable_target.scale_on_memory_utilization("MemoryScaling",
    target_utilization_percent=50
)
```

Java:

```java
Cluster cluster;

ApplicationLoadBalancedFargateService loadBalancedFargateService = ApplicationLoadBalancedFargateService.Builder.create(this, "Service")
        .cluster(cluster)
        .memoryLimitMiB(1024)
        .desiredCount(1)
        .cpu(512)
        .taskImageOptions(ApplicationLoadBalancedTaskImageOptions.builder()
                .image(ContainerImage.fromRegistry("amazon/amazon-ecs-sample"))
                .build())
        .minHealthyPercent(100)
        .build();

ScalableTaskCount scalableTarget = loadBalancedFargateService.service.autoScaleTaskCount(EnableScalingProps.builder()
        .minCapacity(1)
        .maxCapacity(20)
        .build());

scalableTarget.scaleOnCpuUtilization("CpuScaling", CpuUtilizationScalingProps.builder()
        .targetUtilizationPercent(50)
        .build());

scalableTarget.scaleOnMemoryUtilization("MemoryScaling", MemoryUtilizationScalingProps.builder()
        .targetUtilizationPercent(50)
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Cluster cluster;

var loadBalancedFargateService = new ApplicationLoadBalancedFargateService(this, "Service", new ApplicationLoadBalancedFargateServiceProps {
    Cluster = cluster,
    MemoryLimitMiB = 1024,
    DesiredCount = 1,
    Cpu = 512,
    TaskImageOptions = new ApplicationLoadBalancedTaskImageOptions {
        Image = ContainerImage.FromRegistry("amazon/amazon-ecs-sample")
    },
    MinHealthyPercent = 100
});

var scalableTarget = loadBalancedFargateService.Service.AutoScaleTaskCount(new EnableScalingProps {
    MinCapacity = 1,
    MaxCapacity = 20
});

scalableTarget.ScaleOnCpuUtilization("CpuScaling", new CpuUtilizationScalingProps {
    TargetUtilizationPercent = 50
});

scalableTarget.ScaleOnMemoryUtilization("MemoryScaling", new MemoryUtilizationScalingProps {
    TargetUtilizationPercent = 50
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var cluster Cluster

loadBalancedFargateService := ecsPatterns.NewApplicationLoadBalancedFargateService(this, jsii.String("Service"), &ApplicationLoadBalancedFargateServiceProps{
	Cluster: Cluster,
	MemoryLimitMiB: jsii.Number(1024),
	DesiredCount: jsii.Number(1),
	Cpu: jsii.Number(512),
	TaskImageOptions: &ApplicationLoadBalancedTaskImageOptions{
		Image: ecs.ContainerImage_FromRegistry(jsii.String("amazon/amazon-ecs-sample")),
	},
	MinHealthyPercent: jsii.Number(100),
})

scalableTarget := loadBalancedFargateService.Service.AutoScaleTaskCount(&EnableScalingProps{
	MinCapacity: jsii.Number(1),
	MaxCapacity: jsii.Number(20),
})

scalableTarget.ScaleOnCpuUtilization(jsii.String("CpuScaling"), &CpuUtilizationScalingProps{
	TargetUtilizationPercent: jsii.Number(50),
})

scalableTarget.ScaleOnMemoryUtilization(jsii.String("MemoryScaling"), &MemoryUtilizationScalingProps{
	TargetUtilizationPercent: jsii.Number(50),
})
```

## Initializer

TypeScript:

```ts
new ScalableTaskCount(scope: Construct, id: string, props: ScalableTaskCountProps)
```

Python:

```python
ScalableTaskCount(scope: Construct, id: str, props: ScalableTaskCountProps)
```

Java:

```java
new ScalableTaskCount(Construct scope, String id, ScalableTaskCountProps props)
```

C# / .NET:

```csharp
new ScalableTaskCount(Construct scope, string id, ScalableTaskCountProps props)
```

Golang:

```go
NewScalableTaskCount(Scope Construct, Id *string, Props ScalableTaskCountProps)
```

## Construct Props

- `dimension`: string (required) — Scalable dimension of the attribute.

- `maxCapacity`: number (required) — Maximum capacity to scale to.

- `minCapacity`: number = 1 — Minimum capacity to scale to.

- `resourceId`: string (required) — Resource ID of the attribute.

- `role`: [IRole](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) (required) — Role to use for scaling.

- `serviceNamespace`: [ServiceNamespace](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_applicationautoscaling.ServiceNamespace.html) (required) — Service namespace of the scalable attribute.

## Properties

- `env`: [ResourceEnvironment](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `node`: [Node](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `scalableTargetRef`: [ScalableTargetReference](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_applicationautoscaling.ScalableTargetReference.html) — A reference to a ScalableTarget resource.

## Methods

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `scaleOnCpuUtilization(id: string, props: CpuUtilizationScalingProps): void` — Scales in or out to achieve a target CPU utilization.

- `scaleOnMemoryUtilization(id: string, props: MemoryUtilizationScalingProps): void` — Scales in or out to achieve a target memory utilization.

- `scaleOnMetric(id: string, props: BasicStepScalingPolicyProps): void` — Scales in or out based on a specified metric value.

- `scaleOnRequestCount(id: string, props: RequestCountScalingProps): void` — Scales in or out to achieve a target Application Load Balancer request count per target.

- `scaleOnSchedule(id: string, props: ScalingSchedule): void` — Scales in or out based on a specified scheduled time.

- `scaleToTrackCustomMetric(id: string, props: TrackCustomMetricProps): void` — Scales in or out to achieve a target on a custom metric.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
