AWS CDK CodeBuild Construct Library (v1)
The `aws-cdk.aws-codebuild` package is part of the AWS Cloud Development Kit (CDK) v1, providing constructs for defining AWS CodeBuild resources programmatically in Python. It allows users to create, configure, and manage CodeBuild projects, build specifications, and associated resources. While AWS CDK v2 is the latest major version, v1 continues to receive maintenance updates for existing projects. Releases for CDK v1 often align with new AWS service features or bug fixes, typically on a weekly or bi-weekly cadence.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_codebuild'
cause The `aws-cdk.aws-codebuild` package (for CDK v1) is not installed, or you might be trying to use v1 imports while having only `aws-cdk-lib` (CDK v2) installed.fixIf using CDK v1, run `pip install aws-cdk.aws-codebuild`. If intending to use CDK v2, install `aws-cdk-lib` and update your imports to `from aws_cdk_lib import aws_codebuild`. -
AttributeError: module 'aws_cdk' has no attribute 'aws_codebuild'
cause This error occurs when `aws-cdk-lib` (CDK v2) is installed, but the code attempts to use a v1 import path like `from aws_cdk import aws_codebuild`. In v2, all constructs are consolidated under `aws_cdk_lib`.fixIf you are using CDK v2, change your import statement to `from aws_cdk_lib import aws_codebuild as codebuild`. If you intend to use v1, ensure only v1 packages are installed (`aws-cdk.core`, `aws-cdk.aws-codebuild`) and no v2 packages like `aws-cdk-lib`. -
CodeBuild project failed with exit code 1
cause A generic build failure. Common culprits are misconfigured `BuildSpec`, insufficient IAM permissions for the CodeBuild service role, or issues with the source code or build environment.fixInspect the detailed build logs in the AWS CodeBuild console for specific error messages. These logs will usually pinpoint the exact line in your `BuildSpec` that failed, or indicate a permissions issue (`AccessDenied`). Address the root cause based on log output.
Warnings
- breaking AWS CDK v1 (`aws-cdk.*` packages) and v2 (`aws-cdk-lib`) have fundamentally different import paths and API structures. Using v1 packages with v2 imports, or vice-versa, will lead to `ModuleNotFoundError` or `AttributeError`.
- gotcha CodeBuild projects require an IAM service role with explicit permissions to access any AWS resources they interact with (e.g., S3 buckets for artifacts, ECR repositories for images, KMS keys, etc.). Missing permissions are a frequent cause of build failures.
- gotcha The `BuildSpec` configuration is sensitive to syntax and content. Incorrect YAML, invalid phase names, or commands referencing non-existent paths/executables will cause the build to fail.
Install
-
pip install aws-cdk.aws-codebuild==1.204.0 -
pip install "aws-cdk.aws-codebuild<2.0.0"
Imports
- Project
from aws_cdk_lib import aws_codebuild as codebuild
from aws_cdk import aws_codebuild as codebuild
- Source
from aws_cdk_lib import aws_codebuild
from aws_cdk import aws_codebuild as codebuild
- BuildSpec
import aws_cdk.aws_codebuild
from aws_cdk import aws_codebuild as codebuild
Quickstart
import os
from aws_cdk import ( # type: ignore
core as cdk,
aws_codebuild as codebuild
)
class MyCodeBuildStack(cdk.Stack):
def __init__(self, scope: cdk.App, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Define a simple CodeBuild project that prints a message
codebuild.Project(
self,
"MySimpleBuildProject",
project_name="MySimpleBuildProject",
build_spec=codebuild.BuildSpec.from_object({
"version": "0.2",
"phases": {
"install": {
"commands": [
"echo 'Installing dependencies...'"
]
},
"build": {
"commands": [
"echo 'Hello from CodeBuild!'",
"env"
]
}
},
"artifacts": {
"files": []
}
}),
source=codebuild.Source.no_source(), # No external source needed for this basic example
environment=codebuild.BuildEnvironment(
build_image=codebuild.LinuxBuildImage.STANDARD_5_0
)
)
app = cdk.App()
MyCodeBuildStack(app, "CodeBuildQuickstartStack")
app.synth()