<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeBuild Tutorial &#8211; Best DevOps</title>
	<atom:link href="https://www.bestdevops.com/tag/codebuild-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.bestdevops.com</link>
	<description>Lets Learn, Do it &#38; Share! Thats a Best DevOps!!!</description>
	<lastBuildDate>Mon, 12 Oct 2026 06:25:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>
	<item>
		<title>Extending AWS CodeBuild with Custom Build Environments</title>
		<link>https://www.bestdevops.com/extending-aws-codebuild-with-custom-build-environments/</link>
					<comments>https://www.bestdevops.com/extending-aws-codebuild-with-custom-build-environments/#respond</comments>
		
		<dc:creator><![CDATA[anil]]></dc:creator>
		<pubDate>Mon, 20 Feb 2017 06:58:34 +0000</pubDate>
				<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[DevOps]]></category>
		<category><![CDATA[DevOps Best Practices]]></category>
		<category><![CDATA[DevOps Process]]></category>
		<category><![CDATA[DevOps Tools]]></category>
		<category><![CDATA[Amazon Web Services]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[AWS CodeBuild]]></category>
		<category><![CDATA[Build Environments]]></category>
		<category><![CDATA[CodeBuild]]></category>
		<category><![CDATA[CodeBuild Tutorial]]></category>
		<guid isPermaLink="false">http://www.bestdevops.com/?p=721</guid>

					<description><![CDATA[AWS CodeBuild's built-in environments cover popular runtimes, but you can extend it to any language or toolchain by building a custom Docker image and hosting it in Amazon ECR. This walkthrough uses PHP as a concrete example.]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">AWS CodeBuild is a fully managed build service that compiles source code, runs tests, and produces software packages ready for deployment. Out of the box, CodeBuild ships with curated build environments for common runtimes including Java, Ruby, Python, Go, Node.js, Android, and Docker. When your project requires a runtime not covered by these defaults — such as PHP, Rust, or a bespoke internal toolchain — you can supply your own Docker image as a custom build environment.</p>



<p class="wp-block-paragraph">Build environments are Docker images that contain a complete file system with everything required to build and test a project. To use a custom environment, you:</p>



<ul class="wp-block-list">
<li>Build a Docker image that contains your required tools.</li>
<li>Push the image to a container registry — typically Amazon Elastic Container Registry (ECR).</li>
<li>Reference that image in your CodeBuild project configuration.</li>
</ul>



<p class="wp-block-paragraph">When a build runs, CodeBuild pulls the specified Docker image from the registry and uses it to compile source code, run tests, and package the application.</p>



<p class="wp-block-paragraph">This article walks through creating a custom PHP build environment and wiring it into a CodeBuild project, following the pattern published by the AWS CodeBuild team. The companion repository used throughout is <strong>awslabs/codebuild-images</strong> on GitHub.</p>



<h2 class="wp-block-heading">Prerequisites</h2>



<p class="wp-block-paragraph">Before starting, make sure the following tools are installed and configured on your workstation:</p>



<ul class="wp-block-list">
<li><strong>Docker</strong> — to build and push container images.</li>
<li><strong>AWS CLI</strong> — to interact with CloudFormation, ECR, and CodeBuild.</li>
<li><strong>Git</strong> — to clone the example repository and push sample code to CodeCommit.</li>
<li>An <strong>IAM user or role</strong> with permissions to create CloudFormation stacks, push to ECR, write to CodeCommit, and create CodeBuild projects.</li>
</ul>



<h2 class="wp-block-heading">Step 1 — Create the Demo Resources with CloudFormation</h2>



<p class="wp-block-paragraph">The <code>awslabs/codebuild-images</code> repository contains a CloudFormation template (<code>template.yml</code>) that provisions two resources:</p>



<ul class="wp-block-list">
<li>A <strong>CodeCommit repository</strong> to host the PHP sample application source code.</li>
<li>An <strong>ECR repository</strong> to host the custom Docker build environment image.</li>
</ul>



<p class="wp-block-paragraph">Clone the repository and deploy the stack:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">git <span class="hljs-keyword">clone</span> https:<span class="hljs-comment">//github.com/awslabs/codebuild-images.git</span>
cd codebuild-images</code></span></pre>

<pre class="wp-block-code"><span><code class="hljs language-javascript">aws cloudformation create-stack \
  --stack-name codebuild-php \
  --parameters ParameterKey=EnvName,ParameterValue=php \
  --template-body file:<span class="hljs-comment">//template.yml &gt; /dev/null &amp;&amp; \</span>
awscloudformation wait stack-create-complete \
  --stack-name codebuild-php &amp;&amp; \
aws cloudformation describe-stacks \
  --stack-name codebuild-php \
  --output table \
  --query Stacks&#91;<span class="hljs-number">0</span>].Outputs</code></span></pre>


<p class="wp-block-paragraph">Once the stack is created, CloudFormation outputs two values that you will use in subsequent steps:</p>



<ul class="wp-block-list">
<li><strong>BuildImageRepositoryUri</strong> — the URI of the ECR repository that will host the build environment image.</li>
<li><strong>SourceCodeRepositoryCloneUrl</strong> — the HTTPS clone URL of the CodeCommit repository for the sample PHP application.</li>
</ul>



<h2 class="wp-block-heading">Step 2 — Build and Push the Custom Docker Image</h2>



<p class="wp-block-paragraph">The PHP build environment image is defined by the <code>Dockerfile</code> inside the <code>php/</code> directory of the cloned repository. It extends the official PHP 7 Docker image and adds the tooling needed for a typical CI pipeline:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">FROM php:<span class="hljs-number">7</span>

ARG composer_checksum=<span class="hljs-number">55</span>d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30
ARG composer_url=https:<span class="hljs-comment">//raw.githubusercontent.com/composer/getcomposer.org/ba0141a67b9bd1733409b71c28973f7901db201d/web/installer</span>

ENV COMPOSER_ALLOW_SUPERUSER=<span class="hljs-number">1</span>
ENV PATH=$PATH:vendor/bin

RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \
      curl \
      git \
      python-dev \
      python-pip \
      zlib1g-dev \
 &amp;&amp; pip install awscli \
 &amp;&amp; docker-php-ext-install zip \
 &amp;&amp; curl -o installer <span class="hljs-string">"$composer_url"</span> \
 &amp;&amp; <span class="hljs-keyword">echo</span> <span class="hljs-string">"$composer_checksum *installer"</span> | shasum -c -a <span class="hljs-number">384</span> \
 &amp;&amp; php installer --install-dir=/usr/local/bin --filename=composer \
 &amp;&amp; rm -rf /<span class="hljs-keyword">var</span>/lib/apt/lists<span class="hljs-comment">/*</span></code></span></pre>


<p class="wp-block-paragraph">This image layers the following on top of the official PHP runtime:</p>



<ul class="wp-block-list">
<li><strong>Composer</strong> — the PHP dependency manager, installed with checksum verification.</li>
<li><strong>AWS CLI</strong> — enables build steps to interact with AWS services (for example, publishing an SNS notification on build completion or writing artifacts to S3).</li>
<li><strong>Git</strong> — enables build steps such as tagging a release commit after a successful build.</li>
<li><strong>zip extension</strong> — required by many PHP projects and Composer plugins.</li>
</ul>



<p class="wp-block-paragraph">The final <code>rm -rf /var/lib/apt/lists/*</code> instruction removes the APT package index to reduce image size, following <a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/" target="_blank" rel="noopener">Dockerfile best practices</a>.</p>



<p class="wp-block-paragraph">To build and push the image, first authenticate the local Docker daemon with ECR:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">aws ecr get-login-password | docker login \
  --username AWS \
  --password-stdin <span class="hljs-tag">&lt;<span class="hljs-name">account-id</span>&gt;</span>.dkr.ecr.<span class="hljs-tag">&lt;<span class="hljs-name">region</span>&gt;</span>.amazonaws.com</code></span></pre>


<p class="wp-block-paragraph">Then build and push the image, substituting the <code>BuildImageRepositoryUri</code> value from the CloudFormation outputs:</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">cd</span> <span class="hljs-selector-tag">php</span>
<span class="hljs-selector-tag">docker</span> <span class="hljs-selector-tag">build</span> <span class="hljs-selector-tag">-t</span> <span class="hljs-selector-attr">&#91;BuildImageRepositoryUri]</span> .
<span class="hljs-selector-tag">docker</span> <span class="hljs-selector-tag">push</span> <span class="hljs-selector-attr">&#91;BuildImageRepositoryUri]</span></code></span></pre>


<p class="wp-block-paragraph">Once the push completes, the image is available in ECR and ready to be referenced by CodeBuild.</p>



<h2 class="wp-block-heading">Step 3 — Configure the Source Repository</h2>



<p class="wp-block-paragraph">The cloned repository includes a small PHP sample application that converts Roman numerals to Arabic numerals, together with a PHPUnit test. It also contains a <code>buildspec.yml</code> file — the YAML manifest CodeBuild reads to determine what commands to run:</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">version</span>: 0<span class="hljs-selector-class">.1</span>
<span class="hljs-selector-tag">phases</span>:
  <span class="hljs-selector-tag">pre_build</span>:
    <span class="hljs-selector-tag">commands</span>:
      <span class="hljs-selector-tag">-</span> <span class="hljs-selector-tag">composer</span> <span class="hljs-selector-tag">install</span>
  <span class="hljs-selector-tag">build</span>:
    <span class="hljs-selector-tag">commands</span>:
      <span class="hljs-selector-tag">-</span> <span class="hljs-selector-tag">phpunit</span> <span class="hljs-selector-tag">tests</span></code></span></pre>


<p class="wp-block-paragraph">This build spec instructs CodeBuild to:</p>



<ul class="wp-block-list">
<li>Run <code>composer install</code> during the <code>pre_build</code> phase to fetch declared dependencies, including PHPUnit.</li>
<li>Run <code>phpunit tests</code> during the <code>build</code> phase to execute the test suite.</li>
</ul>



<p class="wp-block-paragraph">Push the sample application to the CodeCommit repository created by the CloudFormation stack. Before doing so, ensure your IAM user has the necessary CodeCommit permissions and that your Git client is configured with the correct credentials. See the <a href="http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-gc.html" target="_blank" rel="noopener">CodeCommit HTTPS setup guide</a> for detailed instructions.</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">cd sample
git init
git add .
git commit -m <span class="hljs-string">"Initial commit"</span>
git remote add origin &#91;SourceCodeRepositoryCloneUrl]
git push origin master</code></span></pre>


<h2 class="wp-block-heading">Step 4 — Configure the CodeBuild Project</h2>



<p class="wp-block-paragraph">With the Docker image in ECR and the source code in CodeCommit, create and configure the CodeBuild project from the AWS Management Console:</p>



<ol class="wp-block-list">
<li>Open the <strong>AWS CodeBuild console</strong> and choose <strong>Create project</strong>.</li>
<li>Set <strong>Project name</strong> to <code>php-demo</code>.</li>
<li>Under <strong>Source provider</strong>, choose <strong>AWS CodeCommit</strong>, then select the <strong>codebuild-sample-php</strong> repository.</li>
<li>Under <strong>Environment image</strong>, select <strong>Specify a Docker image</strong>. Set <strong>Custom image type</strong> to <strong>Amazon ECR</strong>, choose the <strong>codebuild/php</strong> ECR repository, and select the <strong>latest</strong> image tag.</li>
<li>Under <strong>Build specification</strong>, select <strong>Use the buildspec.yml in the source code root directory</strong>.</li>
<li>Set <strong>Artifacts type</strong> to <strong>No artifacts</strong> (this demo only runs tests).</li>
<li>Choose <strong>Continue</strong>, then <strong>Save and Build</strong>.</li>
<li>On the start build page, select the <strong>master</strong> branch and choose <strong>Start build</strong>.</li>
</ol>



<p class="wp-block-paragraph">CodeBuild will pull the custom PHP image from ECR and execute the build spec commands. The console shows the status of each build phase, the last 20 lines of build output, and a link to the full log stream in Amazon CloudWatch Logs.</p>



<figure class="wp-block-image"><img decoding="async" src="https://d2908q01vomqb2.cloudfront.net/7719a1c782a1ba91c031a682a0a2f8658209adbf/2017/02/16/build-succeeded-1024x426.png" alt="Screenshot of CodeBuild showing a successful build"/></figure>



<h2 class="wp-block-heading">How Custom Environments Fit into a Broader CI/CD Pipeline</h2>



<p class="wp-block-paragraph">Custom build environments unlock several practical use cases beyond language support:</p>



<ul class="wp-block-list">
<li><strong>Pinned tool versions</strong> — Embed a specific version of a compiler, linter, or test framework so every build uses identical tooling regardless of when it runs.</li>
<li><strong>Internal toolchains</strong> — Package proprietary or licensed software that cannot be installed from public package managers.</li>
<li><strong>Pre-warmed dependencies</strong> — Bake large dependency sets into the image to reduce per-build install time.</li>
<li><strong>Security hardening</strong> — Strip unnecessary packages from the base image and apply your organisation&#8217;s security baselines before builds run.</li>
</ul>



<p class="wp-block-paragraph">Images should be rebuilt and re-pushed to ECR whenever underlying dependencies (OS packages, runtimes, Composer, AWS CLI) receive security updates. Tag images with explicit version strings rather than relying solely on <code>latest</code> for production pipelines, so you can roll back to a known-good environment if a new image introduces a regression.</p>



<h2 class="wp-block-heading">Summary</h2>



<p class="wp-block-paragraph">AWS CodeBuild&#8217;s support for custom Docker build environments means the service is not limited to its bundled runtimes. Any language or toolchain that can be containerised can be used as a CodeBuild environment. The process follows three consistent steps: build a Docker image with your required tools, push it to Amazon ECR, and reference it in your CodeBuild project configuration. The PHP walkthrough above demonstrates this pattern end-to-end, from provisioning infrastructure with CloudFormation to triggering a passing build in the console.</p>



<p class="wp-block-paragraph">The same approach applies to any runtime — Go versions not yet curated by AWS, older JDKs, niche testing frameworks, or heavily customised base images — making CodeBuild a flexible foundation for continuous integration across heterogeneous technology stacks.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.bestdevops.com/extending-aws-codebuild-with-custom-build-environments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
