December 30, 2020By Jason Cronquist← Back to Blog

Running CodeBuild for Raspberry PI Docker Image


I've recently started installing my CozyHem Home Automation suite in my parent's home. The biggest challenge with doing so is running the code off a Raspberry Pi. My own home is running off an x86 server. Naturally, the x86 docker image doesn't run on the Arm processor of the Raspberry Pi.

I decided to use AWS CodeBuild instead of Github Actions primarily because my work uses CodeBuild and I wanted to get some experience with it. Two birds with one stone, setup my CI/CD and get some relevant experience under my belt. In reality the two methods are very similar though.

After some searching I found this example of using Docker buildx with codebuild. However, to keep installs simple, and to keep from having to host x86 and arm64 versions of my dependent images (python:3.8 and node:12) in ECR personally, I updated it to use Docker Hub instead of ECR. I also updated image tag to be based off of the last published git-tag via $(git describe --tags --abbrev=0).

To log into Docker Hub, I stored an api_key in AWS SecretsManager which is retrieved by adding the below to the root level of the buildspec.

env:
  secrets-manager:
    docker_access_token: "secret-name:secret-key"

Logging in was then done in the pre-build phase with the command below.

  prebuild:
    commands:
      - docker login -u username -p $docker_access_token

I used AWS CodePipeline to trigger a build whenever a merge/commit to master happens. This solved the multiple architecture problem, however builds take around 45 minutes to run thanks to the Arm emulation required by Docker. This means total cost for my build is about $1.20 per build.

To further reduce costs and build time, I tried running the Raspberry Pi Build on Graviton 2 instance as per this official AWS tutorial. This means I won't be able to support the Raspberry Pi 1 and 2 (which use the 32bit arm/v7 architecture), but that's a sacrafice I'm more than willing to make. Using the Graviton2 isntance, my builds now tak 10 mins and cost $0.15 per build.