I have pushed the initial docker image into repository and created AWS Fargate using the image, is there any way to update the image as there are certain changes in my docker image.
5 Answers
As simple as:
aws ecs update-service --cluster <cluster> --service <service> --force-new-deployment
-
it will change IP and if Route 53 domain points to previous IP - it will stop work. should I put Load balancer as alias to prevent it? Commented Jun 30, 2021 at 17:52
See AWS documentation:
If you have updated the Docker image of your application, you can create a new task definition with that image and deploy it to your service.
Note
If your updated Docker image uses the same tag as what is in the existing task definition for your service (for example, my_image:latest), you do not need to create a new revision of your task definition. You can update the service using the procedure below, keep the current settings for your service, and select Force new deployment. The new tasks launched by the deployment pull the current image/tag combination from your repository when they start. The Force new deployment option is also used when updating a Fargate task to use a more current platform version when you specify LATEST. For example, if you specified LATEST and your running tasks are using the 1.0.0 platform version and you want them to relaunch using a newer platform version.
https://docs.aws.amazon.com/AmazonECS/latest/userguide/update-service.html#update-service
-
2This link is no longer good, it goes through multiple redirects to the "What is Fargate" guide now.– SparrCommented Dec 7, 2023 at 18:16
Create a new version of the task definition and update the container with latest labels and update the service.
You can write the configuration file once you created a cluster using the default-launch-type
as FARGATE for your application and define the respective parameters in your task definition i.e. ecs-params.yaml
Here is one file for the nginx:latest
image which is stored in Amazon ECR.
version: '2'
services:
web:
image: account-id.dkr.ecr.ap-southeast-1.amazonaws.com/nginx:latest
ports:
- "80:80"
logging:
driver: awslogs
options:
awslogs-group: awslogs-web
awslogs-region: ap-southeast-1
awslogs-stream-prefix: web-nginx
You simply change the image and you could get the updated image into your deployment as you update the service inside your cluster.
If you have updated the Docker image of your application, you can create a new task definition with that image and deploy it to your service. The service scheduler uses the minimum healthy percent and maximum percent parameters (in the service's deployment configuration) to determine the deployment strategy.
Note: The Execution Role
in task definition gives permissions to pull the images from container registry.
You could find the doc guide here, AWS ECS Update Service
-
As explained in the ECS Update Service doc, note that if your image & task definition use the same tag as before (e.g., latest), then you can just do an update of the ECS service and check the "Force new deployment" checkbox so that your new image gets deployed. Commented Feb 14, 2019 at 14:57
If you need to update the images with a specific tag like me, I created a shell script that update my services using GitLab, this is useful because I can easily revert the image tag to the previous one if necessary.
First you will need to install jq. If you're using amazon/aws-cli image it can be done with yum install jq -y
.
#!/usr/bin/env bash
set -e
aws ecs describe-task-definition \
--task-definition your-service \
--query "taskDefinition" > current_task_definition.json
# Update the container image in the task definition JSON using jq (a lightweight and flexible command-line JSON processor)
< current_task_definition.json \
jq --arg image "$ECR_REGISTRY/your-docker-image:v-$CI_PIPELINE_IID" \
'
del(.taskDefinitionArn) |
del(.revision) |
del(.status) |
del(.requiresAttributes) |
del(.compatibilities) |
del(.registeredAt) |
del(.registeredBy) |
.containerDefinitions[0].image=$image
' \
> updated_task_definition.json
# Register the new task definition revision with the updated image
REVISION=$(aws ecs register-task-definition --family your-service \
--cli-input-json file://updated_task_definition.json --query "taskDefinition.revision")
aws ecs update-service --cluster your-cluster --service your-service \
--force-new-deployment --task-definition "your-service:$REVISION"
Note that the line jq --arg image "$ECR_REGISTRY/your-docker-image:v-$CI_PIPELINE_IID"
is where the name of the image is generated, and it's set in the line .containerDefinitions[0].image=$image'
. If your service has more than one task just add more arguments, like
jq --arg python "$ECR_REGISTRY/python-api:v-$CI_PIPELINE_IID" --arg nginx "$ECR_REGISTRY/nginx:v-$CI_PIPELINE_IID"
...
.containerDefinitions[0].image=$python | .containerDefinitions[1].image=$nginx'
The del(...)
remove the invalid attributes from the JSON, it's necessary to remove then otherwise the validation fails.
-
1Great answer that deserves some upvotes. Instead of downloading and modifying the current task definition I usually keep a
taskdefinition.json
file in the git repository and then inject the image tag usingenvsubst
. Commented Mar 26 at 22:47