paul@home:~$

Interesting and Technical problems solved and explained

Running AWS Code build locally

I’ve been working with code build for some time now, have even had to configure our own images for some Windows builds. Recently I helped my friend Zain setup a pipeline, while learning the ins and out of codebuild ( using scp in codebuild to be specfic ) we performed 100’s of test builds. While the sercice itself is quite cheap free is a good option to cheap. When asked how can I cut the costs I suggested running codebuild locally which I had not done myself until now.

There is a great article on AWS Code Build Locally

but I thought I’d repeat the process and provide a very simple app to demonstrate the process as well as using actual aws credentials to perform typical tasks such as syncing to s3 or accessing parameter store.

There are some pre-requisist:

  • Docker
  • aws command line installed
  • aws access key and secret ( if you need to access a native service not just compile and app )
  • 10 gig hdd space ( yes the codebuild image is huge )
  • about 30 mins to buidl the container

The Steps at a high level are:

  1. Clone the codebuild docker image repo
  2. Build the docker image
  3. Pull the codebuild agent container
  4. Download the aws helper script and your good to go.

For those beginning what is the codebuild doing, well it’s a build envrioment. If you look in the docker file you’ll see it’s literally installing all the dependencies you might need ( ok not all of them but lots of common ones ).

You pass your repo to the build environment along with the instructions in the buildspec.yaml and the code build agent will interpret the commands running your build steps.

 paulk@iMac#/tmp/pra-test$ codebuild_build.sh -i aws/codebuild/standard:4.0 -a /tmp -c ~/.aws

My current workign directory ( pwd )

 /tmp/pra-test

I place codebuild.sh in my path so I can call it anywhere

 codebuild_build.sh

Use -i to specify the build envioment image

 -i {imagename}

Use -a to specify the output directory. for me /tmp works fine I don’t need to keep the artifact

 -a /tmp

Use -c to specify the aws credentials file

 -c ~/.aws

You can see how the shell scripts helps as it puts together this:

Build Command:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e "IMAGE_NAME=aws/codebuild/standard:preact" -e "ARTIFACTS=/tmp" -e "SOURCE=/tmp/cra-test" -e "AWS_CONFIGURATION=/Users/paulk/.aws" -e "INITIATOR=paulk" amazon/aws-codebuild-local:latest

See a run below

alt text

Result:

alt text