# EcsTask

Start a task on an ECS cluster.

Implements: [IRuleTarget](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_events.IRuleTarget.html)

- Source: [`aws-events-targets/lib/ecs-task.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-events-targets/lib/ecs-task.ts#L192)
- Type last changed: v2.235.0 · 15 Jan 2026

## Example

TypeScript:

```ts
import * as ecs from 'aws-cdk-lib/aws-ecs';

declare const cluster: ecs.ICluster;
declare const taskDefinition: ecs.TaskDefinition;

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(new targets.EcsTask({
  cluster,
  taskDefinition,
  taskCount: 1,

  // Overrides the cpu and memory values in the task definition
  cpu: '512',
  memory: '512',
}));
```

Python:

```python
import aws_cdk.aws_ecs as ecs

# cluster: ecs.ICluster
# task_definition: ecs.TaskDefinition

rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.hours(1))
)

rule.add_target(targets.EcsTask(
    cluster=cluster,
    task_definition=task_definition,
    task_count=1,

    # Overrides the cpu and memory values in the task definition
    cpu="512",
    memory="512"
))
```

Java:

```java
import software.amazon.awscdk.services.ecs.*;

ICluster cluster;
TaskDefinition taskDefinition;

Rule rule = Rule.Builder.create(this, "Rule")
        .schedule(Schedule.rate(Duration.hours(1)))
        .build();

rule.addTarget(EcsTask.Builder.create()
        .cluster(cluster)
        .taskDefinition(taskDefinition)
        .taskCount(1)

        // Overrides the cpu and memory values in the task definition
        .cpu("512")
        .memory("512")
        .build());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.ECS;

ICluster cluster;
TaskDefinition taskDefinition;

var rule = new Rule(this, "Rule", new RuleProps {
    Schedule = Schedule.Rate(Duration.Hours(1))
});

rule.AddTarget(new EcsTask(new EcsTaskProps {
    Cluster = cluster,
    TaskDefinition = taskDefinition,
    TaskCount = 1,

    // Overrides the cpu and memory values in the task definition
    Cpu = "512",
    Memory = "512"
}));
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import ecs "github.com/aws/aws-cdk-go/awscdk"

var cluster ICluster
var taskDefinition TaskDefinition

rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
	Schedule: events.Schedule_Rate(cdk.Duration_Hours(jsii.Number(1))),
})

rule.AddTarget(targets.NewEcsTask(&EcsTaskProps{
	Cluster: Cluster,
	TaskDefinition: TaskDefinition,
	TaskCount: jsii.Number(1),

	// Overrides the cpu and memory values in the task definition
	Cpu: jsii.String("512"),
	Memory: jsii.String("512"),
}))
```

## Initializer

TypeScript:

```ts
new EcsTask(props: EcsTaskProps)
```

Python:

```python
EcsTask(props: EcsTaskProps)
```

Java:

```java
new EcsTask(EcsTaskProps props)
```

C# / .NET:

```csharp
new EcsTask(EcsTaskProps props)
```

Golang:

```go
NewEcsTask(Props EcsTaskProps)
```

## Properties

- `securityGroups`: [ISecurityGroup](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISecurityGroup.html)[] — The security groups associated with the task.

  **Default:** A new security group is created.

  Only applicable with awsvpc network mode.

## Methods

- `bind(rule: IRuleRef, _id?: string): RuleTargetConfig` — Allows using tasks as target of EventBridge events.
