Skip to content

update_task_protection

update_task_protection(
    cluster: String,
    tasks: StringList,
    protection_enabled: Boolean,
    *,
    config_overrides: Optional[ECSClientConfig] = None,
    expires_in_minutes: Optional[BoxedInteger] = None,
) -> UpdateTaskProtectionResponse

Updates the protection status of a task. You can set protectionEnabled to true to protect your task from termination during scale-in events from Service Autoscaling or deployments.

Task-protection, by default, expires after 2 hours at which point Amazon ECS clears the protectionEnabled property making the task eligible for termination by a subsequent scale-in event.

You can specify a custom expiration period for task protection from 1 minute to up to 2,880 minutes (48 hours). To specify the custom expiration period, set the expiresInMinutes property. The expiresInMinutes property is always reset when you invoke this operation for a task that already has protectionEnabled set to true. You can keep extending the protection expiration period of a task by invoking this operation repeatedly.

To learn more about Amazon ECS task protection, see Task scale-in protection in the Amazon Elastic Container Service Developer Guide .

This operation is only supported for tasks belonging to an Amazon ECS service. Invoking this operation for a standalone task will result in an TASK_NOT_VALID failure. For more information, see API failure reasons.

If you prefer to set task protection from within the container, we recommend using the Task scale-in protection endpoint.

Parameters:

  • cluster (String) –

    The short name or full Amazon Resource Name (ARN) of the cluster that hosts the service that the task sets exist in.

  • tasks (StringList) –

    A list of up to 10 task IDs or full ARN entries.

  • protection_enabled (Boolean) –

    Specify true to mark a task for protection and false to unset protection, making it eligible for termination.

  • expires_in_minutes (Optional[BoxedInteger], default: None ) –

    If you set protectionEnabled to true, you can specify the duration for task protection in minutes. You can specify a value from 1 minute to up to 2,880 minutes (48 hours). During this time, your task will not be terminated by scale-in events from Service Auto Scaling or deployments. After this time period lapses, protectionEnabled will be reset to false.

    If you don’t specify the time, then the task is automatically protected for 120 minutes (2 hours).

Raises:

Examples:

To set task scale-in protection for a task for 60 minutes This example enables scale-in protection for a task for 60 minutes.

>>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=True, expires_in_minutes=60)
To set task scale-in protection for the default time period in minutes
This example enables task scale-in protection for a task, without specifying the expiresInMinutes parameter, for the default protection period of 120 minutes.
>>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=True)
To remove task scale-in protection
This example removes scale-in protection for a task.
>>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=False)
Source code in docs_src/capo_ecs/_services/ecs.py
def update_task_protection(
    self,
    cluster: "capo_ecs.types.string.String",
    tasks: "capo_ecs.types.string_list.StringList",
    protection_enabled: "capo_ecs.types.boolean.Boolean",
    *,
    config_overrides: Optional[ECSClientConfig] = None,
    expires_in_minutes: Optional[
        "capo_ecs.types.boxed_integer.BoxedInteger"
    ] = None,
) -> "capo_ecs.types.update_task_protection_response.UpdateTaskProtectionResponse":
    """<p>Updates the protection status of a task. You can set <code>protectionEnabled</code> to <code>true</code> to protect your task from termination during scale-in events from <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html">Service Autoscaling</a> or <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-types.html">deployments</a>.</p> <p>Task-protection, by default, expires after 2 hours at which point Amazon ECS clears the <code>protectionEnabled</code> property making the task eligible for termination by a subsequent scale-in event.</p> <p>You can specify a custom expiration period for task protection from 1 minute to up to 2,880 minutes (48 hours). To specify the custom expiration period, set the <code>expiresInMinutes</code> property. The <code>expiresInMinutes</code> property is always reset when you invoke this operation for a task that already has <code>protectionEnabled</code> set to <code>true</code>. You can keep extending the protection expiration period of a task by invoking this operation repeatedly.</p> <p>To learn more about Amazon ECS task protection, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-scale-in-protection.html">Task scale-in protection</a> in the <i> <i>Amazon Elastic Container Service Developer Guide</i> </i>.</p> <note> <p>This operation is only supported for tasks belonging to an Amazon ECS service. Invoking this operation for a standalone task will result in an <code>TASK_NOT_VALID</code> failure. For more information, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/api_failures_messages.html">API failure reasons</a>.</p> </note> <important> <p>If you prefer to set task protection from within the container, we recommend using the <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-scale-in-protection-endpoint.html">Task scale-in protection endpoint</a>.</p> </important>

    Args:
        cluster: <p>The short name or full Amazon Resource Name (ARN) of the cluster that hosts the service that the task sets exist in.</p>
        tasks: <p>A list of up to 10 task IDs or full ARN entries.</p>
        protection_enabled: <p>Specify <code>true</code> to mark a task for protection and <code>false</code> to unset protection, making it eligible for termination.</p>
        expires_in_minutes: <p>If you set <code>protectionEnabled</code> to <code>true</code>, you can specify the duration for task protection in minutes. You can specify a value from 1 minute to up to 2,880 minutes (48 hours). During this time, your task will not be terminated by scale-in events from Service Auto Scaling or deployments. After this time period lapses, <code>protectionEnabled</code> will be reset to <code>false</code>.</p> <p>If you don’t specify the time, then the task is automatically protected for 120 minutes (2 hours).</p>

    Raises:
        capo_ecs.errors.access_denied_exception.AccessDeniedException: <p>You don't have authorization to perform the requested action.</p>
        capo_ecs.errors.client_exception.ClientException: <p>These errors are usually caused by a client action. This client action might be using an action or resource on behalf of a user that doesn't have permissions to use the action or resource. Or, it might be specifying an identifier that isn't valid.</p>
        capo_ecs.errors.cluster_not_found_exception.ClusterNotFoundException: <p>The specified cluster wasn't found. You can view your available clusters with <a href="https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ListClusters.html">ListClusters</a>. Amazon ECS clusters are Region specific.</p>
        capo_ecs.errors.invalid_parameter_exception.InvalidParameterException: <p>The specified parameter isn't valid. Review the available parameters for the API request.</p> <p>For more information about service event errors, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-event-messages-list.html">Amazon ECS service event messages</a>. </p>
        capo_ecs.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource wasn't found.</p>
        capo_ecs.errors.server_exception.ServerException: <p>These errors are usually caused by a server issue.</p>
        capo_ecs.errors.unsupported_feature_exception.UnsupportedFeatureException: <p>The specified task isn't supported in this Region.</p>
        capo_ecs.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To set task scale-in protection for a task for 60 minutes
        This example enables scale-in protection for a task for 60 minutes.

        >>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=True, expires_in_minutes=60)
        To set task scale-in protection for the default time period in minutes
        This example enables task scale-in protection for a task, without specifying the expiresInMinutes parameter, for the default protection period of 120 minutes.

        >>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=True)
        To remove task scale-in protection
        This example removes scale-in protection for a task.

        >>> client.update_task_protection(cluster='test-task-protection', tasks=['b8b1cf532d0e46ba8d44a40d1de16772'], protection_enabled=False)
    """

    def _handler(
        req: "OperationRequest[capo_ecs.types.update_task_protection_request.UpdateTaskProtectionRequest]",
    ) -> OperationResponse[
        "capo_ecs.types.update_task_protection_response.UpdateTaskProtectionResponse"
    ]:
        import capo_ecs._operations.amazon_ec2_container_service_v20141113.update_task_protection

        output, http_response = (
            capo_ecs._operations.amazon_ec2_container_service_v20141113.update_task_protection.update_task_protection(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("UpdateTaskProtection")
    )
    input_: capo_ecs.types.update_task_protection_request.UpdateTaskProtectionRequest = {
        "cluster": cluster,
        "tasks": tasks,
        "protection_enabled": protection_enabled,
    }
    if expires_in_minutes is not None:
        input_["expires_in_minutes"] = expires_in_minutes

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output