Update ECS task definition image tag when new image tag – Amazon-web-services

by
Ali Hasan
amazon-ecs amazon-web-services aws-codepipeline boot2docker

Quick Fix: To update the ECS task definition image tag when a new image tag is available:

  1. Create a new revision of the task definition with the new image tag.

  2. Update the ECS service to use the new task definition revision.

The Problem:

You want to update your ECS task definition image tag when a newer tag exists, but it doesn’t work. You push the image with the new tag, and then try to update the service with the aws ecs update-service command, but it doesn’t get the new image automatically, and it still uses the old one. You want to find out how to get the new image tag automatically when you push a new tag.

The Solutions:

Solution 1: Create a New Revision with Updated Image Tag

To automatically update the ECS task definition image tag when a newer tag is pushed:

  1. Create a new task definition revision with the updated image tag using aws ecs register-task-definition. This command takes the existing task definition, modifies the image tag, and registers a new revision.
  2. Get the revision number of the newly created task definition using jq.
  3. Update the ECS service with the new task definition revision using aws ecs update-service. This command specifies the cluster, service, and task definition revision to use.

Sample CodePipeline buildspec.yml snippet:

build:
    commands:
        - docker build -t $FULL_IMAGE --target prod .
        - docker push $FULL_IMAGE
post_build:
    commands:
        - TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_FAMILY" --region "$AWS_DEFAULT_REGION")
        - NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$FULL_IMAGE" '.taskDefinition | .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
        - NEW_TASK_INFO=$(aws ecs register-task-definition --region "$AWS_DEFAULT_REGION" --cli-input-json "$NEW_TASK_DEFINITION")
        - NEW_REVISION=$(echo $NEW_TASK_INFO | jq '.taskDefinition.revision')
        - aws ecs update-service --cluster ${ECS_CLUSTER} --service ${SERVICE_NAME} --task-definition ${TASK_FAMILY}:${NEW_REVISION}

Solution 2: Using Task Definition Revision

To automatically update your ECS task definition image tag with a new tag:

  1. Create a New Task Definition Revision: After pushing the new image with the updated tag, use the aws ecs register-task-definition command to create a new task definition revision with the new image tag. Refer to the tutorial linked in the provided reference for detailed steps.

  2. Update ECS Service: Once the new task definition revision is created, update your ECS service to use this revision by passing the --task-definition parameter with the new task definition revision ID.

  3. Remove --force-new-deployment Parameter: Remove the --force-new-deployment parameter from the aws ecs update-service command, as it is intended for deploying new containers with the current task revision, not for deploying an updated task revision.

By following these steps, you can ensure that your ECS task definition image tag is automatically updated when a newer tag exists. This process takes into account the immutable nature of task definitions in ECS and requires the creation of a new revision for each update.

Solution 3: Auto-update ECS task definition with new image tag

To automatically update an ECS task definition with the latest image tag, you can follow these steps:

  1. Define a task_definition.json file: Create a JSON file that contains the desired task definition. Use a placeholder for the image field, as seen below:
  2. {
      "containerDefinitions": [
        {
          "image": "<FULL_IMAGE>",
          ...
        }
      ],
      ...
    }
    
  3. Extract the task definition: Obtain the original task definition by selecting a task revision on the ECS console. Extract the relevant fields as outlined in the provided reference.
  4. Update the task_definition.json file: Replace the placeholder in task_definition.json with the actual image name and tag.
  5. Register the new task definition: Use the AWS CLI to register the updated task definition. This will generate a new revision number.
  6. Update the service: Use the AWS CLI to update the service with the new task definition revision.

By following these steps, you can ensure that your ECS task definitions are automatically updated with the latest image tags, ensuring seamless deployment of your applications.

Q&A

How to get the new image tag automatically when I push a new tag?

You can create a new task revision with the new image tag, then using AWS API or CLI to update the ECS service.

How to update new image in an ECS service?

Update a new task definition with new image tag, and trigger the ECS service to deploy with the new task definition revision ID.

How to set task definition image placeholder in buildspec?

Set a placeholder value for the image field in task_definition.json file and update it with buildspec command.

Video Explanation:

The following video, titled "”AWS", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This video provides further insights and detailed explanations related to the content discussed in the article.

Best practices for CI/CD using AWS …” description=”AWS re:Invent 2019: [REPEAT] Best practices for CI/CD using AWS Fargate and Amazon ECS (CON333-R). 27K views · 4 years ago …more …”]