• Wrapping up (p. 14)
API Version 2016-10-06 5
Step 1: Create the source code
Step 1: Create the source code
(Part of: Getting started with AWS CodeBuild using the console (p. 5))
In this step, you create the source code that you want CodeBuild to build to the output bucket. This source code consists of two Java class files and an Apache Maven Project Object Model (POM) file.
1. In an empty directory on your local computer or instance, create this directory structure.
(root directory name) `-- src
|-- main | `-- java `-- test `-- java
2. Using a text editor of your choice, create this file, name it MessageUtil.java, and then save it in the src/main/java directory.
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;
} }
This class file creates as output the string of characters passed into it. The MessageUtil constructor sets the string of characters. The printMessage method creates the output. The salutationMessage method outputs Hi! followed by the string of characters.
3. Create this file, name it TestMessageUtil.java, and then save it in the /src/test/java directory.
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
public class TestMessageUtil { String message = "Robert";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
assertEquals(message,messageUtil.printMessage());
} @Test
public void testSalutationMessage() {
Step 1: Create the source code
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Robert";
assertEquals(message,messageUtil.salutationMessage());
} }
This class file sets the message variable in the MessageUtil class to Robert. It then tests to see if the message variable was successfully set by checking whether the strings Robert and Hi!Robert appear in the output.
4. Create this file, name it pom.xml, and then save it in the root (top level) directory.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/
maven-v4_0_0.xsd">
<name>Message Utility Java Sample App</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
</plugins>
</build>
</project>
Apache Maven uses the instructions in this file to convert the MessageUtil.java and TestMessageUtil.java files into a file named messageUtil-1.0.jar and then run the specified tests.
At this point, your directory structure should look like this.
(root directory name)
Step 2: Create the buildspec file (p. 8)
API Version 2016-10-06 7
Step 2: Create the buildspec file
Step 2: Create the buildspec file
(Previous step: Step 1: Create the source code (p. 6))
In this step, you create a build specification (build spec) file. A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. Without a build spec, CodeBuild cannot successfully convert your build input into build output or locate the build output artifact in the build environment to upload to your output bucket.
Create this file, name it buildspec.yml, and then save it in the root (top level) directory.
version: 0.2 phases:
install:
runtime-versions:
java: corretto11 pre_build:
commands:
- echo Nothing to do in the pre_build phase...
build:
commands:
- echo Build started on `date`
- mvn install post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- target/messageUtil-1.0.jar
Important
Because a build spec declaration must be valid YAML, the spacing in a build spec declaration is important. If the number of spaces in your build spec declaration does not match this one, the build might fail immediately. You can use a YAML validator to test whether your build spec declaration is valid YAML.
NoteInstead of including a build spec file in your source code, you can declare build commands separately when you create a build project. This is helpful if you want to build your source code with different build commands without updating your source code's repository each time. For more information, see Buildspec syntax (p. 128).
In this build spec declaration:
• version represents the version of the build spec standard being used. This build spec declaration uses the latest version, 0.2.
• phases represents the build phases during which you can instruct CodeBuild to run commands. These build phases are listed here as install, pre_build, build, and post_build. You cannot change the spelling of these build phase names, and you cannot create more build phase names.
In this example, during the build phase, CodeBuild runs the mvn install command. This command instructs Apache Maven to compile, test, and package the compiled Java class files into a build output artifact. For completeness, a few echo commands are placed in each build phase in this example.
When you view detailed build information later in this tutorial, the output of these echo commands can help you better understand how CodeBuild runs commands and in which order. (Although all build phases are included in this example, you are not required to include a build phase if you do not plan to run any commands during that phase.) For each build phase, CodeBuild runs each specified command, one at a time, in the order listed, from beginning to end.
Step 3: Create two S3 buckets
• artifacts represents the set of build output artifacts that CodeBuild uploads to the output bucket. files represents the files to include in the build output. CodeBuild uploads the single messageUtil-1.0.jar file found in the target relative directory in the build environment. The file name messageUtil-1.0.jar and the directory name target are based on the way Apache Maven creates and stores build output artifacts for this example only. In your own builds, these file names and directories are different.
For more information, see the Buildspec reference (p. 127).
At this point, your directory structure should look like this.
(root directory name) |-- pom.xml |-- buildspec.yml `-- src
|-- main | `-- java
| `-- MessageUtil.java `-- test
`-- java
`-- TestMessageUtil.java
Next step
Step 3: Create two S3 buckets (p. 9)
Step 3: Create two S3 buckets
(Previous step: Step 2: Create the buildspec file (p. 8))
Although you can use a single bucket for this tutorial, two buckets makes it easier to see where the build input is coming from and where the build output is going.
• One of these buckets (the input bucket) stores the build input. In this tutorial, the name of this input bucket is codebuild-region-ID-account-ID-input-bucket, where region-ID is the AWS Region of the bucket and account-ID is your AWS account ID.
• The other bucket (the output bucket) stores the build output. In this tutorial, the name of this output bucket is codebuild-region-ID-account-ID-output-bucket.
If you chose different names for these buckets, be sure to use them throughout this tutorial.
These two buckets must be in the same AWS Region as your builds. For example, if you instruct CodeBuild to run a build in the US East (Ohio) Region, these buckets must also be in the US East (Ohio) Region.
For more information, see Creating a Bucket in the Amazon Simple Storage Service User Guide.
NoteAlthough CodeBuild also supports build input stored in CodeCommit, GitHub, and Bitbucket repositories, this tutorial does not show you how to use them. For more information, see Plan a build (p. 126).
Next step
Step 4: Upload the source code and the buildspec file (p. 10)
API Version 2016-10-06 9
Step 4: Upload the source code and the buildspec file
Step 4: Upload the source code and the buildspec file
(Previous step: Step 3: Create two S3 buckets (p. 9))
In this step, you add the source code and build spec file to the input bucket.
Using your operating system's zip utility, create a file named MessageUtil.zip that includes MessageUtil.java, TestMessageUtil.java, pom.xml, and buildspec.yml.
The MessageUtil.zip file's directory structure must look like this.
MessageUtil.zip |-- pom.xml |-- buildspec.yml `-- src
|-- main | `-- java
| `-- MessageUtil.java `-- test
`-- java
`-- TestMessageUtil.java
Important
Do not include the (root directory name) directory, only the directories and files in the (root directory name) directory.
Upload the MessageUtil.zip file to the input bucket named codebuild-region-ID-account-ID-input-bucket.
Important
For CodeCommit, GitHub, and Bitbucket repositories, by convention, you must store a build spec file named buildspec.yml in the root (top level) of each repository or include the build spec declaration as part of the build project definition. Do not create a ZIP file that contains the repository's source code and build spec file.
For build input stored in S3 buckets only, you must create a ZIP file that contains the source code and, by convention, a build spec file named buildspec.yml at the root (top level) or include the build spec declaration as part of the build project definition.
If you want to use a different name for your build spec file, or you want to reference a build spec in a location other than the root, you can specify a build spec override as part of the build project definition. For more information, see Buildspec file name and storage location (p. 127).
Next step
Step 5: Create the build project (p. 10)
Step 5: Create the build project
(Previous step: Step 4: Upload the source code and the buildspec file (p. 10))
In this step, you create a build project that AWS CodeBuild uses to run the build. A build project includes information about how to run a build, including where to get the source code, which build environment to use, which build commands to run, and where to store the build output. A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build. The build environment is expressed as a Docker image. For more information, see Docker overview on the Docker Docs website.
For this build environment, you instruct CodeBuild to use a Docker image that contains a version of the Java Development Kit (JDK) and Apache Maven.
Step 5: Create the build project
To create the build project
1. Sign in to the AWS Management Console and open the AWS CodeBuild console at https://
console.aws.amazon.com/codesuite/codebuild/home.
2. Use the AWS region selector to choose an AWS Region where CodeBuild is supported. For more information, see AWS CodeBuild endpoints and quotas in the Amazon Web Services General Reference.
3. If a CodeBuild information page is displayed, choose Create build project. Otherwise, on the navigation pane, expand Build, choose Build projects, and then choose Create build project.
4. On the Create build project page, in Project configuration, for Project name, enter a name for this build project (in this example, codebuild-demo-project). Build project names must be unique across each AWS account. If you use a different name, be sure to use it throughout this tutorial.
NoteOn the Create build project page, you might see an error message similar to the following:
You are not authorized to perform this operation.. This is most likely because you signed in to the AWS Management Console as an IAM user who does not have permissions to create a build project.. To fix this, sign out of the AWS Management Console, and then sign back in with credentials belonging to one of the following IAM entities:
• An administrator IAM user in your AWS account. For more information, see Creating your first IAM admin user and group in the IAM User Guide.
• An IAM user in your AWS account with the AWSCodeBuildAdminAccess,
AmazonS3ReadOnlyAccess, and IAMFullAccess managed policies attached to that IAM user or to an IAM group that the IAM user belongs to. If you do not have an IAM user or group in your AWS account with these permissions, and you cannot add these permissions to your IAM user or group, contact your AWS account administrator for assistance. For more information, see AWS managed (predefined) policies for AWS CodeBuild (p. 332).
Both options include administrator permissions that allow you to create a build project so you can complete this tutorial. We recommend that you always use the minimum permissions required to accomplish your task. For more information, see AWS CodeBuild permissions reference (p. 348).
5. In Source, for Source provider, choose Amazon S3.
6. For Bucket, choose codebuild-region-ID-account-ID-input-bucket.
7. For S3 object key, enter MessageUtil.zip.
8. In Environment, for Environment image, leave Managed image selected.
9. For Operating system, choose Amazon Linux 2.
10. For Runtime(s), choose Standard.
11. For Image, choose aws/codebuild/amazonlinux2-x86_64-standard:3.0.
12. In Service role, leave New service role selected, and leave Role name unchanged.
13. For Buildspec, leave Use a buildspec file selected.
14. In Artifacts, for Type, choose Amazon S3.
15. For Bucket name, choose codebuild-region-ID-account-ID-output-bucket.
16. Leave Name and Path blank.
17. Choose Create build project.
Next step
Step 6: Run the build (p. 12)
API Version 2016-10-06 11
Step 6: Run the build
Step 6: Run the build
(Previous step: Step 5: Create the build project (p. 10))
In this step, you instruct AWS CodeBuild to run the build with the settings in the build project.
To run the build
1. Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home.
2. In the navigation pane, choose Build projects.
3. In the list of build projects, choose codebuild-demo-project, and then choose Start build. The build starts immediately.
Next step
Step 7: View summarized build information (p. 12)
Step 7: View summarized build information
(Previous step: Step 6: Run the build (p. 12))
In this step, you view summarized information about the status of your build.
To view summarized build information
1. If the codebuild-demo-project:<build-ID> page is not displayed, in the navigation bar, choose Build history. Next, in the list of build projects, for Project, choose the Build run link for codebuild-demo-project. There should be only one matching link. (If you have completed this tutorial before, choose the link with the most recent value in the Completed column.)
2. On the Build status page, in Phase details, the following build phases should be displayed, with Succeeded in the Status column:
• SUBMITTED
• QUEUED
• PROVISIONING
• DOWNLOAD_SOURCE
• INSTALL
• PRE_BUILD
• BUILD
• POST_BUILD
• UPLOAD_ARTIFACTS
• FINALIZING
• COMPLETED
In Build Status, Succeeded should be displayed.
If you see In Progress instead, choose the refresh button.
3. Next to each build phase, the Duration value indicates how long the build phase lasted. The End
Step 8: View detailed build information
Next step
Step 8: View detailed build information (p. 13)
Step 8: View detailed build information
(Previous step: Step 7: View summarized build information (p. 12))
In this step, you view detailed information about your build in CloudWatch Logs.
NoteTo protect sensitive information, the following are hidden in CodeBuild logs:
• AWS access key IDs. For more information, see Managing Access Keys for IAM Users in the AWS Identity and Access Management User Guide.
• Strings specified using the Parameter Store. For more information, see Systems Manager Parameter Store and Systems Manager Parameter Store Console Walkthrough in the Amazon EC2 Systems Manager User Guide.
• Strings specified using AWS Secrets Manager. For more information, see Key management (p. 326).
To view detailed build information
1. With the build details page still displayed from the previous step, the last 10,000 lines of the build log are displayed in Build logs. To see the entire build log in CloudWatch Logs, choose the View entire log link.
2. In the CloudWatch Logs log stream, you can browse the log events. By default, only the last set of log events is displayed. To see earlier log events, scroll to the beginning of the list.
3. In this tutorial, most of the log events contain verbose information about CodeBuild downloading and installing build dependency files into its build environment, which you probably don't care about. You can use the Filter events box to reduce the information displayed. For example, if you enter "[INFO]" in Filter events, only those events that contain [INFO] are displayed. For more information, see Filter and pattern syntax in the Amazon CloudWatch User Guide.
Next step
Step 9: Get the build output artifact (p. 13)
Step 9: Get the build output artifact
(Previous step: Step 8: View detailed build information (p. 13))
In this step, you get the messageUtil-1.0.jar file that CodeBuild built and uploaded to the output bucket.
You can use the CodeBuild console or the Amazon S3 console to complete this step.
To get the build output artifact (AWS CodeBuild console)
1. With the CodeBuild console still open and the build details page still displayed from the previous step, choose the Build details tab and scroll down to the Artifacts section.
NoteIf the build details page is not displayed, in the navigation bar, choose Build history, and then choose the Build run link.
API Version 2016-10-06 13
Step 10: Delete the S3 buckets
2. The link to the Amazon S3 folder is under the Artifacts upload location. This link opens the folder in Amazon S3 where you find the messageUtil-1.0.jar build output artifact file.
To get the build output artifact (Amazon S3 console)
1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
2. Open codebuild-region-ID-account-ID-output-bucket.
3. Open the codebuild-demo-project folder.
4. Open the target folder, where you find the messageUtil-1.0.jar build output artifact file.
Next step
Step 10: Delete the S3 buckets (p. 14)
Step 10: Delete the S3 buckets
(Previous step: Step 9: Get the build output artifact (p. 13))
To prevent ongoing charges to your AWS account, you can delete the input and output buckets used in this tutorial. For instructions, see Deleting or Emptying a Bucket in the Amazon Simple Storage Service User Guide.
If you are using the IAM user or an administrator IAM user to delete these buckets, the user must have more access permissions. Add the following statement between the markers (### BEGIN ADDING STATEMENT HERE ### and ### END ADDING STATEMENTS HERE ###) to an existing access policy for the user.
The ellipses (...) in this statement are used for brevity. Do not remove any statements in the existing access policy. Do not enter these ellipses into the policy.
{ "Version": "2012-10-17", "Id": "...",
"Statement": [
### BEGIN ADDING STATEMENT HERE ###
{
"Effect": "Allow", "Action": [
"s3:DeleteBucket", "s3:DeleteObject"
],
"Resource": "*"
}
### END ADDING STATEMENT HERE ###
] }
Next step
Wrapping up (p. 14)
Wrapping up
In this tutorial, you used AWS CodeBuild to build a set of Java class files into a JAR file. You then viewed the build's results.
Getting started using the AWS CLI
You can now try using CodeBuild in your own scenarios. Follow the instructions in Plan a build (p. 126).
If you don't feel ready yet, you might want to try building some of the samples. For more information, see Samples (p. 29).
Getting started with AWS CodeBuild using the AWS CLI
In this tutorial, you use AWS CodeBuild to build a collection of sample source code input files (called build input artifacts or build input) into a deployable version of the source code (called build output artifact or build output). Specifically, you instruct CodeBuild to use Apache Maven, a common build tool, to build a set of Java class files into a Java Archive (JAR) file. You do not need to be familiar with Apache Maven or Java to complete this tutorial.
You can work with CodeBuild through the CodeBuild console, AWS CodePipeline, the AWS CLI, or the AWS SDKs. This tutorial demonstrates how to use CodeBuild with the AWS CLI. For information about using CodePipeline, see Use CodePipeline with CodeBuild (p. 377). For information about using the AWS SDKs, see Run CodeBuild directly (p. 377).
Important
The steps in this tutorial require you to create resources (for example, an S3 bucket) that might result in charges to your AWS account. These include possible charges for CodeBuild and for AWS resources and actions related to Amazon S3, AWS KMS, and CloudWatch Logs. For more information, see CodeBuild pricing, Amazon S3 pricing, AWS Key Management Service pricing, and Amazon CloudWatch pricing.
The steps in this tutorial require you to create resources (for example, an S3 bucket) that might result in charges to your AWS account. These include possible charges for CodeBuild and for AWS resources and actions related to Amazon S3, AWS KMS, and CloudWatch Logs. For more information, see CodeBuild pricing, Amazon S3 pricing, AWS Key Management Service pricing, and Amazon CloudWatch pricing.