Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Overview

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

Pre-requisites

  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).

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: https://github.com/RasanKhalsa/codepipeline_ecr.git

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.

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.

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

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
phases:
  pre_build:
    commands:
      – “echo Logging in to Amazon ECR…”
      – “aws –version”
      – “aws –version”
      – REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazon.com/$IMAGE_REPO_NAME
      – REPO=”$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com”
      – “COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)”
      – “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:
    commands:
      – “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:
    commands:
      – “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.

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.

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.

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.

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.

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.

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.

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.

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

The post CI/CD pipeline to build, and deploy containerized applications with AWS ECS service appeared first on Rapyder.



This post first appeared on Managed Cloud Services Provider, please read the originial post: here

Share the post

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

×

Subscribe to Managed Cloud Services Provider

Get updates delivered right to your inbox!

Thank you for your subscription

×