Skip to main content
Welcome — in this lesson we’ll cover the role image tags play when building and deploying containerized applications. You’ll learn a practical tagging strategy that balances convenience and traceability, and see an example CodeBuild buildspec that implements it.

Why tags matter

When you push images to a registry (for example, Amazon ECR), tags determine how images are referenced by deployments and teams. Two complementary tagging patterns are common:
  • latest (or a human-friendly semantic version): convenient for quick testing and ad-hoc deployments, but mutable — each push retags latest.
  • Immutable identifier (short commit hash, full digest, or CI build number): provides traceability and safe rollbacks because it points to a specific build artifact.
Adopting both patterns gives you the convenience of latest plus the safety of an immutable reference for production and auditability.

Typical CI flow

In this example we use AWS CodeBuild to:
  1. Log into ECR.
  2. Derive an image tag from the commit hash (fallback to latest).
  3. Build the image and tag it as both latest and the commit-hash tag.
  4. Push both tags to ECR and register the ECS task definition.
Here’s a representative CodeBuild buildspec.yml:
Notes about the script:
  • COMMIT_HASH extracts a short commit ID from the CodeBuild environment variable CODEBUILD_RESOLVED_SOURCE_VERSION.
  • IMAGE_TAG becomes the short commit hash when available; otherwise, it falls back to latest.
  • Two tags are pushed so you can reference images by latest for quick tests and by commit-hash for production rollbacks and audits.
Why push both tags? latest makes iterative testing easier; the commit-hash tag provides an immutable reference for deployments and rollbacks. Below is an example ECR view where an image has both a commit-hash tag (e.g., EB7245DB) and the latest tag.
The image shows a screenshot of the Amazon Elastic Container Registry (ECR) interface, displaying a list of container images under the repository "cryptoproject," including details such as image tags, push dates, sizes, and vulnerabilities.
If you inspect your source repository you can confirm the commit hash used to tag the image. In this case the CodeCommit commit EB7245DB matches the image tag in ECR.
The image shows an AWS CodeCommit page for a repository named "aws-microservice-project," displaying a list of commits with details such as commit ID, message, date, author, and committer.

Benefits of tagging with commit hashes

  • Traceability: Identify the exact source commit that produced a deployed image.
  • Rollbacks: To revert, update the ECS task definition to reference the REPOSITORY_URI:EB7245DB tag (or whichever commit tag you want) and redeploy. This ensures ECS pulls the image built from that commit.
  • Auditing: Correlate images in ECR with repository history and CI runs to support compliance and incident investigations.

Quick reference: tag types

Example rollback instructions

To roll back an ECS service to an image tagged with a specific commit:
  1. Edit the task definition to reference the exact image tag, e.g. 123456789012.dkr.ecr.us-east-1.amazonaws.com/myrepo:EB7245DB.
  2. Register the new task definition revision:
    • aws ecs register-task-definition --cli-input-json file://task-definition.json
  3. Update the service to use the new task definition revision:
    • aws ecs update-service --cluster my-cluster --service my-service --task-definition my-task:3
This ensures the cluster pulls the image built from the commit you specified.
Best practice: Tag each build with both a human-friendly tag (for example, latest or a semantic version) and an immutable identifier (for example, the short commit hash). Push both tags to your registry so you get the convenience of latest plus the traceability and safety of an immutable tag.
Do not rely solely on latest in production. latest is mutable and can make rollbacks or incident investigations difficult because it does not uniquely identify a build artifact.
Thanks for reading — use immutable tags alongside human-friendly tags to balance convenience, safety, and auditability in your CI/CD pipelines.

Watch Video