CircleCI-Public/aws-ecs-orb

Enhance `run-task` to allow streaming of logs from ecs task and waiting till completion

lokst opened this issue · 3 comments

lokst commented

What would you like to be added

Add the ability to stream logs from an ecs task and wait till completion of the task being run by the orb.

Why is this needed

This is useful for long-running tasks like database migrator tasks.

Related references

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-logs.html

gugu commented

Here is my piece of bash to wait till task completes and fail if container exit code is not 0:

set -euo pipefail
TASK_ARN=$(aws ecs list-tasks --family=authorizer-migration|jq '.taskArns[]' -r)
if [ -z "$TASK_ARN" ]; then
  echo "Task was not found"
  exit 1
fi
aws ecs wait tasks-stopped --tasks $TASK_ARN
aws ecs describe-tasks --tasks $TASK_ARN|jq -e '.tasks[0].containers[0].exitCode == 0'

Have no time for PR, hope someone can improve it to be usable

A slight variation of the above worked well for me as a replacement for the run-task step:

TASK_ARN="$(aws ecs run-task --cluster $ECS_CLUSTER_NAME --task-definition migrations-production --count 1 --launch-type EC2 | jq '.tasks[0].taskArn' -r)"
aws ecs wait tasks-stopped --cluster $ECS_CLUSTER_NAME --tasks $TASK_ARN
aws ecs describe-tasks --cluster $ECS_CLUSTER_NAME --tasks $TASK_ARN | jq -e '.tasks[0].containers[0].exitCode == 0'

Thanks @gugu 💚
I think streaming logs is only possible with the ecs-cli: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-logs.html

brivu commented

Hey @gugu,

I'm working on updating this orb at the moment and wanted to understand your request a little bit more. Right now, when running the run-task command, there's a json output that contains the task-arn below:

    "tasks": [
        {
            "attachments": [],
            "attributes": [
                {
                    "name": "ecs.cpu-architecture",
                    "value": "x86_64"
                }
            ],
            "availabilityZone": "*********b",
            "clusterArn": "arn:aws:ecs:*********:************:cluster/*************-cluster",
            "containerInstanceArn": "arn:aws:ecs:*********:************:container-instance/*************-cluster/2c4f47871e1943479280dc1e8719eed0",
            "containers": [
                {
                    "containerArn": "arn:aws:ecs:*********:************:container/*************-cluster/164acb4a14b44e8084a6d18b851f4ff3/6dc7d919-0c9e-4a8b-9400-e442354f89eb",
                    "taskArn": "arn:aws:ecs:*********:************:task/*************-cluster/164acb4a14b44e8084a6d18b851f4ff3",
                    "name": "*****",
                    "image": "busybox",
                    "lastStatus": "PENDING",
                    "networkInterfaces": [],
                    "cpu": "0",
                    "memory": "256"
                }
            ],
            "cpu": "256",
            "createdAt": "2022-06-08T22:38:41.104000+00:00",
            "desiredStatus": "RUNNING",
            "enableExecuteCommand": false,
            "group": "family:*************-*****360",
            "lastStatus": "PENDING",
            "launchType": "EC2",
            "memory": "512",
            "overrides": {
                "containerOverrides": [
                    {
                        "name": "*****",
                        "memory": 512
                    }
                ],
                "inferenceAcceleratorOverrides": []
            },
            "tags": [],
            "taskArn": "arn:aws:ecs:*********:************:task/*************-cluster/164acb4a14b44e8084a6d18b851f4ff3",
            "taskDefinitionArn": "arn:aws:ecs:*********:************:task-definition/*************-*****360:116",
            "version": 1
        }
    ],
    "failures": []
}

Is there something else that's missing in this output that you need?

Please let me know and I'll see if we can add it for you.

Best,
Brian