Introduction

AWS Graviton processors offer a cost-effective alternative to traditional x86 processors, making them an attractive option for businesses aiming to reduce expenses. In addition to their affordability, Graviton processors consume less power, leading to significant cost savings for businesses operating large data centers. This blog post will guide you through the steps to leverage AWS ECS Graviton processors for multi-architecture builds, enabling efficient and cost-effective deployment via AWS Codebuild.

Pre-requisites
  1. 1. A docker base image that is arm64 platform compatible.
    2. An AWS Codepipeline with AWS Codebuild.

Steps to Migrate

Configuring CodeBuild Environment

To facilitate multi-architecture builds, we'll begin by changing the CodeBuild environment image to standard:5. This image enables the use of Docker BuildKit, which leverages the power of AWS Graviton processors for building both amd64 and arm64 architectures. Additionally, we'll enable DOCKERBUILD_KIT, a crucial setting for utilizing Docker BuildKit efficiently.

Modifying CodeBuild Buildspec

The buildspec file needs to be updated to incorporate the changes required for multi-architecture builds. We'll outline the necessary modifications to enable seamless integration with AWS Graviton processors.

   # Log in to ECR
   - aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | \
   docker login --username AWS --password-stdin ${REGISTRY_HOST} 
   
   # By using standard image, buildx is already installed there but we need to have 
   # a link from bin to lib/docker so that we can use the docker buildx command
   - ln -s /usr/local/bin/docker-buildx /usr/local/lib/docker/cli-plugins/docker-buildx
   
   # Create makes a new builder instance pointing to a docker context or endpoint.
   # Builder instances are isolated environments where builds can be invoked.
   - docker buildx create --use --name buildx_instance
   
   # Building multi archecture and pushing it to ecr at the same time 
   - docker buildx build --platform linux/arm64 linux/amd64 --provenance=false \
    -t ${REGISTRY_URI}:${DOCKER_IMAGE_TAG} --push . 

Dockerfile Cross-Compilation for Faster Multi-Architecture Building

Optimizing the Dockerfile for cross-compilation helps expedite the multi-architecture build process. We recommend utilizing the same build platform during compilation for consistency. Here's some examples of Dockerfile configured for cross-compilation:

PHP

   # It is better to use the same buildplatform during the compilation
   FROM --platform=$BUILDPLATFORM composer as compile
   COPY ./composer.* ./ 
   RUN composer install
   
   # We dont need to specify the platform here 
   # since it will use the platform mentioned in the build command
   FROM php
   COPY --from=compile ./vendor ./vendor

Java

   FROM --platform=linux/amd64 adoptopenjdk/openjdk8
   ENV APP_HOME=/usr/app/
   WORKDIR $APP_HOME
   COPY build.gradle settings.gradle querydsl.gradle gradlew /usr/app/
   COPY gradle $APP_HOME/gradle
   RUN chmod +x ./gradlew && ./gradlew build -x test || return 0
   COPY . .
   RUN ./gradlew build -x test

Kotlin

   FROM --platform=$BUILDPLATFORM eclipse-temurin:17-jdk
   ENV APP_HOME=/usr/app/
   WORKDIR $APP_HOME
   COPY build.gradle.kts settings.gradle.kts gradlew /usr/app/
   COPY gradle $APP_HOME/gradle
   RUN chmod +x ./gradlew && ./gradlew build -x test || return 0
   COPY . .
   RUN ./gradlew build -x test

Updating Task Definition

To fully utilize AWS Graviton processors, we need to update the default "cpuArchitecture" in the task definition to ARM64. This ensures that the task runs on the ARM64 architecture, maximizing cost savings. Modify the "runtimePlatform" section in the task definition as follows:

    "runtimePlatform": {
        "cpuArchitecture": "ARM64",
        "operatingSystemFamily": "LINUX"
    }
Conclusion

By leveraging AWS Graviton processors for multi-architecture builds, businesses can achieve substantial cost savings without compromising performance. The steps outlined in this blog post enable you to configure CodeBuild environments, modify buildspecs, and update task definitions, allowing you to harness the power of Graviton processors for efficient and cost-effective deployment. Embracing the benefits of AWS Graviton processors positions your business to optimize resource utilization and maximize cost savings in the long run.

Remember, AWS Graviton processors provide an excellent option for businesses seeking to save money and elevate their container workloads with enhanced performance.

AWS Services Used
Amazon EC2
Amazon ECS Launch Type Fargate x86
Amazon ECS Launch Type Fargate Graviton
AWS S3
Amazon ECR
Amazon CodeCommit
AWS CodePipeline
Amazon CodeBuild
AWS VPN