From 9a31434fe766be634d8be9d2755268b3a09b7bba Mon Sep 17 00:00:00 2001 From: Viren Nadkarni <viren.nadkarni@gmail.com> Date: Fri, 7 Mar 2025 15:37:04 +0530 Subject: [PATCH 1/4] Add placeholder --- .../coverage/coverage_codebuild/index.md | 1 - content/en/user-guide/aws/codebuild/index.md | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 content/en/user-guide/aws/codebuild/index.md diff --git a/content/en/references/coverage/coverage_codebuild/index.md b/content/en/references/coverage/coverage_codebuild/index.md index 0a7c685776..1d1bc0807a 100644 --- a/content/en/references/coverage/coverage_codebuild/index.md +++ b/content/en/references/coverage/coverage_codebuild/index.md @@ -4,7 +4,6 @@ linkTitle: "codebuild" description: > Implementation details for API codebuild hide_readingtime: true -draft: true --- ## Coverage Overview diff --git a/content/en/user-guide/aws/codebuild/index.md b/content/en/user-guide/aws/codebuild/index.md new file mode 100644 index 0000000000..b467ae3276 --- /dev/null +++ b/content/en/user-guide/aws/codebuild/index.md @@ -0,0 +1,16 @@ +--- +title: CodeBuild +linkTitle: CodeBuild +description: > + Get started with CodeBuild on LocalStack +tags: ["Pro image"] +--- + +## Introduction + + +## Getting Started + + +## Limitations + From e6bcec0cee03810c869914425255768ce24996c0 Mon Sep 17 00:00:00 2001 From: Giovanni Grano <me@giograno.com> Date: Wed, 21 May 2025 09:40:59 +0200 Subject: [PATCH 2/4] wip --- content/en/user-guide/aws/codebuild/index.md | 152 +++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/content/en/user-guide/aws/codebuild/index.md b/content/en/user-guide/aws/codebuild/index.md index b467ae3276..d14e9ce7db 100644 --- a/content/en/user-guide/aws/codebuild/index.md +++ b/content/en/user-guide/aws/codebuild/index.md @@ -8,9 +8,161 @@ tags: ["Pro image"] ## Introduction +AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy. +It's part of the AWS Developer Tools suite and integrates with other AWS services to provide an end-to-end development pipeline. + +LocalStack supports the emulation of most of the CodeBuild operations. +The supported operations are listed on the [API coverage page]({{< ref "coverage_codebuild" >}}). + +AWS CodeBuild emulation is powered by the [AWS CodeBuild agent](https://docs.aws.amazon.com/codebuild/latest/userguide/use-codebuild-agent.html). ## Getting Started +This tutorial will show you how to use AWS CodeBuild to test and build a deployable version of a Java executable. + +It assumes basic knowledge of the [`awslocal`](https://github.com/localstack/awscli-local) wrapper, Apache Maven, and Java. + +### Create the source code + +In the first step, we have to create the project that we want to build with AWS CodeBuild. + +In an empty directory, we need to re-create the following structure: + +```bash +root-directory-name +├── pom.xml +└── src + ├── main + │ └── java + │ └── MessageUtil.java + └── test + └── java + └── TestMessageUtil.java +``` + +Let us walk through these files. +`MessageUtil.java` is the file implementing the logic of this small application. +It does nothing more than print a salutation message. +Copy the following content into the `src/main/java` directory. + +```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; + } +} +``` + +Every build needs some testing! +Therefore, create the `TestMessageUtil.java` file in the `src/test/java` directory. + +```java +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() { + System.out.println("Inside testSalutationMessage()"); + message = "Hi!" + "Robert"; + assertEquals(message,messageUtil.salutationMessage()); + } +} +``` + +This small suite simply verifies that the greeting message is built correctly. + +Finally, we need a `pom.xml` file to instruct Maven about what to build and which artifact needs to be produced. +Create this file at the root of your directory. + +```xml +<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"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.example</groupId> + <artifactId>messageUtil</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + <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> +``` + +With the following configuration, Maven will compile the `java` files into a executable jar and run the specified tests. + +### Create the buildspec file + +Now that we have our project set up, we need to create a `buildspec` file. +A `buildspec` file is a collection of settings and commands, specified in YAML format, that tells AWS CodeBuild how to run a build. + +Create this `buildspec.yml` file in the root directory. + +```yaml +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 +``` ## Limitations +- CodeBuild currently only supports S3 as a code source. +You can use AWS CodePipeline to integrate CodeBuild with a source code repository provider via CodeStarSourceConnection. +- We only use one build +- Talk to the host (pass via the host network) From c9fdfbe41cc9819e5ebd88a9581174951c1239be Mon Sep 17 00:00:00 2001 From: Giovanni Grano <me@giograno.com> Date: Wed, 21 May 2025 11:26:46 +0200 Subject: [PATCH 3/4] create buckets --- content/en/user-guide/aws/codebuild/index.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/content/en/user-guide/aws/codebuild/index.md b/content/en/user-guide/aws/codebuild/index.md index d14e9ce7db..dc6e0985f5 100644 --- a/content/en/user-guide/aws/codebuild/index.md +++ b/content/en/user-guide/aws/codebuild/index.md @@ -160,6 +160,38 @@ artifacts: - target/messageUtil-1.0.jar ``` +### Create input and output buckets + +Now we have to create two S3 buckets: +- one bucket that stores the source we just created, that will be the source of the AWS CodeBuild build; +- one bucket where the output of the build, i.e., the JAR file, will be stored. + +Create the buckets with the following commands: + +{{< command >}} +$ awslocal s3 mb s3://codebuild-demo-input +<disable-copy> +make_bucket: codebuild-demo-input +{{< /command >}} + +{{< command >}} +$ awslocal s3 mb s3://codebuild-demo-output +<disable-copy> +make_bucket: codebuild-demo-output +{{< /command >}} + +Finally, zip the content of the source code directory and upload it to the created source bucket. +With a UNIX system, you can use the `zip` utility: +{{< command >}} +$ zip -r MessageUtil.zip <source-directory> +{{< /command >}} + +Then, upload `MessageUtil.zip` to the `codebuild-demo-input` bucket with the following command: + +{{< command >}} +$ awslocal s3 cp MessageUtil.zip s3://codebuild-demo-input +{{< /command >}} + ## Limitations - CodeBuild currently only supports S3 as a code source. From 49f30f4ec3e82aafc2f23b4ca7454dbf0bab5a54 Mon Sep 17 00:00:00 2001 From: Giovanni Grano <me@giograno.com> Date: Wed, 21 May 2025 13:57:44 +0200 Subject: [PATCH 4/4] iam role --- content/en/user-guide/aws/codebuild/index.md | 89 ++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/content/en/user-guide/aws/codebuild/index.md b/content/en/user-guide/aws/codebuild/index.md index dc6e0985f5..baec9c91a7 100644 --- a/content/en/user-guide/aws/codebuild/index.md +++ b/content/en/user-guide/aws/codebuild/index.md @@ -192,6 +192,95 @@ Then, upload `MessageUtil.zip` to the `codebuild-demo-input` bucket with the fol $ awslocal s3 cp MessageUtil.zip s3://codebuild-demo-input {{< /command >}} +### Configuring IAM + +To properly work, AWS CodeBuild needs access to other AWS services, e.g., to retrieve the source code from a S3 bucket. +Create a `create-role.json` file with following content: + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": "codebuild.amazonaws.com" + }, + "Action": "sts:AssumeRole" + } + ] +} +``` + +Then, run the following command to create the IAM role: +{{< command >}} +$ awslocal iam create-role --role-name CodeBuildServiceRole --assume-role-policy-document file://create-role.json +{{< /command >}} + +From the command's response, keep note of the role ARN: +it will be needed by CodeBuild later on. + +Let us now define the policy for the created role. +Create a `put-role-policy.json` file with the following content: + +```json +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "CloudWatchLogsPolicy", + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "*" + }, + { + "Sid": "CodeCommitPolicy", + "Effect": "Allow", + "Action": [ + "codecommit:GitPull" + ], + "Resource": "*" + }, + { + "Sid": "S3GetObjectPolicy", + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:GetObjectVersion" + ], + "Resource": "*" + }, + { + "Sid": "S3PutObjectPolicy", + "Effect": "Allow", + "Action": [ + "s3:PutObject" + ], + "Resource": "*" + }, + { + "Sid": "S3BucketIdentity", + "Effect": "Allow", + "Action": [ + "s3:GetBucketAcl", + "s3:GetBucketLocation" + ], + "Resource": "*" + } + ] +} +``` + +Finally, assign the policy to the role with the following command: + +{{< command >}} +$ awslocal put-role-policy --role-name CodeBuildServiceRole --policy-name CodeBuildServiceRolePolicy --policy-document file://put-role-policy.json +{{< /command >}} + ## Limitations - CodeBuild currently only supports S3 as a code source.