Run ECS one-time task from CLI

Once you have your task definition setup, (we assume this task will exit when the job completes), you can use this guide to run the task from the command line.

Run Task

aws ecs run-task --region us-west-2 --cluster my-cluster --count 1 --launch-type EC2 --task-definition test:8
{
    "tasks": [
        {
            "taskArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:task/my-cluster/001fafbebea14cc68d19734e4b61e3fe",
            "clusterArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:cluster/my-cluster",
            "taskDefinitionArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:task-definition/test:8",
            "containerInstanceArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:container-instance/my-cluster/73247db541d542ec977dde7369ba93bd",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "test"
                    }
                ]
            },
            "lastStatus": "PENDING",
            "desiredStatus": "RUNNING",
            "cpu": "0",
            "memory": "10",
            "containers": [
                {
                    "containerArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:container/f3f29ab9-ba6e-4ce0-99db-203251212199",
                    "taskArn": "arn:aws:ecs:us-west-2:XXXXXXXXXXX:task/my-cluster/001fafbebea14cc68d19734e4b61e3fe",
                    "name": "echo",
                    "lastStatus": "PENDING",
                    "networkInterfaces": [],
                    "cpu": "0"
                }
            ],
            "version": 1,
            "createdAt": 1611083958.084,
            "group": "family:test",
            "launchType": "EC2",
            "attachments": [],
            "tags": []
        }
    ],
    "failures": []
}

Check Status of Task

aws ecs describe-tasks \
    --cluster my-cluster \
    --tasks arn:aws:ecs:us-west-2:XXXXXXXXXXX:task/my-cluster/001fafbebea14cc68d19734e4b61e3fe

Get Logs for Task

Assuming you have configured cloud watch logs you can use this command retrieve the logs for the task.  ( how-to install the ecs-cli command )

ecs-cli logs \
    --task-id arn:aws:ecs:us-west-2:XXXXXXXXXXX:task/my-cluster/001fafbebea14cc68d19734e4b61e3fe \
    --cluster y-cluster