Sitemap

Package an App with CodeBuild

4 min readNov 6, 2024

--

In this fourth part of our DevOps project, we’ll focus on using CodeBuild to package our web app. We’ll cover some key new concepts, such as:

— CodeBuild and CloudWatch for managing builds and logs

— Terms like Build Process, BuildArtifact, Environment Image, Runtime, and Service Role

Press enter or click to view image in full size
Photo by Kevin Jarrett on Unsplash

Steps We’ll Cover:

  1. Set Up Your Web App (Projects 1–3 in this series)
  2. Set up an S3 Bucket
  3. Create a CodeBuild Build Project
  4. Create Your Web App’s buildspec.yml File
  5. Modify Your CodeBuild IAM Role
  6. Test the Build Project

Let’s dive in!

1. Set Up Your Web App (Projects 1–3 of this Series)

If you haven’t completed the first three steps in this series, here are the links you need:

Complete these steps before moving on.

2. Set up an S3 Bucket

Here, we’ll set up an S3 bucket to store build process zip files.

— In your AWS console, search for S3.

— Click Create Bucket and give it a unique name, e.g., build-artifacts-yourname.

— Leave other options as default and Create the bucket.

3. Create a CodeBuild Project

Now, let’s create a build project for your app.

— Open AWS Console and search for CodeBuild.

— Select Create project and name it your-web-build.

— Under Additional configuration, add a tag with key team and value devops.

For Source:

— Choose GitHub as the source provider.

— Set credentials to default source credentials.

— Select public repository and paste your repository URL.

For Environment:

— Choose Provisioning model: On-demand.

— Set Environment image to Managed image.

— Compute: EC2.

— Operating System: Amazon Linux.

— Runtime: Standard. Image: aws/codebuild/amazonlinux2-x86_64-standard.

— Image version: Always use the latest.

Under Service Role, select New service role.

In Buildspec:

— Choose Use a buildspec file.

In Artifacts:

— Set Type to Amazon S3.

Get Evan Selunani’s stories in your inbox

Join Medium for free to get updates from this writer.

— Select the bucket you created.

— Name the artifact your-web-build.zip.

— Change Artifacts packaging to Zip.

Under Logs:

— Enable CloudWatch logs if needed.

— Set Group name to your-build-logs and Stream name prefix to webapp.

— Finally, click Create build project!

4. Create Your Web App’s buildspec.yml File

The buildspec.yml file is your CodeBuild setup guide. It details all the commands CodeBuild should run, step-by-step, to ensure each build is consistent. Here’s how to create and configure it:

Step 1: Create buildspec.yml in VS Code

  1. Open Remote-SSH in VS Code.
  2. In the left-hand panel, right-click on your project folder (your-web-project) and select New File.
  3. Name the file buildspec.yml (exactly as shown).
  4. Copy in the following content:
version: 0.2

phases:
install:
runtime-versions:
java: corretto8
pre_build:
commands:
- echo Initializing environment
- export CODEARTIFACT_AUTH_TOKEN=`aws codeartifact get-authorization-token --domain yourdomain --domain-owner 123456789012 --query authorizationToken --output text`
build:
commands:
- echo Build started on `date`
- mvn -s settings.xml compile
post_build:
commands:
- echo Build completed on `date`
- mvn -s settings.xml package
artifacts:
files:
- target/nextwork-web-project.war
discard-paths: no

Replace:

— yourdomain with your actual CodeArtifact domain.

— 123456789012 with your AWS Account ID. (To find your Account ID, open any AWS console tab, click your account name at the top right, and copy the ID.)

Breaking Down the buildspec.yml File

— version: Tells CodeBuild to use version 0.2 format for the buildspec file.

— phases:

install: Sets the Java runtime.

pre_build: Gets an authorization token to securely access your CodeArtifact repository. The echo commands display messages to track build stages.

build: Compiles the project and packages it. Automates the Maven compile and package steps you did manually before.

post_build: Finishes up, including the creation of a .war file in target/ for deployment.

— artifacts:

files: Specifies the .war file we want to save after the build.

discard-paths: no: Keeps the .war file organized in the target/ folder instead of the root directory.

Step 2: Save, Commit, and Push Changes

  1. Save the file with Command + S (Mac) or Ctrl + S (Windows).
  2. In your terminal, navigate to your-web-project and run these commands:
git add .
git commit -m "Adding buildspec.yml file"
git push -u origin main

Step 3: Modify Your CodeBuild IAM Role

To enable CodeBuild to access the packages, we need to adjust permissions:

  1. Open the IAM console in AWS.
  2. Go to Roles and search for the CodeBuild role (e.g., codebuild-your-web-build-service-role).
  3. Click Add permissions > Attach policies.
  4. Search for codeartifact-your-consumer-policy, select it, and Add permissions.

Step 4: Test the Build Project

  1. In the CodeBuild console, select your-web-build and click Start build.
  2. Watch the logs for build progress (it should take around 5 minutes).
  3. When finished, open your S3 console, navigate to your-build-artifacts-yourname bucket, and check for your .war file inside a zip named your-web-project.zip.

Now your app is packaged and ready to go!

This brings us to the end of our guide. Try this one out today and enjoy your virtual space!

Have a lovely week… See you on the 5th Devops challange!

--

--

Evan Selunani
Evan Selunani

Written by Evan Selunani

I find documenting my learnings through writing fun . I hope you find value and insight in reading what my brain spews. Cheers!