Configuring Build Environments, Performance and Networking
The build environment decides how fast a build runs, which tools are available, which networks it can reach and how much it costs. This lesson explains the compute options, managed and custom images, caching, batch builds and VPC connectivity, and shows how to choose between them for a given set of requirements.
Learning outcomes
After completing this lesson, you will be able to:
- Compare the compute modes, compute types and image options available for CodeBuild builds.
- Explain the S3 and local caching modes and choose the right one for a dependency or Docker layer cache.
- Describe batch build types and when each is appropriate.
- Explain how a build accesses private resources in a VPC and the network requirements this creates.
Compute Modes and Compute Types
CodeBuild offers two compute modes. EC2 compute runs the build in a container on an Amazon EC2 instance managed by CodeBuild. It supports every feature, including Docker builds in privileged mode, long timeouts, custom images, VPC connectivity and caching. Lambda compute runs the build on AWS Lambda. It starts almost immediately and is charged per second, which makes it attractive for short, frequent builds, but it has restrictions: builds are limited to 15 minutes, privileged mode and Docker builds are not supported, local caching, batch builds and Session Manager debugging are not available, and only specific Lambda images can be used.
Within EC2 compute, the compute type selects memory and vCPU, for example BUILD_GENERAL1_SMALL, MEDIUM, LARGE, XLARGE and 2XLARGE, and the environment type selects Linux, Linux GPU, ARM or Windows. Larger compute types cost more per minute but often reduce total cost for CPU-bound builds because the build finishes sooner. ARM (Graviton) environments are typically cheaper per minute and are required when building native ARM images without emulation.
Reserved capacity fleets
By default, builds run on on-demand capacity that CodeBuild provisions per build. A reserved capacity fleet keeps a configured number of instances running for a project or set of projects. Builds start without provisioning delay, the instances can have a cache that persists between builds, and the fleet can use custom AMIs or be placed in a VPC. Fleets suit organisations with steady, high build volume or builds that must start instantly; they are charged for the time the fleet runs, whether or not builds are running. A fleet can be configured with an overflow behaviour that sends builds to on-demand capacity when all fleet instances are busy.
Managed and Custom Images
AWS provides managed images, such as the Amazon Linux and Ubuntu standard images, which include common runtimes, the AWS CLI and Docker. Managed images are patched by AWS, and the runtime-versions buildspec key chooses language versions within them. They are the default choice.
A custom image is a container image that the team builds with exactly the tools it needs, stored in Amazon ECR or another registry. Custom images make builds faster when tool installation takes a long time, and they pin tool versions for reproducibility. The trade-off is that the team must patch and rebuild the image. For an ECR image in the same account, the project's image pull credentials can be set to the CodeBuild service principal, which uses a repository policy that allows CodeBuild to pull. For a cross-account repository or a private registry, the project uses its service role credentials, or credentials stored in Secrets Manager for a non-ECR registry.
Caching
Builds start in a clean environment, so without caching every build downloads its dependencies again. CodeBuild supports two cache types, and the choice depends on how often builds run and on which host they land.
Amazon S3 cache
The paths listed in the buildspec cache section are uploaded to an S3 bucket at the end of the build and downloaded at the start of the next. Because the cache is stored centrally, it is available to every build regardless of which host runs it. Uploading and downloading take time, so an S3 cache helps most when the cached content is expensive to recreate, such as large dependency trees from slow repositories.
Local cache
A local cache is stored on the build host and is available only to later builds that run on the same host. It is fast but best effort: it helps only when builds run frequently enough to reuse hosts. There are three local cache modes. Source cache keeps Git metadata so that later builds fetch only changes. Docker layer cache keeps existing Docker image layers, which speeds up builds of large images; it requires privileged mode. Custom cache keeps the directories listed in the buildspec cache section.
The comparison below summarises when to choose each cache type.
Batch Builds
A batch build runs several builds as one coordinated unit, defined in a batch section of the buildspec and started with StartBuildBatch or by a pipeline action configured for batch builds. There are four batch types.
- build-list: a list of builds that run in parallel, each with its own buildspec, environment or variables, such as building the same code for Linux and Windows.
- build-matrix: a matrix of parameters, where CodeBuild runs a build for every combination, such as three runtime versions multiplied by two operating systems.
- build-graph: builds with dependencies between them, run in dependency order, such as building a shared library before the services that use it.
- build-fanout: one build split into parallel shards, typically to distribute a large test suite across several environments. Test files are divided among the shards with the
codebuild-tests-runcommand, which reduces total test time.
Batch builds also support combining the artifacts of the individual builds into one location and limiting the number of builds and the compute types a batch may use, which protects the account from an accidental matrix that starts hundreds of builds.
Choose the batch type
Accessing Resources in a VPC
By default, a build runs in a network managed by CodeBuild with internet access but no access to resources in your VPCs. When a build must reach a private resource, such as an Amazon RDS database for integration tests, a private package repository or an internal API, the project is configured with a VPC, subnets and security groups. CodeBuild then creates elastic network interfaces in those subnets.
These network interfaces do not receive public IP addresses. A build placed in a VPC therefore has no internet access unless the subnets are private subnets with a route to a NAT gateway; placing the build in a public subnet with an internet gateway does not work. If the build needs only AWS services, interface VPC endpoints (for example for Amazon ECR, CloudWatch Logs and Secrets Manager) and an S3 gateway endpoint can replace the NAT gateway. The service role needs EC2 permissions to create and manage the network interfaces, such as ec2:CreateNetworkInterface, ec2:DescribeSubnets and ec2:CreateNetworkInterfacePermission.
The following diagram shows the network paths of a build placed in a VPC.

Builds that hang after adding a VPC
A build that worked before VPC configuration was added and now times out while downloading dependencies or the source has lost internet access. Use private subnets with a NAT gateway or add VPC endpoints for the services it calls.
Tune the build environment
Speeding Up a Slow Pipeline
A team's build takes 25 minutes: 8 minutes downloading Maven dependencies, 5 minutes building a Docker image from scratch and 12 minutes running 4,000 integration tests on a small compute type. Builds run about 40 times a day.
-
Dependencies
Add the local Maven repository path to the buildspec cache section. Because builds are frequent, combine a local custom cache with an S3 cache if hosts are not always reused.
-
Docker layers
Enable the local Docker layer cache mode, which requires privileged mode, so unchanged layers are reused.
-
Tests
Use a build-fanout batch with several shards and codebuild-tests-run to divide the test files, and choose a larger compute type for CPU-bound tests.
-
Start time
If queuing or provisioning remains significant at this volume, a reserved capacity fleet removes provisioning delay.
A team wants to move a 40-minute Docker image build to Lambda compute to reduce start time. Is this possible?
Show answer
No. Lambda compute limits builds to 15 minutes and does not support privileged mode or Docker builds. The build should stay on EC2 compute, possibly on a reserved capacity fleet to remove provisioning delay.
Exam Tips
Remember for the exam
- Lambda compute is fast to start but has no Docker or privileged mode, no local caching and a 15-minute limit.
- A build in a VPC has no internet access unless it runs in private subnets that route to a NAT gateway, or uses VPC endpoints for the AWS services it needs.
- Local cache (source, Docker layer, custom) is fast but host-specific; the S3 cache is available to every build.
- Use build-matrix for combinations, build-graph for dependencies and build-fanout to shard tests.
- Reserved capacity fleets remove provisioning delay for high-volume builds and can use custom AMIs and VPCs.