From 6d756e21e7b7156631a626fa26a034fb60596b58 Mon Sep 17 00:00:00 2001 From: Zhen Li Date: Thu, 16 Jan 2025 18:38:03 -0800 Subject: [PATCH] feat(codebuild): add new image types --- packages/aws-cdk-lib/aws-codebuild/README.md | 9 ++++- .../lib/linux-arm-build-image.ts | 26 +++++++++++- .../aws-cdk-lib/aws-codebuild/lib/project.ts | 40 ++++++++++++++++++- .../aws-codebuild/test/project.test.ts | 9 +++-- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/packages/aws-cdk-lib/aws-codebuild/README.md b/packages/aws-cdk-lib/aws-codebuild/README.md index 238af9d4af946..08aab8b12f507 100644 --- a/packages/aws-cdk-lib/aws-codebuild/README.md +++ b/packages/aws-cdk-lib/aws-codebuild/README.md @@ -329,9 +329,16 @@ is available for specifying Lambda-compatible images. You can specify one of the predefined Windows/Linux images by using one of the constants such as `WindowsBuildImage.WIN_SERVER_CORE_2019_BASE`, `WindowsBuildImage.WINDOWS_BASE_2_0`, `LinuxBuildImage.STANDARD_2_0`, -`LinuxBuildImage.AMAZON_LINUX_2_5`, `MacBuildImage.BASE_14`, `LinuxArmBuildImage.AMAZON_LINUX_2_ARM`, +`LinuxBuildImage.AMAZON_LINUX_2_5`, `LinuxArmBuildImage.AMAZON_LINUX_2_ARM`, `LinuxLambdaBuildImage.AMAZON_LINUX_2_NODE_18` or `LinuxArmLambdaBuildImage.AMAZON_LINUX_2_NODE_18`. +For non-containerized environment types: + +* `LINUX_EC2`: `LinuxBuildImage.AMAZON_LINUX_2023_1_0_AMI` +* `ARM_EC2`: `LinuxArmBuildImage.AMAZON_LINUX_2023_1_0_AMI` +* `WINDOWS_EC2`: `WindowsBuildImage.WIN_SERVER_2022_1_0_AMI` +* `MAC_ARM`: `MacBuildImage.BASE_14` + Alternatively, you can specify a custom image using one of the static methods on `LinuxBuildImage`: diff --git a/packages/aws-cdk-lib/aws-codebuild/lib/linux-arm-build-image.ts b/packages/aws-cdk-lib/aws-codebuild/lib/linux-arm-build-image.ts index ce112e6370a71..f5bbb97e4a8d9 100644 --- a/packages/aws-cdk-lib/aws-codebuild/lib/linux-arm-build-image.ts +++ b/packages/aws-cdk-lib/aws-codebuild/lib/linux-arm-build-image.ts @@ -6,6 +6,21 @@ import { BuildEnvironment, IBuildImage, ImagePullPrincipalType, DockerImageOptio import * as ecr from '../../aws-ecr'; import * as secretsmanager from '../../aws-secretsmanager'; +/** + * Environment type for Linux Arm Docker images or AMI + */ +export enum LinuxArmImageType { + /** + * The ARM_CONTAINER environment type + */ + ARM_CONTAINER = EnvironmentType.ARM_CONTAINER, + + /** + * The ARM_EC2 environment type + */ + ARM_EC2 = EnvironmentType.ARM_EC2, +} + /** * Construction properties of `LinuxArmBuildImage`. * Module-private, as the constructor of `LinuxArmBuildImage` is private. @@ -15,6 +30,7 @@ interface LinuxArmBuildImageProps { readonly imagePullPrincipalType?: ImagePullPrincipalType; readonly secretsManagerCredentials?: secretsmanager.ISecret; readonly repository?: ecr.IRepository; + readonly imageType?: LinuxArmImageType; } /** @@ -46,6 +62,13 @@ export class LinuxArmBuildImage implements IBuildImage { /** Image "aws/codebuild/amazonlinux-aarch64-standard:3.0" based on Amazon Linux 2023. */ public static readonly AMAZON_LINUX_2023_STANDARD_3_0 = LinuxArmBuildImage.fromCodeBuildImageId('aws/codebuild/amazonlinux-aarch64-standard:3.0'); + /** The Amazon Linux aarch64 1.0 AMI, based on Amazon Linux 2023. */ + public static readonly AMAZON_LINUX_2023_1_0_AMI: IBuildImage = new LinuxArmBuildImage({ + imageId: 'aws/codebuild/ami/amazonlinux-arm-base:2023-1.0', + imagePullPrincipalType: ImagePullPrincipalType.CODEBUILD, + imageType: LinuxArmImageType.ARM_EC2, + }); + /** * @returns a aarch-64 Linux build image from a Docker Hub image. */ @@ -93,7 +116,7 @@ export class LinuxArmBuildImage implements IBuildImage { }); } - public readonly type = EnvironmentType.ARM_CONTAINER as string; + public readonly type: string; public readonly defaultComputeType = ComputeType.LARGE; public readonly imageId: string; public readonly imagePullPrincipalType?: ImagePullPrincipalType; @@ -101,6 +124,7 @@ export class LinuxArmBuildImage implements IBuildImage { public readonly repository?: ecr.IRepository; private constructor(props: LinuxArmBuildImageProps) { + this.type = (props.imageType ?? LinuxArmImageType.ARM_CONTAINER).toString(); this.imageId = props.imageId; this.imagePullPrincipalType = props.imagePullPrincipalType; this.secretsManagerCredentials = props.secretsManagerCredentials; diff --git a/packages/aws-cdk-lib/aws-codebuild/lib/project.ts b/packages/aws-cdk-lib/aws-codebuild/lib/project.ts index 8b3ebd330e5c3..1c9b487786d85 100644 --- a/packages/aws-cdk-lib/aws-codebuild/lib/project.ts +++ b/packages/aws-cdk-lib/aws-codebuild/lib/project.ts @@ -1779,6 +1779,21 @@ export interface DockerImageOptions { readonly secretsManagerCredentials?: secretsmanager.ISecret; } +/** + * Environment type for Linux Docker images or AMI + */ +export enum LinuxImageType { + /** + * The LINUX_CONTAINER environment type + */ + LINUX_CONTAINER = EnvironmentType.LINUX_CONTAINER, + + /** + * The LINUX_EC2 environment type + */ + LINUX_EC2 = EnvironmentType.LINUX_EC2, +} + /** * Construction properties of `LinuxBuildImage`. * Module-private, as the constructor of `LinuxBuildImage` is private. @@ -1788,6 +1803,7 @@ interface LinuxBuildImageProps { readonly imagePullPrincipalType?: ImagePullPrincipalType; readonly secretsManagerCredentials?: secretsmanager.ISecret; readonly repository?: ecr.IRepository; + readonly imageType?: LinuxImageType; } // Keep around to resolve a circular dependency until removing deprecated ARM image constants from LinuxBuildImage @@ -1855,6 +1871,13 @@ export class LinuxBuildImage implements IBuildImage { /** The Amazon Coretto 11 image x86_64, based on Amazon Linux 2023. */ public static readonly AMAZON_LINUX_2023_CORETTO_11 = LinuxBuildImage.codeBuildImage('aws/codebuild/amazonlinux-x86_64-standard:corretto11'); + /** The Amazon Linux x86_64 1.0 AMI, based on Amazon Linux 2023. */ + public static readonly AMAZON_LINUX_2023_1_0_AMI: IBuildImage = new LinuxBuildImage({ + imageId: 'aws/codebuild/ami/amazonlinux-x86_64-base:2023-1.0', + imagePullPrincipalType: ImagePullPrincipalType.CODEBUILD, + imageType: LinuxImageType.LINUX_EC2, + }); + /** * Image "aws/codebuild/amazonlinux2-aarch64-standard:1.0". * @see {LinuxArmBuildImage.AMAZON_LINUX_2_STANDARD_1_0} @@ -1997,7 +2020,7 @@ export class LinuxBuildImage implements IBuildImage { }); } - public readonly type = EnvironmentType.LINUX_CONTAINER as string; + public readonly type: string; public readonly defaultComputeType = ComputeType.SMALL; public readonly imageId: string; public readonly imagePullPrincipalType?: ImagePullPrincipalType; @@ -2005,6 +2028,7 @@ export class LinuxBuildImage implements IBuildImage { public readonly repository?: ecr.IRepository; private constructor(props: LinuxBuildImageProps) { + this.type = (props.imageType ?? LinuxImageType.LINUX_CONTAINER).toString(); this.imageId = props.imageId; this.imagePullPrincipalType = props.imagePullPrincipalType; this.secretsManagerCredentials = props.secretsManagerCredentials; @@ -2027,7 +2051,7 @@ export class LinuxBuildImage implements IBuildImage { } /** - * Environment type for Windows Docker images + * Environment type for Windows Docker images or AMI */ export enum WindowsImageType { /** @@ -2049,6 +2073,11 @@ export enum WindowsImageType { * @see https://docs.aws.amazon.com/codebuild/latest/userguide/fleets.html */ SERVER_2022 = EnvironmentType.WINDOWS_SERVER_2022_CONTAINER, + + /** + * The WINDOWS_EC2 environment type + */ + WINDOWS_EC2 = EnvironmentType.WINDOWS_EC2, } /** @@ -2143,6 +2172,13 @@ export class WindowsBuildImage implements IBuildImage { imageType: WindowsImageType.SERVER_2022, }); + /** The CodeBuild Windows Server 2022 1.0 AMI */ + public static readonly WIN_SERVER_2022_1_0_AMI: IBuildImage = new WindowsBuildImage({ + imageId: 'aws/codebuild/ami/windows-base:2022-1.0', + imagePullPrincipalType: ImagePullPrincipalType.CODEBUILD, + imageType: WindowsImageType.WINDOWS_EC2, + }); + /** * @returns a Windows build image from a Docker Hub image. */ diff --git a/packages/aws-cdk-lib/aws-codebuild/test/project.test.ts b/packages/aws-cdk-lib/aws-codebuild/test/project.test.ts index f1438ac0c4cbb..420de09df886e 100644 --- a/packages/aws-cdk-lib/aws-codebuild/test/project.test.ts +++ b/packages/aws-cdk-lib/aws-codebuild/test/project.test.ts @@ -966,8 +966,11 @@ describe('Environment', () => { }); test.each([ - ['Base 14', codebuild.MacBuildImage.BASE_14, 'aws/codebuild/macos-arm-base:14'], - ])('has build image for %s', (_, buildImage, expected) => { + ['macOS Base 14', codebuild.EnvironmentType.MAC_ARM, codebuild.MacBuildImage.BASE_14, 'aws/codebuild/macos-arm-base:14'], + ['Linux EC2 AL2023 1.0', codebuild.EnvironmentType.LINUX_EC2, codebuild.LinuxBuildImage.AMAZON_LINUX_2023_1_0_AMI, 'aws/codebuild/ami/amazonlinux-x86_64-base:2023-1.0'], + ['Arm EC2 AL2023 1.0', codebuild.EnvironmentType.ARM_EC2, codebuild.LinuxArmBuildImage.AMAZON_LINUX_2023_1_0_AMI, 'aws/codebuild/ami/amazonlinux-arm-base:2023-1.0'], + ['Windows EC2 Server 2022 1.0', codebuild.EnvironmentType.WINDOWS_EC2, codebuild.WindowsBuildImage.WIN_SERVER_2022_1_0_AMI, 'aws/codebuild/ami/windows-base:2022-1.0'], + ])('has build image for %s', (_, environmentType, buildImage, expected) => { // GIVEN const stack = new cdk.Stack(); const bucket = s3.Bucket.fromBucketName(stack, 'Bucket', 'my-bucket'); // (stack, 'Bucket'); @@ -975,7 +978,7 @@ describe('Environment', () => { fleetName: 'MyFleet', baseCapacity: 1, computeType: codebuild.FleetComputeType.MEDIUM, - environmentType: codebuild.EnvironmentType.MAC_ARM, + environmentType: environmentType, }); // WHEN