# EcsEc2LaunchTargetOptions

Options to run an ECS task on EC2 in StepFunctions and ECS.

Used as props by: [EcsEc2LaunchTarget](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions_tasks.EcsEc2LaunchTarget.html)

- Source: [`aws-stepfunctions-tasks/lib/ecs/run-task.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-stepfunctions-tasks/lib/ecs/run-task.ts#L184)
- Type last changed: v2.234.0 · 8 Jan 2026

## Example

TypeScript:

```ts
const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
  isDefault: true,
});

const cluster = new ecs.Cluster(this, 'Ec2Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', {
  instanceType: new ec2.InstanceType('t2.micro'),
  vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
});

const taskDefinition = new ecs.TaskDefinition(this, 'TD', {
  compatibility: ecs.Compatibility.EC2,
});

taskDefinition.addContainer('TheContainer', {
  image: ecs.ContainerImage.fromRegistry('foo/bar'),
  memoryLimitMiB: 256,
});

const runTask = new tasks.EcsRunTask(this, 'Run', {
  integrationPattern: sfn.IntegrationPattern.RUN_JOB,
  cluster,
  taskDefinition,
  launchTarget: new tasks.EcsEc2LaunchTarget({
    placementStrategies: [
      ecs.PlacementStrategy.spreadAcrossInstances(),
      ecs.PlacementStrategy.packedByCpu(),
      ecs.PlacementStrategy.randomly(),
    ],
    placementConstraints: [
      ecs.PlacementConstraint.memberOf('blieptuut'),
    ],
  }),
  propagatedTagSource: ecs.PropagatedTagSource.TASK_DEFINITION,
});
```

Python:

```python
vpc = ec2.Vpc.from_lookup(self, "Vpc",
    is_default=True
)

cluster = ecs.Cluster(self, "Ec2Cluster", vpc=vpc)
cluster.add_capacity("DefaultAutoScalingGroup",
    instance_type=ec2.InstanceType("t2.micro"),
    vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC)
)

task_definition = ecs.TaskDefinition(self, "TD",
    compatibility=ecs.Compatibility.EC2
)

task_definition.add_container("TheContainer",
    image=ecs.ContainerImage.from_registry("foo/bar"),
    memory_limit_mi_b=256
)

run_task = tasks.EcsRunTask(self, "Run",
    integration_pattern=sfn.IntegrationPattern.RUN_JOB,
    cluster=cluster,
    task_definition=task_definition,
    launch_target=tasks.EcsEc2LaunchTarget(
        placement_strategies=[
            ecs.PlacementStrategy.spread_across_instances(),
            ecs.PlacementStrategy.packed_by_cpu(),
            ecs.PlacementStrategy.randomly()
        ],
        placement_constraints=[
            ecs.PlacementConstraint.member_of("blieptuut")
        ]
    ),
    propagated_tag_source=ecs.PropagatedTagSource.TASK_DEFINITION
)
```

Java:

```java
IVpc vpc = Vpc.fromLookup(this, "Vpc", VpcLookupOptions.builder()
        .isDefault(true)
        .build());

Cluster cluster = Cluster.Builder.create(this, "Ec2Cluster").vpc(vpc).build();
cluster.addCapacity("DefaultAutoScalingGroup", AddCapacityOptions.builder()
        .instanceType(new InstanceType("t2.micro"))
        .vpcSubnets(SubnetSelection.builder().subnetType(SubnetType.PUBLIC).build())
        .build());

TaskDefinition taskDefinition = TaskDefinition.Builder.create(this, "TD")
        .compatibility(Compatibility.EC2)
        .build();

taskDefinition.addContainer("TheContainer", ContainerDefinitionOptions.builder()
        .image(ContainerImage.fromRegistry("foo/bar"))
        .memoryLimitMiB(256)
        .build());

EcsRunTask runTask = EcsRunTask.Builder.create(this, "Run")
        .integrationPattern(IntegrationPattern.RUN_JOB)
        .cluster(cluster)
        .taskDefinition(taskDefinition)
        .launchTarget(EcsEc2LaunchTarget.Builder.create()
                .placementStrategies(List.of(PlacementStrategy.spreadAcrossInstances(), PlacementStrategy.packedByCpu(), PlacementStrategy.randomly()))
                .placementConstraints(List.of(PlacementConstraint.memberOf("blieptuut")))
                .build())
        .propagatedTagSource(PropagatedTagSource.TASK_DEFINITION)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var vpc = Vpc.FromLookup(this, "Vpc", new VpcLookupOptions {
    IsDefault = true
});

var cluster = new Cluster(this, "Ec2Cluster", new ClusterProps { Vpc = vpc });
cluster.AddCapacity("DefaultAutoScalingGroup", new AddCapacityOptions {
    InstanceType = new InstanceType("t2.micro"),
    VpcSubnets = new SubnetSelection { SubnetType = SubnetType.PUBLIC }
});

var taskDefinition = new TaskDefinition(this, "TD", new TaskDefinitionProps {
    Compatibility = Compatibility.EC2
});

taskDefinition.AddContainer("TheContainer", new ContainerDefinitionOptions {
    Image = ContainerImage.FromRegistry("foo/bar"),
    MemoryLimitMiB = 256
});

var runTask = new EcsRunTask(this, "Run", new EcsRunTaskProps {
    IntegrationPattern = IntegrationPattern.RUN_JOB,
    Cluster = cluster,
    TaskDefinition = taskDefinition,
    LaunchTarget = new EcsEc2LaunchTarget(new EcsEc2LaunchTargetOptions {
        PlacementStrategies = new [] { PlacementStrategy.SpreadAcrossInstances(), PlacementStrategy.PackedByCpu(), PlacementStrategy.Randomly() },
        PlacementConstraints = new [] { PlacementConstraint.MemberOf("blieptuut") }
    }),
    PropagatedTagSource = PropagatedTagSource.TASK_DEFINITION
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
vpc := ec2.Vpc_FromLookup(this, jsii.String("Vpc"), &VpcLookupOptions{
	IsDefault: jsii.Boolean(true),
})

cluster := ecs.NewCluster(this, jsii.String("Ec2Cluster"), &ClusterProps{
	Vpc: Vpc,
})
cluster.AddCapacity(jsii.String("DefaultAutoScalingGroup"), &AddCapacityOptions{
	InstanceType: ec2.NewInstanceType(jsii.String("t2.micro")),
	VpcSubnets: &SubnetSelection{
		SubnetType: ec2.SubnetType_PUBLIC,
	},
})

taskDefinition := ecs.NewTaskDefinition(this, jsii.String("TD"), &TaskDefinitionProps{
	Compatibility: ecs.Compatibility_EC2,
})

taskDefinition.AddContainer(jsii.String("TheContainer"), &ContainerDefinitionOptions{
	Image: ecs.ContainerImage_FromRegistry(jsii.String("foo/bar")),
	MemoryLimitMiB: jsii.Number(256),
})

runTask := tasks.NewEcsRunTask(this, jsii.String("Run"), &EcsRunTaskProps{
	IntegrationPattern: sfn.IntegrationPattern_RUN_JOB,
	Cluster: Cluster,
	TaskDefinition: TaskDefinition,
	LaunchTarget: tasks.NewEcsEc2LaunchTarget(&EcsEc2LaunchTargetOptions{
		PlacementStrategies: []PlacementStrategy{
			ecs.PlacementStrategy_SpreadAcrossInstances(),
			ecs.PlacementStrategy_PackedByCpu(),
			ecs.PlacementStrategy_Randomly(),
		},
		PlacementConstraints: []PlacementConstraint{
			ecs.PlacementConstraint_MemberOf(jsii.String("blieptuut")),
		},
	}),
	PropagatedTagSource: ecs.PropagatedTagSource_TASK_DEFINITION,
})
```

## Properties

- `capacityProviderOptions`: [CapacityProviderOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions_tasks.CapacityProviderOptions.html) — The capacity provider options to use for the task.

  **Default:** 'EC2' LaunchType running tasks on Amazon EC2 instances registered to your cluster is used without the capacity provider strategy.

  This property allows you to set the capacity provider strategy for the task.

  If you want to set the capacity provider strategy for the task, specify
  `CapacityProviderOptions.custom()`.

  If you want to use the cluster's default capacity provider strategy, specify
  `CapacityProviderOptions.default()`.

- `placementConstraints`: [PlacementConstraint](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.PlacementConstraint.html)[] = None — Placement constraints.

- `placementStrategies`: [PlacementStrategy](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.PlacementStrategy.html)[] = None — Placement strategies.
