Skip to main content
Welcome — in this lesson you’ll learn how to register an Amazon ECS task definition as part of a CI/CD pipeline. Instead of configuring the task definition in the ECS console, keep a task-definition.json file in your repository and have CodeBuild register it on every push. This approach ensures your task definition is versioned with your code and that each change can create a new ECS revision automatically.

Overview

Steps covered here:
  1. Build, tag, and push your Docker image to Amazon ECR using CodeBuild.
  2. Keep an ECS task definition (Fargate example) in source control.
  3. Register the task definition from CodeBuild using aws ecs register-task-definition.
  4. (Optional) Inject the CI image tag into the task definition before registering.
Useful references:
Store your ECS task definition in source control so its changes are auditable and reproducible. Use the CI pipeline to register new revisions automatically.

Example CodeBuild buildspec (build, tag, push)

This representative buildspec.yml logs into ECR, builds and tags the image, and pushes both latest and a short commit-hash tag. Note the use of CODEBUILD_RESOLVED_SOURCE_VERSION to derive a short commit hash for the image tag.

Task definition file (task-definition.json)

Create a task-definition.json file in the repository. The example below is a basic Fargate task definition configured to use awslogs. Adjust image URI, container name, CPU/memory, and other values to match your environment.

Register the task definition from CodeBuild

To register the task definition as part of the build, call the AWS CLI register-task-definition in the post_build phase. The important command is:
A minimal post_build section with registration looks like this:
If your task-definition.json contains a static :latest image, you can update the file in CI to reference the pushed commit-tagged image before registration. This ensures the registered task definition references the exact image pushed by the current build. Example using jq to update the JSON and register:
This pattern avoids registering a task definition that still points to :latest and gives you an explicit immutable image reference per revision.
Ensure the CodeBuild service role has permissions to call ecs:RegisterTaskDefinition and ecr:GetAuthorizationToken / ecr:BatchCheckLayerAvailability / ecr:GetDownloadUrlForLayer / ecr:PutImage as needed. Missing IAM permissions will cause the build to fail.

Files in the repository

Commit and push

Commit the task definition and buildspec to your repository so CodeBuild picks up the changes:

Trigger a build and inspect results

Start a build in CodeBuild (or let your CI trigger it). When the build completes, the logs will show the aws ecs register-task-definition call and the CLI will return the registered task definition metadata similar to this example:

Verify in the ECS console

Open the ECS console, navigate to Task Definitions, and confirm that a new revision (for example, revision 2) has been created. To deploy the new revision, update your ECS service to use the latest revision and perform a deployment.
The image shows the AWS Elastic Container Service (ECS) console, displaying information about a cluster named "ProductionCluster" with 1 running service and task status.
Storing the task definition in source control and registering it during CI achieves two main goals:
  • The task definition becomes part of your codebase and change history.
  • Any change to the task definition can automatically produce a new ECS revision that you can deploy by updating the service.
Automating the service update to trigger a deployment (for example via aws ecs update-service) is possible and commonly done in CD pipelines, but is outside the scope of this lesson. That’s it for this lesson — thank you for reading.

Watch Video