latest image tag can be convenient during early development, but it causes problems during ECS rolling updates because latest is mutable and multiple revisions may reference the same tag. In this lesson we fix that by ensuring the ECS task definition references the image built from the exact commit hash instead of latest. This produces immutable, reproducible deployments and prevents image ambiguity during rolling updates.
Revert any temporary changes in app.py so the app behaves as originally intended. For reference, here is the login route used by the app:
- Replace the
imagefield in the task definition with a placeholder (e.g.REPOSITORY_URI_PLACEHOLDER). - In CodeBuild’s
buildspec.yml, compute a short image tag from the commit SHA, build and push bothlatestand the commit-tagged image to ECR. - Use
sed(or another replacement method) to replace the placeholder intask-definition.jsonwith the fullREPOSITORY_URI:IMAGE_TAG. - Register the updated task definition with ECS so the service uses the immutable, commit-specific image.
Compute
IMAGE_TAG from the first 7 characters of the commit SHA (CODEBUILD_RESOLVED_SOURCE_VERSION) to create a short, stable, and human-readable tag. Pushing both latest and the commit-tagged image keeps latest convenience during development while ensuring deployments reference an immutable tag.IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)— derives a short tag from the build’s commit SHA.- Building and tagging both
$REPOSITORY_URI:latestand$REPOSITORY_URI:$IMAGE_TAGpreserveslatestwhile creating an immutable image reference for deployments. sed -i "s|REPOSITORY_URI_PLACEHOLDER|${REPOSITORY_URI}:${IMAGE_TAG}|g" task-definition.json— replaces the placeholder in the task definition with the commit-tagged image before registering the task definition.aws ecs register-task-definition --cli-input-json file://task-definition.json— registers the new task definition revision in ECS which points to the specific image tag.
- Build the Docker image.
- Tag and push both
latestand commit-tagged images to ECR. - Replace the placeholder in
task-definition.json. - Register the updated task definition in ECS.
sed replacement followed by ECS task definition registration. Example of a registration response (trimmed):
- Inspect CodeCommit to confirm the commit that triggered the build and to capture the exact commit hash used for your image tag.

- In the ECS console, open Task Definitions and verify the new revision JSON references the specific commit-tagged image in the
imagefield:
- Update the ECS service to use the latest task definition revision: in your cluster, select the service -> Update Service -> pick the latest task definition revision -> Update Service. ECS will start a new deployment using the commit-tagged image.

- After the deployment finishes and new tasks are in
RUNNINGstatus, validate the application through the ALB (copy the ALB DNS name and open the site). - Verify the app workflow: select a crypto coin, provide the name and shipping address, set quantity, submit order, and return to the product page.
- We replaced the
imageURI in the task definition with a placeholder and then programmatically replaced it with the commit-specific image URI during the build pipeline. - CodeBuild builds and tags images with both
latestand the short commit hash. - The replacement and ECS task definition registration ensure deployments reference immutable image tags (commit hashes), preventing rolling-update issues caused by the mutable
latesttag.
- Use commit-based or semantic version tags for production deployments to ensure repeatability and easier rollbacks.
- Ensure CodeBuild has the required IAM permissions to push to ECR and register task definitions in ECS.
- If you prefer not to use
sed, consider using a templating tool (e.g.,envsubst,jq, or a small script) for cross-platform robustness.
Make sure your CodeBuild project has the correct IAM permissions and environment variables (
REPOSITORY_URI, AWS_DEFAULT_REGION, etc.). Mistakes in permissions or region configuration will cause push or register-step failures.