CI/CD pipeline to build, and deploy containerized applications with AWS ECS service

CI/CD pipeline to build, and deploy containerized applications with AWS ECS service
March 9, 2023


Most clients nowadays are moving toward workflows and infrastructure that are containerized. AWS offers native tools and services that clients can use to create CI/CD pipelines for deploying containerized applications. To build and deploy microservices on Amazon Elastic Container Service, this pattern explains how to automatically create the continuous integration and continuous delivery (CI/CD) pipelines and supporting infrastructure (Amazon ECS). These CI/CD pipelines pull updates from an AWS CodeCommit source repository, automatically create the updates, and then deploy the updates to the appropriate environment.

Using the AWS native tools listed below, we will create an end-to-end CI/CD solution for container deployments in this post.

Pipeline components

The AWS pipeline includes the following elements:

· AWS Codecommit

· AWS CodeBuild

· AWS CodeDeply

· AWS CodePipeline

· AWS Artifact Bucket

Pipeline components


  1. Familiarity with AWS Fargate, Amazon ECS, and Amazon Elastic Container Registry (Amazon ECR) is required.
  2. A basic understanding of CI/CD, DevOps, and Docker
  3. An AWS ECS repository (e.g., ecr-demo) needs to be created and an Apache docker image pushed to the ECR repository.
  4. One ECS Fargate Cluster (e.g., web-cluster) is up and running with one ECS task definition (e.g., web-task) and one operational ECS service (e.g., web-service) using the previously prepared docker image.

Steps walkthrough

Create a CodeCommit repository

You can use it as a private version control service to store and manage your Git repositories in the AWS Cloud.

  1. Open the CodeCommit Dashboard in the AWS Console. On the Repositories page, click Create repository.
  2. On the Create repository screen, select Create after giving your repository a name (for instance, demo-repo).
Repository creation

3. After the repository has been created, it must have permissions added and Git credentials created in order to be able to access the CodeCommit.

4. After that, upload the Docker code to the repository. The following is how your CodeCommit repository will appear following a successful code push. Sample source code link:

Uploading docker code to the repository

Creating a CodeBuild

It is a cloud-based build service that is fully managed. It runs unit tests, compiles the source code, and creates artifacts.

1. Select Create Build Project from the CodeBuild dashboard.

2. Include the project name in the project configuration (e.g., ECS-Build). Choose AWS CodeCommit as your source provider, making sure to include the repository name and branch information.

Project configuration

3. In the environment settings, select Managed Image running the latest version of the image and the operating system Amazon Linux 2 with runtime Standard. Make sure the option to grant elevated rights for Docker builds is checked. Next, select the new service role option. Please ensure that the AWS Build role has AWS ECR rights so that it can build and push the Docker image.

Environment settings

4. Declare the following variables in Environment variables; they will be used in buidspec.yml.

Environment variables

5. The buildspec.yml file, which contains the commands used to compile, test, and package the code, is next on the list of parameters. For our purposes, we will write instructions to containerize, build, and submit a Docker image to ECR in the buildspec.yml file. It will look like this:

version: 0.2
      – “echo Logging in to Amazon ECR…”
      – “aws –version”
      – “aws –version”
      – REPO=”$AWS_ACCOUNT_ID.dkr.ecr.$”
      – “IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F\”:\” ‘{print $2}’)”
      – “echo image_tag $IMAGE_TAG”
      – “$(aws ecr get-login –region $AWS_DEFAULT_REGION –no-include-email)”  build:
      – “echo Build started on `date`”
      – “echo Building the Docker image…”
      – “docker build -t $REPOSITORY_URI:latest .”
      – “docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG”post_build:
      – “echo Build completed on `date`”
      – “echo Pushing the Docker images…”
      – “docker push $REPOSITORY_URI:$IMAGE_TAG”
      – “echo Writing image definitions file…”
      – “printf ‘[{\”name\”:\”%s\”,\”imageUri\”:\”%s\”}]’ $CONTAINER_NAME $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json”
      – “cat imagedefinitions.json”artifacts:
  files: imagedefinitions.json

6. In the Artifacts section, choose the Amazon S3 type and the name of the bucket where we will save the artifacts mentioned in the buildspec. Click on Create build project and leave the remaining settings at their default values.

Artifacts settings

Configure AWS CodePipeline for deployment

When there are any updates in the source repository, it will automatically build and deploy our docker container.

1. Select Create pipeline from the CodePipeline console in the AWS Console.

2. Mention the pipeline name in the pipeline settings (e.g., ECS-pipeline). After leaving the advanced options’ default values in place, select Next.

Choose pipeline settings

3. In the source stage, choose AWS CodeCommit under the Source provider heading. Choose the repository name we previously created by clicking Repository name (e.g., demo-repo). Under the branch name, select “master”. Under Change detection options, leave everything as it is. This allows CodePipeline to use Amazon CloudWatch Events to detect changes in your source repository, and then choose Next.

AWS source settings

4. In the build stage, choose AWS CodeBuild. Mention the project name (such as ECS-Build) that was formed in the previous stage. Add the build stage parameters and then choose Next.

AWS CodeBuild

5. In Add Deploy Stage, under Deploy provider, choose AWS ECS. Choose the cluster name (such as “web-cluster”) and service name (such as “web-service”) that were built as pre-requisites, and then mention the JSON file name (such as “imagedefinations.json”) as part of the code for code commit. An image definition document is a JSON file that describes your Amazon ECS container name and the image and tag. Then click on Next.

Deploy Stage, under Deploy provider

6. After reviewing the data in the Review stage, select Create pipeline.

7. After completing the creation stage, the pipeline will be kicked off. The code can be downloaded from the CodeCommit repository. Next, CodeBuild and CodeDeploy are used to build and deploy a Docker container to the ECS Fargate service with the new image. On the AWS CodePipeline Console, you can see the status, success, and failure notifications.

Pipeline creation

8. The results will then be verified. Open your web browser and paste the ECS Container’s public IP address. The container web page is displayed in the browser.

AWS container webpage

9. The execution logs of the pipeline are recorded in the pipeline information after it has been completed. You may track the pipeline’s individual steps, the resources that have been deployed, and the progress here.

execution logs of the pipeline

For the purpose of deploying microservices on the Fargate ECS service, we have successfully built a CI/CD pipeline utilizing the AWS developer tools. When a code change is made to the CodeCommit repository, the pipeline will launch immediately and deploy the modification to the AWS ECS fargate cluster with the new docker image.

Written by – Manish Juneja

Cloud Computing Insights and Resources

Challenges Faced by Gaming Companies While Adopting Cloud Services

Challenges Faced by Gaming Companies While Adopting Cloud Services 

With the advancement in technology and the progress in cloud infrastructure, the cloud gaming industry is growing at a lightning …

Challenges Faced by Gaming Companies While Adopting Cloud Services  Read More »

5 Trends in Data Analytics Impacting the Cloud World 

5 Trends in Data Analytics Impacting the Cloud World 

What is Cloud data & analytics?  Cloud analytics is the process of analysing data stored in the cloud. It is …

5 Trends in Data Analytics Impacting the Cloud World  Read More »

How Microsoft Azure Cloud Services Different From Competitors

How Microsoft Azure Cloud Services is Different From its Competitors?

Microsoft Azure is one of the eminent cloud computing platforms in the market created by Microsoft for building, deploying, and …

How Microsoft Azure Cloud Services is Different From its Competitors? Read More »