Reduce Build Time with AWS CodeBuild and S3

Reduce Build Time with AWS CodeBuild and S3

If you are not aware of AWS CodeBuild, it's a fully managed continuous integration service that runs your builds for you.

With AWS CodeBuild you are charged by the minute for the compute resources that you use, and in this tutorial we will see how we can reduce our build times to save costs.

What are we doing today

In this tutorial, we will demonstrate how to save costs by reducing our build time by introducing S3 for caching our maven build cache.

So our first build's time will be the normal time as it downloads all the dependencies using maven, then once everything is finished it will push the build cache to AWS S3.

On our second run we will see that the build time is a lot faster as the CodeBuild job restored the cache from S3 to the CodeBuild instance.

Creating the CodeBuild Project

From AWS CodeBuild, when you create a new project, you will see something like this:

image

From the source section you can choose your source provider of choice, I will be using Github as my source code resides there.

From the environment section, you can choose the docker image that you want to run as a container where your job will be executed from, I will be using Amazon Linux:

image

From the Buildspec section you can either have your buildspec.yml in version control or using the UI to inject your buildspec info into the project.

You will notice at the bottom is the section which CodeBuild will use to upload/restore cache to and from:

version: 0.2
phases:
  install:
    commands:
      - java -version
  build:
    commands:
      - mvn clean install
cache:
  paths:
    - '/root/.m2/**/*'

Under Artifacts is the section where we will configure our S3 section for caching:

If you are using the wizard, CodeBuild will create the required IAM permissions for the CodeBuild role in order for CodeBuild to Put and Get from S3, but it's advisable reviewing those permissions.

Run your CodeBuild Jobs

Below is the results of my initial build and my second build, which you will noticed that we saved 7 minutes of run time.

image

So let's say you have 10 CodeBuild jobs, that runs once a day during weekdays and assuming they all take the same time to run, then you will be saving 1400 minutes of runtime per month.

That's good savings

To read more on AWS CodeBuild have a look at their Product Page

Thank You

If you found this useful, come say hi on my Website: ruan.dev or follow me on Twitter: @ruanbekker