Everything about AWS ECS(with hands-on)

anubhav jhalani
4 min readDec 9, 2022

--

This is final article of the series Everything about AWS ECS where I am going to show a pipeline example to deploy and update ECS cluster, Service and tasks. For the other articles in this series please click on following links:

  1. ECS Overview and Task Definition
  2. Cluster
  3. Service
  4. Load Testing
  5. CI/CD Pipeline

I am going to explain one of the deployment strategies to deploy and update ECS cluster, Service and tasks automatically using GitHub Actions whenever there is a new commit in GitHub repository. This article also illustrates how AWS CodeBuild can be used with GitHub Actions to execute application tests as part of a complete CI/CD pipeline.
I am going to use a custom docker image instead of official nginx image to make a generic pipeline.

I am assuming that you are familiar with Github Actions and AWS CodeBuild. If you are not, then please learn about these tools before you go further in this article:

The following diagram shows the high-level architecture that we implement. This architecture represents a complete CI/CD pipeline that uses a GitHub workflow to automatically building and deploying an application to ECS for every commit to the repository and uses CodeBuild to execute application tests.

Testing with AWS CodeBuild

In our workflow, CodeBuild uses webhooks to trigger a build of the source code every time a code change is pushed to the GitHub repository.

You can use CodeBuild for more than just compiling your application code or building an application container image. With CodeBuild, you can easily run a variety of tests against your code, such as unit tests, static code analysis, and integration tests.

Here in our workflow, we don’t use CodeBuild as the “build” action of the CI/CD pipeline. Instead, we use CodeBuild as an environment to execute tests and then CodeBuild provides the status of these tests to GitHub. If the tests do not pass, CodeBuild marks the build as Failed and this status is reported to GitHub.

Creating a CodeBuild Project

Here is my Codebuild configuration:

Project Configuration
Source Configuration
Source Configuration
Environment Configuration
Buildspec Configuration
Batch Configuration
Artifact Configuration
Logs Configuration

Creating Github Actions Workflow

First clone the GitHub repository https://github.com/anubhav1/ecs-devops-sandbox and there you will find .github/workflows file which has the workflow defined for automatic deployment.

In practice, we want our GitHub workflow to build and deploy a new container to ECS only if the tests executed in CodeBuild were successful. The CodeBuild project we created earlier reports the success or failure of its build execution to GitHub for every commit. If the CodeBuild project fails for a commit, GitHub marks this commit with a status of failure. If the CodeBuild project has not yet reported its success or failure, GitHub marks this commit with a status of pending (learn more about GitHub commit statuses).

In our pipeline, we are executing the GitHub workflow and the CodeBuild project in parallel on each commit to the master branch. To ensure that our workflow does not deploy anything on AWS if a commit does not pass the required tests, we introduce an additional step into the GitHub workflow to check the status of the commit.

You can see the execution of GitHub workflow on Actions tab:

--

--

No responses yet