What is AWS CodeBuild?
AWS CodeBuild is a service that fully manages your project's build cycle. CodeBuild can help you enable continuous integration by automatically building/testing your project any time changes are pushed to a specific branch. Once the build has completed, other services can automate its deployment to your production servers, in turn enabling continuous delivery for the project.
AWS CodeBuild Project Setup
Navigate to the AWS CodeBuild console. Under Build in the sidebar, click on Getting started. Take a few minutes to read the page and watch the video to better understand what CodeBuild is and how it works.
You will find that CodeBuild is based on projects, and you will create your first build project now. Click Create project.
Info: As before, if a setting isn't mentioned below, you can go ahead and leave the default value as-is.
Project Configuration
- Project name - for now you can use something like "DemoBuild"
Source
- Source provider - GitHub
- Repository -
- Choose Connect using OAuth, click Connect to GitHub, a window will pop up:
- If necessary, log in to GitHub
- Click Authorize aws-codesuite
- Click Confirm
- Choose Repository in my GitHub account
- Use the drop-down to select your demo deployment repository
- Choose Connect using OAuth, click Connect to GitHub, a window will pop up:
Primary Source Webhook Events
- Webhook - leave unchecked
Environment
- Environment image - Managed image
- Operating system - Ubuntu
- Runtime(s) - Standard
- Image - standard:5.0 (Includes Java 11)
- Image version - Always use the latest version for this runtime image
- Environment type - Linux
- Service role - New service role
- Role name - Automatically generated
Buildspec
- Choose Use a buildspec file
You'll need to create a file named buildspec.yml in the root folder of your project repository. This file provides AWS CodeBuild instructions on how to handle the build process. You can use this example:
version: 0.2
phases:
install:
runtime-versions:
java: corretto17
pre_build:
commands:
- echo In the pre_build phase... nothing to do here
build:
commands:
- echo Build started on `date`
- ./mvnw clean package
post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- target/ROOT.war
- appspec.yml
Pretty self-explanatory, but capable of much more. You can find a detailed reference guide here. This buildspec is configured to match the war file deployment that you set up in the previous lesson.
Create this file in the root of your project (alongside your pom.xml) and push it to your GitHub repo now, then come back here to continue.
Artifacts
- Type - Amazon S3
- Bucket name - The bucket you created previously
- Name - For now you can use DemoBuild.zip
- Path - Purely for organizational purposes
- Namespace type - Used to organize artifact builds - helpful, but not necessary for this demo
- Artifacts packaging - Zip. You want the war file placed in a zip along with your
appspec.ymlfile (you'll create this in the next lesson)
Logs
Here you can choose to upload logs from this build project to either Amazon CloudWatch, or Amazon S3. CloudWatch is a logging and monitoring service that blankets most of AWS. It offers some free tier functionality beyond the introductory 12-month period, and will come in very helpful here.
If you choose to only upload logs to an S3 bucket, any build failure would require manually downloading the logs via the S3 console (or CLI). It's not a very user-friendly process. CloudWatch will significantly improve the CodeBuild logging process, which you will witness first-hand shortly.
- CloudWatch logs - Checked
- Group name - Log groups are collections of log streams. You can use " build-logs".
- Stream name - A log stream serves a specific purpose, something like "build-demo-log")
Click Create build project.
Building Your Project
You'll now be taken to the build project detail page, where you can click Start build and the process should start immediately.
Here's where the magic of CloudWatch comes into play - scroll down just a tad, and you can tail the build log output live! This feature is not available with S3 logs. Give it a few minutes to attempt to complete the build.
Oh Snap!
The build has failed. Go ahead and inspect the logs thoroughly to figure out why. Seriously, don't continue reading until you diagnose the problem.
Got it? Nice! Can you see the value that these easily accessible CloudWatch logs provide now? Your build tests are failing because the application can't access the database - you've seen this before.
Unable to open JDBC Connection for DDL execution
Take a look at your buildspec.yml file, which command is being used to build the project? And how did you bypass testing in the previous lessons? Right, make the change to your buildspec file:
build:
commands:
- echo Build started on `date`
- ./mvnw clean package -DskipTests
Commit this change to your GitHub repository, and start the build again. This time, the build should succeed. If you experience further issues, check the logs again and take steps to remedy the situation.
Once the build has succeeded, check the contents of your S3 bucket, and you should see your zip file ready to go. Feel free to download the zip and expand it, you'll find your ROOT.war file inside!
Tip: When working with a real project, disabling testing is not the appropriate thing to do. This step is only for demonstration purposes, as tests should normally pass before deployment.
Summary: AWS Codebuild
You've now enabled AWS CodeBuild to pull your latest project source files from GitHub, build a war file using Maven, and then upload the file to an S3 bucket - all with the click of a button!
This is a major step towards setting up a sophisticated deployment workflow that implements continuous delivery. Up next, you'll automate the deployment of your builds using CodeDeploy.