Skip to content

execute_command async

execute_command(
    command: String,
    interactive: Boolean,
    task: String,
    *,
    config_overrides: Optional[AsyncECSClientConfig] = None,
    cluster: Optional[String] = None,
    container: Optional[String] = None,
) -> ExecuteCommandResponse

Runs a command remotely on a container within a task.

If you use a condition key in your IAM policy to refine the conditions for the policy statement, for example limit the actions to a specific cluster, you receive an AccessDeniedException when there is a mismatch between the condition key value and the corresponding parameter value.

For information about required permissions and considerations, see Using Amazon ECS Exec for debugging in the Amazon ECS Developer Guide.

Parameters:

  • cluster (Optional[String], default: None ) –

    The Amazon Resource Name (ARN) or short name of the cluster the task is running in. If you do not specify a cluster, the default cluster is assumed.

  • container (Optional[String], default: None ) –

    The name of the container to execute the command on. A container name only needs to be specified for tasks containing multiple containers.

  • command (String) –

    The command to run on the container.

  • interactive (Boolean) –

    Use this flag to run your command in interactive mode.

  • task (String) –

    The Amazon Resource Name (ARN) or ID of the task the container is part of.

Raises:

  • AccessDeniedException –

    You don't have authorization to perform the requested action.

  • ClientException –

    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.

  • ClusterNotFoundException –

    The specified cluster wasn't found. You can view your available clusters with ListClusters. Amazon ECS clusters are Region specific.

  • InvalidParameterException –

    The specified parameter isn't valid. Review the available parameters for the API request.

    For more information about service event errors, see Amazon ECS service event messages.

  • ServerException –

    These errors are usually caused by a server issue.

  • TargetNotConnectedException –

    The execute command cannot run. This error can be caused by any of the following configuration issues:

    • Incorrect IAM permissions

    • The SSM agent is not installed or is not running

    • There is an interface Amazon VPC endpoint for Amazon ECS, but there is not one for Systems Manager Session Manager

    For information about how to troubleshoot the issues, see Troubleshooting issues with ECS Exec in the Amazon Elastic Container Service Developer Guide.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

To run a command remotely on a container in a task This example runs an interactive /bin/sh command on a container MyContainer.

>>> await client.execute_command(cluster='MyCluster', container='MyContainer', command='/bin/sh', interactive=True, task='arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE')
Source code in docs_src/capo_ecs/_services/async_ecs.py
async def execute_command(
    self,
    command: "capo_ecs.types.string.String",
    interactive: "capo_ecs.types.boolean.Boolean",
    task: "capo_ecs.types.string.String",
    *,
    config_overrides: Optional[AsyncECSClientConfig] = None,
    cluster: Optional["capo_ecs.types.string.String"] = None,
    container: Optional["capo_ecs.types.string.String"] = None,
) -> "capo_ecs.types.execute_command_response.ExecuteCommandResponse":
    """<p>Runs a command remotely on a container within a task.</p> <p>If you use a condition key in your IAM policy to refine the conditions for the policy statement, for example limit the actions to a specific cluster, you receive an <code>AccessDeniedException</code> when there is a mismatch between the condition key value and the corresponding parameter value.</p> <p>For information about required permissions and considerations, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html">Using Amazon ECS Exec for debugging</a> in the <i>Amazon ECS Developer Guide</i>. </p>

    Args:
        cluster: <p>The Amazon Resource Name (ARN) or short name of the cluster the task is running in. If you do not specify a cluster, the default cluster is assumed.</p>
        container: <p>The name of the container to execute the command on. A container name only needs to be specified for tasks containing multiple containers.</p>
        command: <p>The command to run on the container.</p>
        interactive: <p>Use this flag to run your command in interactive mode.</p>
        task: <p>The Amazon Resource Name (ARN) or ID of the task the container is part of.</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.server_exception.ServerException: <p>These errors are usually caused by a server issue.</p>
        capo_ecs.errors.target_not_connected_exception.TargetNotConnectedException: <p>The execute command cannot run. This error can be caused by any of the following configuration issues:</p> <ul> <li> <p>Incorrect IAM permissions</p> </li> <li> <p>The SSM agent is not installed or is not running</p> </li> <li> <p> There is an interface Amazon VPC endpoint for Amazon ECS, but there is not one for Systems Manager Session Manager</p> </li> </ul> <p>For information about how to troubleshoot the issues, see <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html">Troubleshooting issues with ECS Exec</a> in the <i>Amazon Elastic Container Service Developer Guide</i>.</p>
        capo_ecs.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To run a command remotely on a container in a task
        This example runs an interactive /bin/sh command on a container MyContainer.

        >>> await client.execute_command(cluster='MyCluster', container='MyContainer', command='/bin/sh', interactive=True, task='arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_ecs.types.execute_command_request.ExecuteCommandRequest]",
    ) -> AsyncOperationResponse[
        "capo_ecs.types.execute_command_response.ExecuteCommandResponse"
    ]:
        import capo_ecs._operations.amazon_ec2_container_service_v20141113.execute_command

        (
            output,
            http_response,
        ) = await capo_ecs._operations.amazon_ec2_container_service_v20141113.execute_command.async_execute_command(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_output(
        output: "capo_ecs.types.execute_command_response.ExecuteCommandResponse",
    ) -> dict[str, Any]:
        import capo_ecs.types.execute_command_response

        return capo_ecs.types.execute_command_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("ExecuteCommand", redact_output=_redact_output),
    )
    input_: capo_ecs.types.execute_command_request.ExecuteCommandRequest = {
        "command": command,
        "interactive": interactive,
        "task": task,
    }
    if cluster is not None:
        input_["cluster"] = cluster
    if container is not None:
        input_["container"] = container

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output