SaigonTech Vault
Loại: knowledge-doc 2026-09-29 active #wiki-import

AWS Codebuild

Code building service in the cloud
  • It allows you to build your code in the cloud.
  • That means that the source course is going to be compiled, the tests are going to be run, and then the output of which is going to produce packages, and these packages are going to be ready to be deployed(by CodeDeploy for example) onto servers so that your application can run.
  • CodeBuild eliminates the need to provision, manage, and scale your own build servers. It provides prepackaged build environments for popular programming languages and build tools such as Apache Maven, Gradle, and more. You can also customize build environments in CodeBuild to use your own build tools. CodeBuild scales automatically to meet peak build requests.
Benefits
  • Fully managed, serverless: CodeBuild eliminates the need to set up, patch, update, and manage your own build servers
  • Scalable and highly available
  • Secure
  • On demand, pay-as-you-go pricing: only pay for the build time
Build
  1. Buildspec file name and storage location
  2. Default(if not specific when config): buildspec.yml, placed in the root of your source directory
  3. Custom: Store a buildspec file somewhere other than the root of your source directory
  4. Other place in the project: config/buildspec.yml for example
  5. In an S3 bucket:The S3 bucket must be in the same AWS Region as your build project. Specify the buildspec file using its ARN (for example, arn:aws:s3:::<my-codebuild-sample2>/buildspec.yml).

  6. Buildspec file syntax: Buildspec files must be expressed in YAML format. <details> <summary>Click to see</summary>

```yml version: 0.2 run-as: Linux-user-name env: shell: shell-tag variables: key: "value" key: "value" parameter-store: key: "value" key: "value" exported-variables: - variable - variable secrets-manager: key: secret-id:json-key:version-stage:version-id git-credential-helper: no | yes proxy: upload-artifacts: no | yes logs: no | yes

batch: fast-fail: false | true # build-list: # build-matrix: # build-graph:

phases: install: run-as: Linux-user-name on-failure: ABORT | CONTINUE runtime-versions: runtime: version runtime: version commands: - command - command finally: - command - command # steps: pre_build: run-as: Linux-user-name on-failure: ABORT | CONTINUE commands: - command - command finally: - command - command # steps: build: run-as: Linux-user-name on-failure: ABORT | CONTINUE commands: - command - command finally: - command - command # steps: post_build: run-as: Linux-user-name on-failure: ABORT | CONTINUE commands: - command - command finally: - command - command # steps: reports: report-group-name-or-arn: files: - location - location base-directory: location discard-paths: no | yes file-format: report-format artifacts: files: - location - location name: artifact-name discard-paths: no | yes base-directory: location exclude-paths: excluded paths enable-symlinks: no | yes s3-prefix: prefix secondary-artifacts: artifactIdentifier: files: - location - location name: secondary-artifact-name discard-paths: no | yes base-directory: location artifactIdentifier: files: - location - location discard-paths: no | yes base-directory: location cache: paths: - path - path ``` </details>

  1. Example buildspec file: <details> <summary>Click to see</summary>

```yml version: 0.2

env: variables: JAVA_HOME: "/usr/lib/jvm/java-8-openjdk-amd64" parameter-store: LOGIN_PASSWORD: /CodeBuild/dockerLoginPassword

phases: install: commands: - echo Entered the install phase... - apt-get update -y - apt-get install -y maven finally: - echo This always runs even if the update or install command fails pre_build: commands: - echo Entered the pre_build phase... - docker login -u User -p $LOGIN_PASSWORD finally: - echo This always runs even if the login command fails build: commands: - echo Entered the build phase... - echo Build started on date - mvn install finally: - echo This always runs even if the install command fails post_build: commands: - echo Entered the post_build phase... - echo Build completed on date

reports: arn:aws:codebuild:your-region:your-aws-account-id:report-group/report-group-name-1: files: - "/*" base-directory: 'target/tests/reports' discard-paths: no reportGroupCucumberJson: files: - 'cucumber/target/cucumber-tests.xml' discard-paths: yes file-format: CUCUMBERJSON # default is JUNITXML artifacts: files: - target/messageUtil-1.0.jar discard-paths: yes secondary-artifacts: artifact1: files: - target/artifact-1.0.jar discard-paths: yes artifact2: files: - target/artifact-2.0.jar discard-paths: yes cache: paths: - '/root/.m2//*' ``` </details>

  1. Provided docker image

  2. Available runtimes

  3. Quotas

  4. Working with CodeBuild: https://wiki.sgt-dev.space/6580fcfda2928a2b6cc464ad

Pricing

  • On-Demand EC2 pricing
    • Build duration: is calculated in minutes, from the time you submit your build until your build is terminated, rounded up to the nearest minute.
    • Compute types: Charges vary by the compute instance type that you choose for your build.
  • On-Demand Lambda pricing

    • Build duration: is calculated in seconds, from the time you submit your build until your build is terminated, rounded up to the nearest second.
    • Compute types: Charges vary by the compute type that you choose for your build.
  • On-Demand EC2 pricing example:If you execute 100 builds in one month using build.general1.small where each build runs for 5 minutes, then your charges would be calculated as follows:

Monthly build charges

Build minutes = 100 builds * 5 minutes = 500 build minutes

Build minutes – Free tier build minutes = Monthly billable build minutes = 500 – 100 = 400 build minutes

Monthly build charges = 400 build minutes * $0.005 = $2
  • On-Demand Lambda pricing example: If you execute 100 builds in one month using lambda.arm.1GB where each build runs for 300 seconds, then your charges would be calculated as follows:
Monthly build charges

Build seconds = 100 builds * 300 seconds = 30,000 build seconds
Build seconds – Free tier build seconds = Monthly billable build seconds = 30,000 – 6,000 = 24,000 build seconds
Monthly build charges = 24,000 build seconds * $0.00001 = $0.24