Class TaskDefinition
- All Implemented Interfaces:
- IResource,- ITaskDefinition,- software.amazon.jsii.JsiiSerializable,- software.constructs.IConstruct,- software.constructs.IDependable
- Direct Known Subclasses:
- Ec2TaskDefinition,- ExternalTaskDefinition,- FargateTaskDefinition
Example:
 import software.amazon.awscdk.services.cloudwatch.*;
 Cluster cluster;
 TaskDefinition taskDefinition;
 Alarm elbAlarm;
 FargateService service = FargateService.Builder.create(this, "Service")
         .cluster(cluster)
         .taskDefinition(taskDefinition)
         .minHealthyPercent(100)
         .deploymentAlarms(DeploymentAlarmConfig.builder()
                 .alarmNames(List.of(elbAlarm.getAlarmName()))
                 .behavior(AlarmBehavior.ROLLBACK_ON_ALARM)
                 .build())
         .build();
 // Defining a deployment alarm after the service has been created
 String cpuAlarmName = "MyCpuMetricAlarm";
 Alarm.Builder.create(this, "CPUAlarm")
         .alarmName(cpuAlarmName)
         .metric(service.metricCpuUtilization())
         .evaluationPeriods(2)
         .threshold(80)
         .build();
 service.enableDeploymentAlarms(List.of(cpuAlarmName), DeploymentAlarmOptions.builder()
         .behavior(AlarmBehavior.FAIL_ON_ALARM)
         .build());
 - 
Nested Class SummaryNested ClassesNested classes/interfaces inherited from class software.amazon.jsii.JsiiObjectsoftware.amazon.jsii.JsiiObject.InitializationModeNested classes/interfaces inherited from interface software.constructs.IConstructsoftware.constructs.IConstruct.Jsii$DefaultNested classes/interfaces inherited from interface software.amazon.awscdk.IResourceIResource.Jsii$DefaultNested classes/interfaces inherited from interface software.amazon.awscdk.services.ecs.ITaskDefinitionITaskDefinition.Jsii$Default, ITaskDefinition.Jsii$Proxy
- 
Field SummaryFields
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedTaskDefinition(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) protectedTaskDefinition(software.amazon.jsii.JsiiObjectRef objRef) TaskDefinition(software.constructs.Construct scope, String id, TaskDefinitionProps props) Constructs a new instance of the TaskDefinition class.
- 
Method SummaryModifier and TypeMethodDescriptionaddContainer(String id, ContainerDefinitionOptions props) Adds a new container to the task definition.voidaddExtension(ITaskDefinitionExtension extension) Adds the specified extension to the task definition.Adds a firelens log router to the task definition.voidaddInferenceAccelerator(InferenceAccelerator inferenceAccelerator) Deprecated.ECS TaskDefinition's inferenceAccelerator is EOL since April 2024voidaddPlacementConstraint(PlacementConstraint constraint) Adds the specified placement constraint to the task definition.voidaddToExecutionRolePolicy(PolicyStatement statement) Adds a policy statement to the task execution IAM role.voidaddToTaskRolePolicy(PolicyStatement statement) Adds a policy statement to the task IAM role.voidAdds a volume to the task definition.findContainer(String containerName) Returns the container that match the provided containerName.findPortMappingByName(String name) Determine the existing port mapping for the provided name.static ITaskDefinitionfromTaskDefinitionArn(software.constructs.Construct scope, String id, String taskDefinitionArn) Imports a task definition from the specified task definition ARN.static ITaskDefinitionfromTaskDefinitionAttributes(software.constructs.Construct scope, String id, TaskDefinitionAttributes attrs) Create a task definition from a task definition reference.The task launch type compatibility requirement.protected List<ContainerDefinition> The container definitions.Default container for this task.The amount (in GiB) of ephemeral storage to be allocated to the task.Execution role for this task definition.The name of a family that this task definition is registered to.Public getter method to access list of inference accelerators attached to the instance.Return true if the task definition can be run on an EC2 cluster.Return true if the task definition can be run on a ECS anywhere cluster.Return true if the task definition can be run on a Fargate cluster.Return true if the task definition can be run on Managed Instances.The networking mode to use for the containers in the task.The process namespace to use for the containers in the task.Whether this task definition has at least a container that references a specific JSON field of a secret stored in Secrets Manager.The full Amazon Resource Name (ARN) of the task definition.The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf.grantRun(IGrantable grantee) Grants permissions to run this task definition.Creates the task execution IAM role if it doesn't already exist.voidDefault container for this task.Methods inherited from class software.amazon.awscdk.ResourceapplyRemovalPolicy, generatePhysicalName, getEnv, getPhysicalName, getResourceArnAttribute, getResourceNameAttribute, getStack, isOwnedResource, isResourceMethods inherited from class software.constructs.ConstructgetNode, isConstruct, toStringMethods inherited from class software.amazon.jsii.JsiiObjectjsiiAsyncCall, jsiiAsyncCall, jsiiCall, jsiiCall, jsiiGet, jsiiGet, jsiiSet, jsiiStaticCall, jsiiStaticCall, jsiiStaticGet, jsiiStaticGet, jsiiStaticSet, jsiiStaticSetMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface software.constructs.IConstructgetNodeMethods inherited from interface software.amazon.awscdk.IResourceapplyRemovalPolicy, getEnv, getStackMethods inherited from interface software.amazon.jsii.JsiiSerializable$jsii$toJson
- 
Field Details- 
PROPERTY_INJECTION_IDUniquely identifies this class.
 
- 
- 
Constructor Details- 
TaskDefinitionprotected TaskDefinition(software.amazon.jsii.JsiiObjectRef objRef) 
- 
TaskDefinitionprotected TaskDefinition(software.amazon.jsii.JsiiObject.InitializationMode initializationMode) 
- 
TaskDefinition@Stability(Stable) public TaskDefinition(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull TaskDefinitionProps props) Constructs a new instance of the TaskDefinition class.- Parameters:
- scope- This parameter is required.
- id- This parameter is required.
- props- This parameter is required.
 
 
- 
- 
Method Details- 
fromTaskDefinitionArn@Stability(Stable) @NotNull public static ITaskDefinition fromTaskDefinitionArn(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull String taskDefinitionArn) Imports a task definition from the specified task definition ARN.The task will have a compatibility of EC2+Fargate. - Parameters:
- scope- This parameter is required.
- id- This parameter is required.
- taskDefinitionArn- This parameter is required.
 
- 
fromTaskDefinitionAttributes@Stability(Stable) @NotNull public static ITaskDefinition fromTaskDefinitionAttributes(@NotNull software.constructs.Construct scope, @NotNull String id, @NotNull TaskDefinitionAttributes attrs) Create a task definition from a task definition reference.- Parameters:
- scope- This parameter is required.
- id- This parameter is required.
- attrs- This parameter is required.
 
- 
addContainer@Stability(Stable) @NotNull public ContainerDefinition addContainer(@NotNull String id, @NotNull ContainerDefinitionOptions props) Adds a new container to the task definition.- Parameters:
- id- This parameter is required.
- props- This parameter is required.
 
- 
addExtensionAdds the specified extension to the task definition.Extension can be used to apply a packaged modification to a task definition. - Parameters:
- extension- This parameter is required.
 
- 
addFirelensLogRouter@Stability(Stable) @NotNull public FirelensLogRouter addFirelensLogRouter(@NotNull String id, @NotNull FirelensLogRouterDefinitionOptions props) Adds a firelens log router to the task definition.- Parameters:
- id- This parameter is required.
- props- This parameter is required.
 
- 
addInferenceAccelerator@Stability(Deprecated) @Deprecated public void addInferenceAccelerator(@NotNull InferenceAccelerator inferenceAccelerator) Deprecated.ECS TaskDefinition's inferenceAccelerator is EOL since April 2024(deprecated) Adds an inference accelerator to the task definition.- Parameters:
- inferenceAccelerator- This parameter is required.
 
- 
addPlacementConstraintAdds the specified placement constraint to the task definition.- Parameters:
- constraint- This parameter is required.
 
- 
addToExecutionRolePolicyAdds a policy statement to the task execution IAM role.- Parameters:
- statement- This parameter is required.
 
- 
addToTaskRolePolicyAdds a policy statement to the task IAM role.- Parameters:
- statement- This parameter is required.
 
- 
addVolumeAdds a volume to the task definition.- Parameters:
- volume- This parameter is required.
 
- 
findContainer@Stability(Stable) @Nullable public ContainerDefinition findContainer(@NotNull String containerName) Returns the container that match the provided containerName.- Parameters:
- containerName- This parameter is required.
 
- 
findPortMappingByNameDetermine the existing port mapping for the provided name.- Parameters:
- name- : port mapping name. This parameter is required.
- Returns:
- PortMapping for the provided name, if it exists.
 
- 
grantRunGrants permissions to run this task definition.This will grant the following permissions: - ecs:RunTask
- iam:PassRole
 - Parameters:
- grantee- Principal to grant consume rights to. This parameter is required.
 
- 
obtainExecutionRoleCreates the task execution IAM role if it doesn't already exist.
- 
getCompatibilityThe task launch type compatibility requirement.- Specified by:
- getCompatibilityin interface- ITaskDefinition
 
- 
getContainersThe container definitions.
- 
getFamilyThe name of a family that this task definition is registered to.A family groups multiple versions of a task definition. 
- 
getInferenceAcceleratorsPublic getter method to access list of inference accelerators attached to the instance.
- 
getIsEc2CompatibleReturn true if the task definition can be run on an EC2 cluster.- Specified by:
- getIsEc2Compatiblein interface- ITaskDefinition
 
- 
getIsExternalCompatibleReturn true if the task definition can be run on a ECS anywhere cluster.- Specified by:
- getIsExternalCompatiblein interface- ITaskDefinition
 
- 
getIsFargateCompatibleReturn true if the task definition can be run on a Fargate cluster.- Specified by:
- getIsFargateCompatiblein interface- ITaskDefinition
 
- 
getIsManagedInstancesCompatibleReturn true if the task definition can be run on Managed Instances.- Specified by:
- getIsManagedInstancesCompatiblein interface- ITaskDefinition
 
- 
getNetworkModeThe networking mode to use for the containers in the task.- Specified by:
- getNetworkModein interface- ITaskDefinition
 
- 
getTaskDefinitionArnThe full Amazon Resource Name (ARN) of the task definition.- Specified by:
- getTaskDefinitionArnin interface- ITaskDefinition
 
- 
getTaskRoleThe name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf.- Specified by:
- getTaskRolein interface- ITaskDefinition
 
- 
getEphemeralStorageGiBThe amount (in GiB) of ephemeral storage to be allocated to the task.Only supported in Fargate platform version 1.4.0 or later. 
- 
getExecutionRoleExecution role for this task definition.- Specified by:
- getExecutionRolein interface- ITaskDefinition
 
- 
getPidModeThe 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. 
- 
getReferencesSecretJsonFieldWhether this task definition has at least a container that references a specific JSON field of a secret stored in Secrets Manager.
- 
getDefaultContainerDefault 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. 
- 
setDefaultContainerDefault 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. 
 
-