End-to-End CD Pipeline: Amazon ECS Deployment using AWS CodePipeline

Azzan Amin
TheLorry Data, Tech & Product
8 min readFeb 15, 2022

Let’s automate our GitHub project deployment to Amazon ECS using AWS CodePipeline!

Photo by Mathew Schwartz on Unsplash

Automated software deployment has been an essential element in making the production cloud-application delivery process more efficient as it amazingly improves tons of DevOps tasks. This process can be refers to the term called Continuous Deployment (CD).

Continuous Deployment is known as a software development strategy for DevOps where the code changes and updates are automatically built, tested, and prepared for release to production without explicit approval from a developer. This modern practice makes the entire software release process automated continuously.

For those who are working with Amazon ECS, they must be experiencing some repetitive work when every new release is being executed. For example, starting from pushing code changes to GitHub, building and pushing the new images to ECR, creating new task definition in ECS Cluster, updating the service, and monitor the ECS task, testing the application, and more. These processes are tedious as the time taken to perform these tasks might take longer than expected and reduce the work productivity of a developer.

The good news is, these manual processes can be automated by using the AWS CodePipeline!

In this article, we will guide you through a demo on how to set up an end-to-end continuous deployment pipeline using AWS CodePipeline to deploy our GitHub application on ECS. It will definitely help us to remove all the repetitive processes to just only one click away deployment.

Without further ado, let’s jump in!

Table of Contents

  1. Amazon Code Pipeline Building Blocks
  2. The Pre-requisites
  3. Add Build Specification file
  4. Create New Pipeline
  5. Configure the Source Provider
  6. Set Up the Build Stage
  7. Choose the Deploy Provider
  8. Test the Pipeline

1. AWS CodePipeline Building Blocks

AWS CodePipeline is known as a fully managed service that reliably helps to automate our release pipelines and infrastructure updates into production safely and quickly in a sustainable manner. It performs the automation process for building, testing, and deploying the phases of the release process every time there is a commit code based on the release models that we define. It promotes faster time to market our production application and also contributes to lesser risk releases.

Below is the example AWS CodePipeline building blocks that we will be using in this article where GitHub, CodeBuild, and ECS are included.

Figure 1: Building Blocks of AWS CodePipeline

There are three main building blocks in the above CodePipeline which are:

  • Source Provider: Holds the version of a source change that triggers a pipeline execution. Examples of source providers other than GitHub are Amazon S3, ECR, CodeCommit &, etc.
  • Build Provider: Compiles the source code, builds docker images, and produces software packages that are ready to deploy.
  • Deploy Provider: Deploy the updated image to Amazon ECS.

2. The Pre-requisites

In order to follow this article walkthrough, you might need to configure and get these steps to be done first in order to understand the flow properly.

  1. Project GitHub Repository — It works as the source provider that will trigger the pipeline. You can use the GitHub project as your test project. This repository already included the important files for our CodePipeline project.
  2. Docker Image deployed on ECR, ECS Cluster and ECS Task Definitions already up and running — You may refer to the article below in order to guide you on how to deploy docker images to ECR, create ECS Task Definition, and ECS Cluster.

If you have already done these steps, you’re good to go!

3. Add Build Specification file

A buildspec is a series of build commands together with related configurations in YAML format, that CodeBuild uses to run a build. We can include a buildspec as part of the source code or we can define a buildspec when we create a building project. For this article, we will be creating the buildspec file and locating it in our source code root folder.

There are few variables which is retrieving values from CodePipeline build stage environment variables. Here are the variables:

  • $docker_username
  • $docker_password
  • $ecs_container_name
  • $ecs_uri

Later in the build stage, we will define the values for these variables. Anyway, let’s create our CodePipeline first.

4. Create New CodePipeline

Go to AWS Management Console and log in using our AWS Account. Then, proceed to CodePipeline in the console and click on Create Pipeline.

Figure 2: Create CodePipeline

Let’s give a name to our pipeline. For service role, if you do not have any suitable roles for this particular service, just choose the New service role and then click on Next.

Figure 3: Pipeline Settings

5. Configure the Source Provider: GitHub

In the source stage, choose GitHub Version 2 as the source provider. For connection, we will need to connect our GitHub. If there is no connection is established, we can click on the Connect to GitHub and kindly follow the steps instructed by AWS.

Figure 4: GitHub as Source Provider

After successfully connecting to our GitHub Account, we need to select the code repository and the branch. Then, we can leave the other settings as default and click on Next.

Figure 5: Choose repository and branch for the source provider

6. Set Up the Build Stage

Add build step by choosing AWS CodeBuild as the build provider and our preferred region. Then, click on Create project.

Figure 6: Add Build Stage

Once we clicked on Create project, it will redirect us to a new tab/window. We need to fill in some information for our CodeBuild Project.

Figure 7: Create Build Project

In the Environment section, you can configure the settings as below. We will be using Amazon Linux 2 as our operating system. Please make sure to check the Privileged checkbox in order to allow CodeBuild to build your docker image with elevated privileges. It is very crucial for CodeBuild to have this privilege otherwise the docker build process cannot be done.

Figure 8: Build Project Environment

For service role, if you do not have any suitable roles for this particular service, just choose the New service role and then click on Next.

Figure 9: CodeBuild service role

You can leave the other settings as default and proceed to click on Continue to CodePipeline.

Figure 10: Build Project Default settings

Finishing the Build Stage

Once you successfully create the CodeBuild project, the green alert will prompt.

Figure 11: Successfully created CodeBuild Project

Then, we will need to define some environment variables in our build stage as it is essential for our buildspec file as we have been discussed in the section earlier. These are the variables that we need and click on Next once we have defined all of them.

Figure 12: Build Stage Environment Variables

7. Choose the Deploy Provider

Select the region where our cluster resides, and choose the cluster name and service name. Kindly note that the image definition file name must be the same as the artifacts file name defined in the buildspec.yaml. Once all is good, click on Next.

Figure 13: ECS as Deploy Provider

After that, we need to review our pipeline configurations and click on Create Pipeline to complete the pipeline creation.

Figure 14: Review pipeline creation

8. Test the Pipeline

Our end-to-end native AWS continuous deployment is now set up and now, let’s try to push some code changes in our repository. Once we push the commit code to GitHub, it will trigger the pipeline.

Here is what it looks like.

Figure 15: GitHub new push commit triggers the pipeline

Absolutely awesome!

Every update that triggers the ECS deployment will be a rolling update by default. It means that a new version update will be deployed alongside the previous version of the application. The older version will be terminated once the latest version is stable.

When our continuous deployment pipeline is succeeded, it will show this in our AWS CodePipeline.

Figure 16: New release succeeded

Congratulations! You have successfully create your own continuous deployment pipeline for your very own applications.

Summary

In conclusion, AWS CodePipeline definitely could help to provide a complete end-to-end continuous deployment pipeline where developers only need to commit changes to a source repository and then, the deployment part will be fully automated in a scalable way. It solves the problems that we highlight as well as improving the productivity and saves time for the developers especially DevOps.

In this article, we discussed the building blocks of AWS CodePipeline, creating the CodePipeline by setting up GitHub as our source provider, the build stage, and ECS as the Deploy provider. The build specification file is also crucial to be included in our code project as it’s define the steps for the CodeBuild to perform tasks that needs to be done.

We hope this article will help you to build your own CD Pipeline using CodePipeline for your own awesome GitHub projects to ECS.

Thank you for reading.

Peace! ✌️

--

--