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:- Build, tag, and push your Docker image to Amazon ECR using CodeBuild.
- Keep an ECS task definition (Fargate example) in source control.
- Register the task definition from CodeBuild using
aws ecs register-task-definition. - (Optional) Inject the CI image tag into the task definition before registering.
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 representativebuildspec.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 atask-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 CLIregister-task-definition in the post_build phase. The important command is:
post_build section with registration looks like this:
Inject the built image tag into the task definition (recommended)
If yourtask-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:
: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 theaws 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 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.
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.