Updating ECS Tasks Image Versions is not the easiest task if you don't go to the AWS Web UI. Also, there is not a direct code in order to do this in the AWS CLI. That's why I came up with the following code that supports you doing this without major effort.
The only prerequisites are:
- A machine with WSL2 (I only tested in Ubuntu), Linux, or macOS.
- Install jq (sudo apt-get install jq).
- Create a .sh file with the following script.
TASK_NAME=task_name
SERVICE_NAME=service_name
IMAGE_NAME=image_name
CLUSTER_NAME=cluster_name
REGION=region
VERSION=version
ACCOUNT_NUMBER=account_number
NEW_IMAGE=$ACCOUNT_NUMBER.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME:$VERSION
TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition "$TASK_NAME" --region "$REGION")
NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq --arg IMAGE "$NEW_IMAGE" '.taskDefinition | .containerDefinitions[0].image = $IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
NEW_REVISION=$(aws ecs register-task-definition --region "$REGION" --cli-input-json "$NEW_TASK_DEFINITION")
NEW_REVISION_DATA=$(echo $NEW_REVISION | jq '.taskDefinition.revision')
NEW_SERVICE=$(aws ecs update-service --cluster $CLUSTER_NAME --service $SERVICE_NAME --task-definition $TASK_NAME --force-new-deployment)
echo "done"
echo "${TASK_NAME}, Revision: ${NEW_REVISION_DATA}"
After this, you need to update the environment variables:
- TASK_NAME=task_name
- SERVICE_NAME=service_name
- IMAGE_NAME=image_name
- CLUSTER_NAME=cluster_name
- REGION=region
- VERSION=version
- ACCOUNT_NUMBER=account_number
And then you run the .sh like this, ./update_task.sh. A0nd that's all!
Top comments (0)