Skip to content

fix(codebuild): enhance validation for Windows image types #34125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/aws-cdk-lib/aws-codebuild/lib/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2223,20 +2223,34 @@ export class WindowsBuildImage implements IBuildImage {
const errors: string[] = [];

if (buildEnvironment.privileged) {
// Reference: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html
// "Also, Windows does not support privileged mode."
// Last accessed: 2025-04-12
errors.push('Windows images do not support privileged mode');
}

if (buildEnvironment.computeType && isLambdaComputeType(buildEnvironment.computeType)) {
// Reference: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html
// The table below the sentence "AWS CodeBuild provides build environments with the following available memory and disk space for AWS Lambda compute mode:" does not include Windows environment types.
// Last accessed: 2025-04-12
errors.push('Windows images do not support Lambda compute types');
}

const unsupportedComputeTypes = [ComputeType.SMALL, ComputeType.X_LARGE, ComputeType.X2_LARGE];
const unsupportedComputeTypes = [ComputeType.SMALL];
if (buildEnvironment.computeType !== undefined && unsupportedComputeTypes.includes(buildEnvironment.computeType)) {
// Reference: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html#environment.types
// The table includes Windows Medium, Large, XLarge, and 2XLarge.
// Last accessed: 2025-04-12
errors.push(`Windows images do not support the '${buildEnvironment.computeType}' compute type`);
}

if (!buildEnvironment.fleet && this.type === WindowsImageType.SERVER_2022) {
errors.push('Windows Server 2022 images must be used with a fleet');
const supportedOnDemandEnvironmentRegion = ['us-east-2', 'us-east-1', 'us-west-2', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1', 'sa-east-1'];
if (!buildEnvironment.fleet && this.type === WindowsImageType.SERVER_2022 && !supportedOnDemandEnvironmentRegion.includes(Aws.REGION)) {
// Reference: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html#environment.types
// The environment type WINDOWS_SERVER_2022_CONTAINER is only available in these Regions: [...]
// The environment type WINDOWS_EC2 (BUILD_GENERAL1_MEDIUM, BUILD_GENERAL1_LARGE) is only available in these Regions: [...]
// Last accessed: 2025-04-12
errors.push(`Windows Server 2022 images must be used with a fleet in ${Aws.REGION}`);
}

return errors;
Expand Down
50 changes: 46 additions & 4 deletions packages/aws-cdk-lib/aws-codebuild/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,9 +1197,9 @@ describe('Environment', () => {
}).toThrow('The environment type of the fleet (LINUX_CONTAINER) must match the environment type of the build image (WINDOWS_SERVER_2019_CONTAINER)');
});

test('throws when Windows 2022 build image is used without a fleet', () => {
test('throws when Windows 2022 build image is used without a fleet in an unsupported region', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-west-1' } });
const bucket = s3.Bucket.fromBucketName(stack, 'Bucket', 'my-bucket'); // (stack, 'Bucket');

// THEN
Expand All @@ -1216,9 +1216,51 @@ describe('Environment', () => {
}).toThrow('Windows Server 2022 images must be used with a fleet');
});

test('throws when 2022 WindowsImageType is used without a fleet', () => {
test('can use Windows 2022 build image without a fleet in a supported region', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-west-2' } });
const bucket = s3.Bucket.fromBucketName(stack, 'Bucket', 'my-bucket'); // (stack, 'Bucket');

// THEN
expect(() => {
new codebuild.Project(stack, 'Project', {
source: codebuild.Source.s3({
bucket,
path: 'path',
}),
environment: {
buildImage: codebuild.WindowsBuildImage.WIN_SERVER_CORE_2022_BASE_3_0,
},
});
}).not.toThrow('Windows Server 2022 images must be used with a fleet in us-west-2');
});

test('throws when 2022 WindowsImageType is used without a fleet in an unsupported region', () => {
// GIVEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-west-1' } });
const bucket = s3.Bucket.fromBucketName(stack, 'Bucket', 'my-bucket'); // (stack, 'Bucket');

// THEN
expect(() => {
new codebuild.Project(stack, 'Project', {
source: codebuild.Source.s3({
bucket,
path: 'path',
}),
environment: {
buildImage: codebuild.WindowsBuildImage.fromDockerRegistry(
'aws/codebuild/future-windows-version:2099-9.0',
{},
codebuild.WindowsImageType.SERVER_2022,
),
},
});
}).toThrow('Windows Server 2022 images must be used with a fleet');
});

test('can use 2022 WindowsImageType without a fleet in a supported region', () => {
// GIVEN
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-west-2' } });
const bucket = s3.Bucket.fromBucketName(stack, 'Bucket', 'my-bucket'); // (stack, 'Bucket');

// THEN
Expand Down
Loading