CICD Pipeline in AWS using ECS

shahebaz sayed
5 min readMar 5, 2020

--

This is a small Example for setting up a CICD pipeline in AWS using Cloud native services and git.

The motive of this Blog is to just give an outline about Automate deployment of a containerize application in ECS (Elastic Container Service) which is a cloud native service from AWS for container orchestration

Before you go though the detail, I will explain a basic concept of these services. However, I think you can access the AWS Docs directly to understand deeply.

Services Prerequisites:

Code Build

CodeBuild is a build service. We can define the environment such as Python, Ruby , Docker and Go. For instance, our application is written by Python. And we need to an environment to run unit test when a developer commit code. If the unit test fails , there is an alarm to notify other developers. We can configure some tasks such as building artifacts, building docker images and deploy processes.

AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers. AWS CodeDeploy makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of updating your applications.

CodePipeline is a service which combine CodeBuild, CodeDeploy, and Source by order. For example, in step one, we can configure where contains source code such as Github, CodeCommit , S3 or Bitbucket. In step 2, we can configure CodeBuild to run Unit Test. In step 3, we can configure CodeBuild to build artifacts . In step 4, we can configure build docker images. In step 5, we can configure CodeDeploy to deploy the application.

ECR is like a docker hub where we can save Docker images. We can pull and push docker images from ECR.

ECS is a docker orchestration tools. It has a range of servers which is called Workers. We can scale up and scale down containers, and also workers ( servers ). It has a user interface where we can manage our infrastructure resources such as Ram, CPU and Container merits easily and effectively. Moreover, it can integrate with other services like CloudWatch Logs for logging.

AWS CloudFormation allows you to use programming languages or a simple text file to model and provision, in an automated and secure manner, all the resources needed for your applications across all regions and accounts. Here we have deployed ECS cluster using CFT

I assume that you have basic knowledge of AWS services like ECR, ECS, and Docker. I will focus on CodePipeline, CodeBuild to demonstrate how to apply CI/CD with AWS services.

CICD in AWS Architecture

So this is a basic CI/CD architecture to demonstrate how to deploy a Sample application when developers commit the source code and the application is containerized and deployed in ECS cluster.

  1. Source Stage: The developer will commit code on the master branch of the Github/Code Commit from AWS as a source repository along with the application code,Buildspec.yml file, Cloudformation template are also in root directory.
  2. Code Repository:My GitHub is now the part of code pipeline as its authenticated using Webhook and my git hub credentials.In Pipeline we have other Cloud native tools integrated.Pipeline is triggered whenever developer is committing the changes into his code.
  3. Build Stage: After the source stage the build stage will get triggered.Build process will run using the buildspec.yml file in this file developer has given dockerfile to build image and push it to ECR .Basically we are building image,containerizing it and pushing it to ECR.
  4. Deploy Stage: This stage is integrated with cloudformation template as a deploy provider. This template is use to create or update ECS cluster.It also create taskdefiniton and services and assign it to ECS cluster
  5. Pulling images: ECS task will get executed and pull the latest images(tag) from the ECR repo

Codes

1: BuildSpec.yml

  1. Variables will be used in the build phase of this file.
  2. In Prebuild phase we are updating environment and installing jq which is need for json manipulation.Docker client of build env is authenticating ECR repo
  3. Build Phase docker image is created and getting tagged
  4. docker image is uploaded to ECR
version: 2.0
env:
variales:
AWS_DEFAULT_REGION:"ap-south-1"
AWS_ACCOUNT_ID:"123456789"
IMAGE_REPO_NAME:"Your ECR repo name"
IMAGE_TAG:"Latest"
phases:
pre_build:
commands:
- sudo apt-get update -y && apt-get install jq-y
- echo logging into ECR
- $(aws ecr get-logn --region $AWS_DEFAULT_REGION)

build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG
$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazon.com/$IMAGE_REPO_NAME:$IMAGE_TAG

post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
-docker push
$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazon.com/$IMAGE_REPO_NAME:$IMAGE_TAG

2: Dockerfile

  1. Using base image as maven with jdk
  2. installing dependencies for mongodb server
  3. we add application code lying in the build env to app folder
  4. changing directory to app and run mvn package
  5. exposing port 8080
  6. Entrypoint command will get executed when the container will get launched
FROM maven: 3.5.0-jdk-8RUN apt-get update -y && apt-get install mongodb-server -yADD ./APPWORKDIR /AppRUN mvn package -DskiptestsEXPOSE 8080ENTRYPOINT service mongodb start && java -jar target/my-first-app-1.0-SNAPSHOT-fat.jar

--

--