AWS CodePipeline (CI/CD Pipeline)
CICD Pipeline with CodeCommit CodeBuild and CodeDeploy
CodeBuild Demo
Welcome to this step-by-step tutorial on integrating AWS CodeBuild with Amazon S3 for continuous integration and delivery. By the end of this guide, you'll learn how to compile Java code, run unit tests, and store build artifacts in S3—all within a fully managed AWS service.
Overview
AWS CodeBuild is a fully managed build service that compiles source code, executes tests, and produces deployable artifacts. In this demo, we will:
- Implement a simple Java utility and unit tests.
- Create a
buildspec.yml
to define build commands. - Provision two S3 buckets—one for source input, one for build output.
- Configure and run a CodeBuild project that pulls from S3, builds with Maven, and pushes artifacts back to S3.
Note
You can follow along in your own AWS account. After completing the tutorial, remember to clean up any resources to avoid unexpected charges.
Prerequisites
- An AWS account with permissions for CodeBuild, S3, and IAM.
- AWS CLI or access to the AWS Management Console.
- Java 11 (Corretto) and Maven installed locally (for packaging).
Step 1: Create the Java Utility and Unit Tests
- In your project directory, create the
MessageUtil
class:
// src/main/java/MessageUtil.java
public class MessageUtil {
private String message;
public MessageUtil(String message) {
this.message = message;
}
public String printMessage() {
System.out.println(message);
return message;
}
public String salutationMessage() {
message = "Hi! " + message;
System.out.println(message);
return message;
}
}
- Next, add a JUnit test for
MessageUtil
:
// src/test/java/TestMessageUtil.java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestMessageUtil {
String message = "Robert";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
assertEquals(message, messageUtil.printMessage());
}
@Test
public void testSalutationMessage() {
String expected = "Hi! Robert";
assertEquals(expected, messageUtil.salutationMessage());
}
}
Step 2: Define the Build Specification (buildspec.yml
)
Create a file named buildspec.yml
at the project root:
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
pre_build:
commands:
- echo "Starting pre-build phase..."
build:
commands:
- echo "Build started on `date`"
- mvn install
artifacts:
files:
- target/*.jar
Build Phases at a Glance
Phase | Description | Sample Command |
---|---|---|
install | Set up runtime environments | java: corretto11 |
pre_build | Prepare dependencies or variables | echo "Preparing..." |
build | Compile, test, and package the app | mvn install |
Step 3: Verify Directory Structure
Ensure your project looks like this:
project-root/
├── pom.xml
├── buildspec.yml
└── src
├── main
│ └── java
│ └── MessageUtil.java
└── test
└── java
└── TestMessageUtil.java
Step 4: Create S3 Buckets for Source and Artifacts
- In the AWS Console, navigate to S3 and click Create bucket.
Warning
S3 bucket names must be globally unique. Choose a naming convention (e.g., <yourname>-codebuild-source
).
- Enter a name like
codebuilddemo-kodekloud-mbbucket1
and accept the defaults.
- Repeat to create a second bucket (e.g.,
codebuilddemo-kodekloud-mbbucket2
).
Step 5: Package and Upload Your Source Code
- Zip your project files:
pom.xml
,buildspec.yml
, and thesrc
directory.
- Name the archive
MessageUtil.zip
and upload it to your source bucket:
- Click the bucket, choose Upload, add
MessageUtil.zip
, and complete the upload wizard.
Step 6: Create and Configure the CodeBuild Project
- In the AWS Console, open CodeBuild and click Create build project.
- Under Source, choose Amazon S3.
- Select your input bucket and set Object key to
MessageUtil.zip
.
- Scroll to Environment:
- Managed image
- Operating system: Linux
- Runtime: Standard
- Image: x86_64 standard: 3.0
- Under Buildspec, choose Use a buildspec file.
- For Artifacts, select Amazon S3 and pick your output bucket.
- Click Create build project to finalize.
Step 7: Start the Build and Monitor Logs
- Select your newly created project and click Start build.
- Watch the real-time logs for each build phase:
[Container] 2023/04/29 17:58:45 Waiting for agent ping
[Container] 2023/04/29 17:58:48 Phase is DOWNLOAD_SOURCE
[Container] 2023/04/29 17:58:49 Selecting 'java' runtime version 'corretto'...
[Container] 2023/04/29 17:58:49 Running command mvn install
...
- Upon completion, you’ll see Succeeded status and your JAR files in the output bucket.
What’s Next?
You’ve successfully automated a Java build using AWS CodeBuild and S3. In upcoming lessons, we’ll explore deployment strategies using AWS CodeDeploy and AWS CloudFormation.
References
Watch Video
Watch video content
Practice Lab
Practice lab