{{announcement.body}}
{{announcement.title}}

Continuous Integration and Delivery for Microservices on AWS ECS

DZone 's Guide to

Continuous Integration and Delivery for Microservices on AWS ECS

Learn how to run continuous integration and delivery for your microservices projects to enable DevOps processes.

· Microservices Zone ·
Free Resource

Introduction

In my previous article, I discussed how to set up AWS Elastic Container Service (ECS) for microservices using a Fargate launch type. In this article, I'll outline a step-by-step procedure to create a continuous integration & delivery pipeline for ECS. This would essentially be a continuation of the previous article and would use the same ECS setup that was configured.      

The pom File

I have a microservices application built in Spring Boot. I used Maven as my build tool and the Spotify plugin to build my Docker image. Following are the relevant sections from my pom file that show the complete Docker-related configuration:

<plugin>
  <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
      <imageName>886236829342.dkr.ecr.us-east-2.amazonaws.com/${project.artifactId}</imageName>
      <baseImage>java</baseImage>
      <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
      <registryUrl>XXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com</registryUrl>
      <resources>
        <resource>
          <targetPath>/</targetPath>
          <directory>${project.build.directory}</directory>
          <include>${project.build.finalName}.jar</include>
        </resource>
      </resources>
      <forceTags>true</forceTags>
      <imageTags>
        <imageTag>${project.version}</imageTag>
        <imageTag>latest</imageTag>
      </imageTags>
    </configuration>
</plugin>

The imageName is the ECS registry URL followed by the project artifact id. The ECS registry URL is typically of the following format: <12 digit AWS account id>.dkr.ecr.<AWS region> .amazon.com . The command to execute the application goes into entryPoint. Once the Docker image is generated, two tags will be created: the version number and the "latest." Setting forceTags to true will ensure that tags are overwritten; that is, the latest and the greatest version will always carry the "latest" tag.

The Build Specification

In addition to my pom file, I have a buildspec.yml file in my project root folder. It's important to place this file in the root folder as AWS CodeBuild expects this file to be in that location. This file contains the build script that will use Maven to build and push a Docker image to our repository. Following is the script that I used:

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
      - REPOSITORY_URI=XXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/fargate-checkin
      - IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - echo $IMAGE_TAG
  build:
    commands:
      - echo Build started on `date`
      - echo Starting Java Build...
      - mvn clean package docker:build -DpushImageTag
  post_build:
    commands:
      - echo Writing image definitions file...
      - echo $REPOSITORY_URI
      - echo $IMAGE_TAG
      - printf '[{"name":"fetch-booking-container","imageUri":"XXXXXXXXXXXX.dkr.ecr.us-east-2.amazonaws.com/fetch-booking:latest"}]' > imagedefinitions.json
artifacts:
  files:
    - imagedefinitions.json

This is essentially a CloudFormation template that will be used to orchestrate the build and deployment. In the pre-build phase, it connects to the ECS repository. The get-login returns a command that is subsequently used to login to the ECS repository. In the build phase, it uses Maven to build the project, create a Docker image, and push the image to the repository. Finally, in the post-build phase, it creates a definition of the Docker image and writes it into a file called imagedefinitions.json

The next step is to jump into AWS CodeCommit and create a repository. Let’s call it hello-fargate-code. Once that’s done, the project has to be committed to this repository. We are now ready to build our pipeline.

The CI/CD Pipeline

Step 1: Configure the Source

On the AWS console, browse to CodePipeline > Create Pipeline.

Enter a name for the pipeline and move to the next page.

Image title

On the next page, select AWS CodeCommit as the source provider. Select the repository hello-fargate-code, the default branch, and click on "Next step."

Image title

Step 2: Configure the Build

Select AWS CodeBuild as the build provider. In the configuration section, opt to create a new project and enter a project name.

Image title

In the next section, we need to instruct CodeBuild on how to build the project. We already have a build script that uses Maven as well as Docker commands. Select Ubuntu as the operating system.

Selecting the runtime for this one is a little tricky. Please be aware that selecting Docker as the runtime will result in a build error, as the Maven commands will fail without a Java runtime. On the other hand, selecting Java would result in the failure of the Docker commands. The way out is to select Java as the runtime and let CodeBuild know that we need to generate a Docker image as well. To do that, scroll down to the Advanced section and check the Privileged box. This will ensure that our build script is executed successfully.

Image title

Image title

Clicking on "Save build project" will now save the project. Clicking on Next step will take us to the deployment phase.

Step 3: Configure the Deployment

On the deployment page, select Amazon ECS as the deployment provider. The image definitions file mentioned in the build script to be generated as part of the build has to be entered as the image filename.

Image title

That’s it. Once this pipeline is created, it’ll start a build with the current code in the repository and deploy it to our ECS cluster. The ECS tasks have a name followed by a colon (:) and a version number. Whenever a new version of a Docker image is pushed to the ECS repository and new tasks are deployed, the version number is auto-incremented. Subsequently, whenever there’s a code check-in, a successful build will result in a new deployment and a new version of tasks will be created.

Concluding Remarks

As discussed in this article, the tools provided by AWS make container deployment pretty straightforward. On top of that, the ability to create an automated pipeline for continuous integration and delivery takes away the hassles of deployment and lets developers focus on their code.

Topics:
aws ecs, ci/cd, containers, continuous delivery, continuous integration, devops, docker, microservices, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}