In this article I will show how you can run your AWS CodeBuild projects locally. AWS CodeBuild is a “fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy”. By running your CodeBuild projects locally you can test code changes before committing, allowing you to rapidly develop and debug your projects.
Workstation
I recommend using Windows Subsytem for Linux 2 with Ubuntu 20.04 for your local workstation configuration. Additionally, the Chef code I have created at aw5academy/chef/workstation will bootstrap an environment for you with everything you need to follow along in this article. Note: remember to run the /home/ec2-user/codebuild_setup.sh which, builds the Amazon Linux CodeBuild Docker image (this process can take over 60 minutes to complete).

If not using this workstation, the resources you will need are:
- awscb.sh # Copy this file into your PATH (without the .sh extension)
- codebuild_setup.sh # Download and run this script (Note: this can take over 60minutes to complete)
- codebuild_build.sh # Copy this file into your PATH
- git-remote-codecommit # Install this Python module
- Docker Desktop
CodeBuild Project
Let’s first create a CodeBuild project in AWS. In this example our project will be a Docker based Apache application with the built Docker image pushed to Amazon Elastic Container Registry. We will use the Terraform code at aw5academy/terraform/docker-codebuild to provision the resources we need.
git clone https://gitlab.com/aw5academy/terraform/docker-codebuild.git
cd docker-codebuild
terraform init
terraform apply
Part of this Terraform stack is an AWS CodeCommit repository that we will use to store our Docker code. We can copy the code I have created at aw5academy/docker/httpd into this CodeCommit repository.
git clone codecommit::us-east-1://aw5academy-httpd
cd aw5academy-httpd
git clone https://gitlab.com/aw5academy/docker/httpd.git
mv httpd/{Dockerfile,buildspec.yaml} .
rm -rf httpd
git add .
git commit -m "Initial commit"
git push origin master
Now let’s test that the CodeBuild project works from AWS. Navigate to the CodeBuild service and find the docker-aw5academy-httpd project. Click on “Start Build” and select the “master” branch.


Now if you start the build and view the build logs you will see the Docker build happening and the Docker image being pushed to ECR.

CodeBuild Local
We can now try running CodeBuild locally. From your checkout of the “aw5academy-httpd” repository, simply run “awscb”.


Success! You now have a Docker image locally, built in the same way as is done by the AWS CodeBuild service.
You can also add script arguments to awscb to pass in environment variables that will be available within your builds. For example:
awscb -e "env1=foo,env2=bar"
We can also use the “-p” arg to push the Docker images we build locally into ECR. You can combine this with the “-t” arg to tag your images differently. E.g.
awscb -t develop -p
If we run the above command and view our repository in ECR we can see the “latest” image created by AWS CodeBuild and the “develop” image we created locally and pushed.

Wrap-Up
In this article I have demonstrated CodeBuild local for Docker. But you can use this for other build types, e.g. Maven. For more detailed information, refer to the AWS blog post at https://aws.amazon.com/blogs/devops/announcing-local-build-support-for-aws-codebuild/