CircleCI-Public / aws-ecs-orb

An orb that simplifies deployment to Amazon's Elastic Container Service (ECS). Supports both EC2 and Fargate launch types.
https://circleci.com/orbs/registry/orb/circleci/aws-ecs
MIT License
51 stars 80 forks source link

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

Closed lokst closed 2 years ago

lokst commented 4 years ago

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 4 years ago

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

jerryskye commented 4 years ago

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 2 years ago

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