{CodeBuild}AWS CodeBuild を使用した AWS CLI の開始方法

 

https://docs.aws.amazon.com/ja_jp/codebuild/latest/userguide/getting-started-cli.html

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

 

-- 1.2 jqインストール
sudo yum -y install jq


-- 2. ソースコードを作成する

mkdir -p src/main/java
mkdir -p src/test/java

vim 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;
  }
}

vim src/test/java/TestMessageUtil.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());
  }
}

vim pom.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>


-- 3. buildspec ファイルを作成する

vim buildspec.yml

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

 

-- 4. 2 つの S3 バケットを作成する

aws s3 ls

aws s3 mb s3://bucket123input
aws s3 mb s3://bucket123output

 

-- 5. ソースコードと buildspec ファイルをアップロードする

zip    MessageUtil.zip pom.xml
zip -g MessageUtil.zip buildspec.yml
zip -g MessageUtil.zip src/main/java/MessageUtil.java
zip -g MessageUtil.zip src/test/java/TestMessageUtil.java

zipinfo MessageUtil.zip

aws s3 cp MessageUtil.zip s3://bucket123input

aws s3 ls s3://bucket123input --recursive

 

-- 6. ビルドプロジェクトを作成する

-- 6.1 IAMポリシー作成
vim policy01.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": "*"
    }
  ]
}


aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json

-- 6.2 IAMロール作成
vim role01.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json

-- 6.3 ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--role-name role01


-- 6.4 ビルドプロジェクト作成


aws codebuild create-project \
--generate-cli-skeleton

vim a.json

{
  "name": "project01",
  "source": {
    "type": "S3",
    "location": "bucket123input/MessageUtil.zip"
  },
  "artifacts": {
    "type": "S3",
    "location": "bucket123output"
  },
  "environment": {
    "type": "LINUX_CONTAINER",
    "image": "aws/codebuild/standard:4.0",
    "computeType": "BUILD_GENERAL1_SMALL"
  },
  "serviceRole": "arn:aws:iam::999999999999:role/role01"
}

aws codebuild create-project \
--cli-input-json file://a.json

aws codebuild list-projects

 

-- 7. ビルドを実行する

aws codebuild start-build \
--project-name project01

 


-- 8. ビルド情報の要約を表示する

aws codebuild list-builds


aws codebuild batch-get-builds \
--ids project01:e55bd557-ed20-4ee7-98a4-d55764b512e0


-- 9. 詳細なビルド情報を表示する

 


-- 10. ビルド出力アーティファクトを取得する


aws s3 ls s3://bucket123output --recursive

 

-- 11. クリーンアップ


-- ビルドプロジェクト削除

aws codebuild list-projects

aws codebuild delete-project \
--name project01

 

-- IAMロールの削除
aws iam list-roles | grep role01

aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01

aws iam delete-role --role-name role01


-- IAMポリシーの削除
aws iam list-policies | grep policy01

aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01

 

-- バケットの削除

aws s3 ls

aws s3 rb s3://bucket123input --force
aws s3 rb s3://bucket123output --force