Skip to content
v2.273.0

EcsTask Class ​

Start a task on an ECS cluster.

Example ​

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
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
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());
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"
}));
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 ​

ts
new EcsTask(props: EcsTaskProps)
python
EcsTask(props: EcsTaskProps)
java
new EcsTask(EcsTaskProps props)
csharp
new EcsTask(EcsTaskProps props)
go
NewEcsTask(Props EcsTaskProps)

Properties ​

securityGroupsISecurityGroup[]security_groupstyping.List[ISecurityGroup]securityGroupsList<ISecurityGroup>SecurityGroupsISecurityGroup[]SecurityGroups*[]ISecurityGroup= A new security group is created.#
The security groups associated with the task.

Only applicable with awsvpc network mode.

Methods ​

bind(rule: IRuleRef, _id?: string): RuleTargetConfigdef bind(self, rule: IRuleRef, _id: typing.Optional[str] = None) -> RuleTargetConfigpublic RuleTargetConfig bind(IRuleRef rule, String _id)public RuleTargetConfig Bind(IRuleRef rule, string? _id = null)func Bind(Rule IRuleRef, _id *string) RuleTargetConfig#
Allows using tasks as target of EventBridge events.