# FargateTaskDefinition

The details of a task definition run on a Fargate cluster.

Extends: Construct › Resource › TaskDefinition › FargateTaskDefinition
Implements: IResource, IFargateTaskDefinition, ITaskDefinition, IEnvironmentAware, ITaskDefinitionRef, IConstruct, IDependable

- CloudFormation: [`AWS::ECS::TaskDefinition`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ecs-taskdefinition.html)
- L1 type: [`CfnTaskDefinition`](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.CfnTaskDefinition.html)
- Source: [`aws-ecs/lib/fargate/fargate-task-definition.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ecs/lib/fargate/fargate-task-definition.ts#L128)
- Type last changed: v2.196.0 · 16 May 2025

## Example

```ts
declare const vpc: ec2.Vpc;

const cluster = new ecs.Cluster(this, 'FargateCPCluster', {
  vpc,
  enableFargateCapacityProviders: true,
});

const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef');

taskDefinition.addContainer('web', {
  image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

new ecs.FargateService(this, 'FargateService', {
  cluster,
  taskDefinition,
  minHealthyPercent: 100,
  capacityProviderStrategies: [
    {
      capacityProvider: 'FARGATE_SPOT',
      weight: 2,
    },
    {
      capacityProvider: 'FARGATE',
      weight: 1,
    },
  ],
});
```

## Initializer

```ts
new FargateTaskDefinition(scope: Construct, id: string, props?: FargateTaskDefinitionProps)
```

## Construct Props

- `cpu`: number = 256 — The number of cpu units used by the task.

  For tasks using the Fargate launch type,
  this field is required and you must use one of the following values,
  which determines your range of valid values for the memory parameter:

  256 (.25 vCPU) - Available memory values: 512 (0.5 GB), 1024 (1 GB), 2048 (2 GB)

  512 (.5 vCPU) - Available memory values: 1024 (1 GB), 2048 (2 GB), 3072 (3 GB), 4096 (4 GB)

  1024 (1 vCPU) - Available memory values: 2048 (2 GB), 3072 (3 GB), 4096 (4 GB), 5120 (5 GB), 6144 (6 GB), 7168 (7 GB), 8192 (8 GB)

  2048 (2 vCPU) - Available memory values: Between 4096 (4 GB) and 16384 (16 GB) in increments of 1024 (1 GB)

  4096 (4 vCPU) - Available memory values: Between 8192 (8 GB) and 30720 (30 GB) in increments of 1024 (1 GB)

  8192 (8 vCPU) - Available memory values: Between 16384 (16 GB) and 61440 (60 GB) in increments of 4096 (4 GB)

  16384 (16 vCPU) - Available memory values: Between 32768 (32 GB) and 122880 (120 GB) in increments of 8192 (8 GB)

  Note: For windows platforms, this field is not enforced at runtime. However, it is still required as it is used to determine
  the instance type and size that tasks run on.

- `enableFaultInjection`: boolean — Enables fault injection and allows for fault injection requests to be accepted from the task's containers.

  **Default:** undefined - ECS default setting is false

  Fault injection only works with tasks using the {@link NetworkMode.AWS_VPC} or {@link NetworkMode.HOST} network modes.

- `ephemeralStorageGiB`: number = 20 — The amount (in GiB) of ephemeral storage to be allocated to the task.

  The maximum supported value is 200 GiB.

  NOTE: This parameter is only supported for tasks hosted on AWS Fargate using platform version 1.4.0 or later
  for Linux tasks, and platform version 1.0.0 or later for Windows tasks.

- `executionRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The name of the IAM task execution role that grants the ECS agent permission to call AWS APIs on your behalf.

  **Default:** An execution role will be automatically created if you use ECR images in your task definition.

  The role will be used to retrieve container images from ECR and create CloudWatch log groups.

- `family`: string — The name of a family that this task definition is registered to.

  **Default:** Automatically generated name.

  A family groups multiple versions of a task definition.

- `memoryLimitMiB`: number = 512 — The amount (in MiB) of memory used by the task.

  For tasks using the Fargate launch type,
  this field is required and you must use one of the following values, which determines your range of valid values for the cpu parameter:

  512 (0.5 GB), 1024 (1 GB), 2048 (2 GB) - Available cpu values: 256 (.25 vCPU)

  1024 (1 GB), 2048 (2 GB), 3072 (3 GB), 4096 (4 GB) - Available cpu values: 512 (.5 vCPU)

  2048 (2 GB), 3072 (3 GB), 4096 (4 GB), 5120 (5 GB), 6144 (6 GB), 7168 (7 GB), 8192 (8 GB) - Available cpu values: 1024 (1 vCPU)

  Between 4096 (4 GB) and 16384 (16 GB) in increments of 1024 (1 GB) - Available cpu values: 2048 (2 vCPU)

  Between 8192 (8 GB) and 30720 (30 GB) in increments of 1024 (1 GB) - Available cpu values: 4096 (4 vCPU)

  Between 16384 (16 GB) and 61440 (60 GB) in increments of 4096 (4 GB) - Available cpu values: 8192 (8 vCPU)

  Between 32768 (32 GB) and 122880 (120 GB) in increments of 8192 (8 GB) - Available cpu values: 16384 (16 vCPU)

  Note: For windows platforms, this field is not enforced at runtime. However, it is still required as it is used to determine
  the instance type and size that tasks run on.

- `pidMode`: [PidMode](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.PidMode.html) — The process namespace to use for the containers in the task.

  **Default:** PidMode used by the task is not specified

  Only supported for tasks that are hosted on AWS Fargate if the tasks
  are using platform version 1.4.0 or later (Linux).  Only the TASK option
  is supported for Linux-based Fargate containers. Not supported in
  Windows containers. If pidMode is specified for a Fargate task, then
  runtimePlatform.operatingSystemFamily must also be specified.  For more
  information, see [Task Definition Parameters](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_definition_pidmode).

- `proxyConfiguration`: [ProxyConfiguration](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ProxyConfiguration.html) — The configuration details for the App Mesh proxy.

  **Default:** No proxy configuration.

- `runtimePlatform`: [RuntimePlatform](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.RuntimePlatform.html) = Undefined. — The operating system that your task definitions are running on.

  A runtimePlatform is supported only for tasks using the Fargate launch type.

- `taskRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf.

  **Default:** A task role is automatically created for you.

- `volumes`: [Volume](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.Volume.html)[] — The list of volume definitions for the task.

  **Default:** No volumes are passed to the Docker daemon on a container instance.

  For more information, see
  [Task Definition Parameter Volumes](https://docs.aws.amazon.com/AmazonECS/latest/developerguide//task_definition_parameters.html#volumes).

## Properties

- `compatibility`: [Compatibility](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.Compatibility.html) — The task launch type compatibility requirement.

- `cpu`: number — The number of cpu units used by the task.

- `defaultContainer`: [ContainerDefinition](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ContainerDefinition.html) — Default container for this task.

  Load balancers will send traffic to this container. The first
  essential container that is added to this task will become the default
  container.

- `env`: [ResourceEnvironment](/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.

- `ephemeralStorageGiB`: number — The amount (in GiB) of ephemeral storage to be allocated to the task.

- `executionRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — Execution role for this task definition.

- `family`: string — The name of a family that this task definition is registered to.

  A family groups multiple versions of a task definition.

- `inferenceAccelerators`: [InferenceAccelerator](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.InferenceAccelerator.html)[] — Public getter method to access list of inference accelerators attached to the instance.

- `isEc2Compatible`: boolean — Return true if the task definition can be run on an EC2 cluster.

- `isExternalCompatible`: boolean — Return true if the task definition can be run on a ECS anywhere cluster.

- `isFargateCompatible`: boolean — Return true if the task definition can be run on a Fargate cluster.

- `isManagedInstancesCompatible`: boolean — Return true if the task definition can be run on Managed Instances.

- `memoryMiB`: number — The amount (in MiB) of memory used by the task.

- `networkMode`: [NetworkMode](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.NetworkMode.html) — The Docker networking mode to use for the containers in the task.

  Fargate tasks require the awsvpc network mode.

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

- `pidMode`: [PidMode](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.PidMode.html) — The process namespace to use for the containers in the task.

  Only supported for tasks that are hosted on AWS Fargate if the tasks
  are using platform version 1.4.0 or later (Linux). Not supported in
  Windows containers. If pidMode is specified for a Fargate task,
  then runtimePlatform.operatingSystemFamily must also be specified.  For more
  information, see [Task Definition Parameters](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_definition_pidmode).

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `referencesSecretJsonField`: boolean — Whether this task definition has at least a container that references a specific JSON field of a secret stored in Secrets Manager.

- `runtimePlatform`: [RuntimePlatform](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.RuntimePlatform.html) — The runtime platform (operating system family and CPU architecture) for the task definition, or `undefined` if none was configured.

  Intentionally public: `runtimePlatform` is already public API as an input prop and on the
  underlying L1 `CfnTaskDefinition`, so reading it back from the L2 is deliberate read-parity.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

- `taskDefinitionArn`: string — The full Amazon Resource Name (ARN) of the task definition.

- `taskDefinitionRef`: [TaskDefinitionReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ecs.TaskDefinitionReference.html) — A reference to this task definition.

- `taskRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf.

## Methods

- `addContainer(id: string, props: ContainerDefinitionOptions): ContainerDefinition` — Adds a new container to the task definition.

- `addExtension(extension: ITaskDefinitionExtension): void` — Adds the specified extension to the task definition.

  Extension can be used to apply a packaged modification to
  a task definition.

- `addFirelensLogRouter(id: string, props: FirelensLogRouterDefinitionOptions): FirelensLogRouter` — Adds a firelens log router to the task definition.

- `addInferenceAccelerator(inferenceAccelerator: InferenceAccelerator): void` — ECS TaskDefinition's inferenceAccelerator is EOL since April 2024

- `addPlacementConstraint(constraint: PlacementConstraint): void` — Adds the specified placement constraint to the task definition.

- `addToExecutionRolePolicy(statement: PolicyStatement): void` — Adds a policy statement to the task execution IAM role.

- `addToTaskRolePolicy(statement: PolicyStatement): void` — Adds a policy statement to the task IAM role.

- `addVolume(volume: Volume): void` — Adds a volume to the task definition.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `findContainer(containerName: string): ContainerDefinition` — Returns the container that match the provided containerName.

- `findPortMappingByName(name: string): PortMapping` — Determine the existing port mapping for the provided name.

  **Parameters**

  - `name` — : port mapping name.

  **Returns:** PortMapping for the provided name, if it exists.

- `fromFargateTaskDefinitionArn(scope: Construct, id: string, fargateTaskDefinitionArn: string): IFargateTaskDefinition` (static) — Imports a task definition from the specified task definition ARN.

- `fromFargateTaskDefinitionAttributes(scope: Construct, id: string, attrs: FargateTaskDefinitionAttributes): IFargateTaskDefinition` (static) — Import an existing Fargate task definition from its attributes.

- `fromTaskDefinitionArn(scope: Construct, id: string, taskDefinitionArn: string): ITaskDefinition` (static) — Imports a task definition from the specified task definition ARN.

  The task will have a compatibility of EC2+Fargate.

- `fromTaskDefinitionAttributes(scope: Construct, id: string, attrs: TaskDefinitionAttributes): ITaskDefinition` (static) — Create a task definition from a task definition reference.

- `grantRun(grantee: IGrantable): Grant` — Grants permissions to run this task definition.

  This will grant the following permissions:

    - ecs:RunTask
    - iam:PassRole

  [disable-awslint:no-grants]

  **Parameters**

  - `grantee` — Principal to grant consume rights to.

- `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`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `obtainExecutionRole(): IRole` — Creates the task execution IAM role if it doesn't already exist.

- `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.
