Skip to main content
Welcome back. We’ve already split the monolith into microservices. In this lesson we’ll finish by automating the login microservice’s service deployment using AWS CodePipeline and Amazon ECS. What you’ll accomplish
  • Ensure the build creates an imagedefinitions.json artifact required by ECS.
  • Add a Deploy stage to the login microservice pipeline that uses the Amazon ECS deploy action.
  • Verify the new deployment in the ECS console and confirm healthy tasks behind the load balancer.
Prerequisites
  • You already have a working login microservice repository and a CodePipeline with Source and Build stages.
  • You have an ECS cluster and service (task definition with a container name) configured to run the login microservice.
  • You have access to the AWS Console and Cloud9 (or any editor) to update files and push commits.
Open Cloud9 and edit buildspec.yml
  • In the AWS Console, open Cloud9, close open files, and open the buildspec.yml file.
The image shows an AWS Cloud9 IDE interface with a file explorer on the left featuring several project files, and a terminal window below indicating the active environment.
Why imagedefinitions.json is required
  • Amazon ECS’s CodePipeline action expects imagedefinitions.json in the Build artifact. This file maps the container name from your task definition to the pushed ECR image URI so ECS can update the service with the new image.
Example content of imagedefinitions.json:
Add imagedefinitions.json creation to buildspec.yml
  • Add (or update) a post_build step that writes imagedefinitions.json using the built image URI (typically available in an environment variable such as $IMAGE_URI).
Add or update this snippet in buildspec.yml:
  • Replace login-app-container with the exact container name from your ECS task definition.
  • Save the file.
Update the login UI (optional small change)
  • Make a minor UI tweak to confirm deployment behavior. For example, ensure the login button text shows “Login” and add basic styling.
Example style block to include in the template:
Commit and push the changes From the Cloud9 terminal (or your local environment), commit and push:
(Replace master with your branch name if different.) Open CodePipeline and add the Deploy stage
  • In the AWS Console, go to CodePipeline and locate the login-page-microservice pipeline.
The image shows an AWS CodePipeline dashboard with two pipelines, "login-page-microservice" (in progress) and "crypto-app" (succeeded), displaying their execution status and latest revisions.
  • You’ll notice this pipeline currently has Source and Build stages only. Wait for the running execution to finish, then click Edit.
The image shows an AWS CodePipeline interface for a "login-page-microservice" pipeline, with sections for source and build stages. The source shows success, and the build is in progress.
Step-by-step: adding the Deploy stage
  1. Click Add stage and name it Deploy.
  2. In the Deploy stage, click Add action group → Add action.
  3. Set Action provider to Amazon ECS.
  4. For Input artifact, choose the Build artifact (often named something like BuildArtifact or AppBuildArtifact) — this artifact must contain imagedefinitions.json. Do not select the Source artifact.
  5. Select the target ECS cluster (for example, production-cluster) and the service (for example, login-app-microservice).
  6. Set the Image definitions file name to imagedefinitions.json.
  7. Click Done and Save the pipeline.
Trigger a release
  • Back in the pipeline, click Release change to start a new execution. Leave defaults and confirm.
Troubleshooting a failed Deploy action
  • If the deploy action fails, inspect the action execution details to find the cause (most commonly the pipeline cannot locate imagedefinitions.json).
The image shows an AWS CodePipeline interface with a pop-up window displaying action execution details for a failed deployment to ECS, including input artifact, cluster name, file name, and service name.
Common causes and fixes Verify action details
  • Inspect a successful pipeline’s Deploy action to confirm it uses the Build artifact and points to the correct cluster/service and file name.
The image shows an AWS console page with the "Action execution details" window open, displaying information about an ECSDeploy action, including the input artifact, cluster name, file name, and service name.
Pipeline progress and deployment verification
  • After releasing a change, the pipeline should move from Build to Deploy.
The image shows an AWS CodePipeline interface where the "Build" stage has succeeded and the "Deploy" stage is in progress. The interface includes options for managing transitions and viewing pipeline details.
  • Open the ECS console, navigate to the login service, and check the Deployments tab for the new deployment initiated by CodePipeline.
The image shows the AWS Elastic Container Service (ECS) interface displaying deployment details for a service named "login-app-microservice," including deployment status, configuration, and events.
  • Wait for the new task to reach healthy status. Inspect the target group behind the load balancer to view health checks and target registration.
The image shows an AWS Elastic Container Service (ECS) console displaying the health and metrics of a service named "login-app-microservice" with an active status and one healthy target indicated.
  • Copy the load balancer DNS into a browser and refresh. During a rolling deployment you may observe requests temporarily split between the old and new tasks until the older task is drained. This is expected behavior for healthy rolling releases.
Pause automatic deploys (optional)
  • To temporarily prevent automatic deployments, disable the transition from Build to Deploy in CodePipeline. Re-enable it later and provide a reason in the dialog.
Key reminder
When adding the ECS Deploy action, always select the Build artifact as the input and ensure imagedefinitions.json is created in the Build stage. This is the most common cause of ECS deploy failures in CodePipeline.
Next steps
  • Optionally commit simultaneous updates for the login microservice and other microservices (like the product app) to confirm both pipelines build and deploy independently.
  • Review IAM roles used by CodePipeline, CodeBuild, and ECS to verify least-privilege access for automated deployments.
Links and references This completes the automation: committing to your Git repository should now trigger a pipeline that builds the image and deploys it to ECS automatically.

Watch Video