amazon-ecs-deploy-task-definition icon indicating copy to clipboard operation
amazon-ecs-deploy-task-definition copied to clipboard

Mark previous task definition(s) as inactive

Open tovbinm opened this issue 4 years ago • 8 comments

When a new task definition is created it might be a good idea to allow marking the last one (or the one before the last one to allow rollback) as inactive.

tovbinm avatar Dec 03 '20 18:12 tovbinm

Has anyone worked around this?

tractorcow avatar May 16 '21 20:05 tractorcow

@tractorcow I add another step:

      - name: Download task definition
        id: download-task
        run: |
          aws ecs describe-task-definition \
            --task-definition ${{ env.task-definition }} \
            --query taskDefinition > task-definition.json
          echo "::set-output name=revision::$(cat task-definition.json | jq .revision)"

     - ...update the task definition...
     - ...deploy the task definition with `aws-actions/amazon-ecs-deploy-task-definition`
     
      - name: De-register previous revision
        run: |
          aws ecs deregister-task-definition \
            --task-definition ${{ env.task-definition }}:${{ steps.download-task.outputs.revision }}

ashutoshrishi avatar May 18 '21 01:05 ashutoshrishi

You may want delete not the latest revision but say latest - 2 just for the faster rollbacks. In this case the line that extracts the revision number can be replaced by this one echo "::set-output name=revision::$(($(cat task-definition.json | jq .revision) - 2))"

P.S. I have not tested it how it will behave with the negative number when you just start. But since this is the last step in the deployment, error probably can be ignored for the first two runs.

slava-lu avatar Apr 08 '22 11:04 slava-lu

if deregistering after a new task definition was pushed, then you would want to get rid of latest-1 revision. here is a sample of my pipeline that works and tested

      - name: Download task definition
        id: download-task
        run: |
          aws ecs describe-task-definition \
            --task-definition ${{ env.SERVICE_NAME }} \
            --query taskDefinition > task-definition.json

          echo "::set-output name=revision::$(($(cat task-definition.json | jq .revision) - 1))"

      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: task-definition.json
          container-name: ${{ env.SERVICE_NAME }}
          image: ghcr.io/${{ env.REPO }}:${{ inputs.imageTag }}

      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ env.SERVICE_NAME }}
          cluster: ecs-${{ env.ENV }}
          wait-for-service-stability: ${{ inputs.waitForServiceStability }}

      - name: De-register (latest-1) revision
        if: ${{ steps.download-task.outputs.revision > 0 }}
        run: |
          aws ecs deregister-task-definition \
            --task-definition  ${{ env.SERVICE_NAME }}:${{ steps.download-task.outputs.revision }}

povisenko avatar Jun 02 '22 12:06 povisenko

I wonder if this will be included in the action or not. I just found out that the old task definitions are not de-registered and now I have 150+ versions of task definition as active.

I guess every old one should be deleted on success.

ghost avatar Dec 05 '22 06:12 ghost

Active task definitions cost money on AWS?

nireld avatar Sep 07 '23 09:09 nireld

I don't think so, it's the running task themselves that should only cost anything.

tractorcow avatar Sep 08 '23 01:09 tractorcow

I don't think so, it's the running task themselves that should only cost anything.

I use tasks in order to update my fargate service Every update equals new ECR + new taskDef which pointer to the new image. Then, the task of my fargate service changed to the new one.

Therefore, if I understand correctly, the old once are meaningless

nireld avatar Sep 08 '23 06:09 nireld