From 6f37cf75512813c92448283f6c478e7683199e9b Mon Sep 17 00:00:00 2001 From: phuhung273 Date: Tue, 28 Oct 2025 04:23:21 +0000 Subject: [PATCH] chore(batch): init base EC2 class for ECS and EKS Signed-off-by: phuhung273 --- .../lib/managed-compute-environment.ts | 171 +++++++++--------- 1 file changed, 82 insertions(+), 89 deletions(-) diff --git a/packages/aws-cdk-lib/aws-batch/lib/managed-compute-environment.ts b/packages/aws-cdk-lib/aws-batch/lib/managed-compute-environment.ts index de0e17446fdea..e8034f122a928 100644 --- a/packages/aws-cdk-lib/aws-batch/lib/managed-compute-environment.ts +++ b/packages/aws-cdk-lib/aws-batch/lib/managed-compute-environment.ts @@ -498,10 +498,32 @@ export enum DefaultInstanceClass { ARM64 = 'default_arm64', } +/** + * Props for a ManagedEc2ComputeEnvironment + */ +export interface ManagedEc2ComputeEnvironmentProps extends ManagedComputeEnvironmentProps { + /** + * The instance types that this Compute Environment can launch. + * Which one is chosen depends on the `AllocationStrategy` used. + * + * @default - the instances Batch considers will be used (currently C4, M4, and R4) + */ + readonly instanceTypes?: ec2.InstanceType[]; + + /** + * The instance classes that this Compute Environment can launch. + * Which one is chosen depends on the `AllocationStrategy` used. + * Batch will automatically choose the instance size. + * + * @default - the instances Batch considers will be used (currently C4, M4, and R4) + */ + readonly instanceClasses?: ec2.InstanceClass[]; +} + /** * Props for a ManagedEc2EcsComputeEnvironment */ -export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedComputeEnvironmentProps { +export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedEc2ComputeEnvironmentProps { /** * Use batch's default instance types. * A simpler way to choose up-to-date instance classes based on region @@ -567,23 +589,6 @@ export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedComputeEnvi */ readonly spotFleetRole?: iam.IRole; - /** - * The instance types that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - * - * @default - the instances Batch considers will be used (currently C4, M4, and R4) - */ - readonly instanceTypes?: ec2.InstanceType[]; - - /** - * The instance classes that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - * Batch will automatically choose the instance size. - * - * @default - the instances Batch considers will be used (currently C4, M4, and R4) - */ - readonly instanceClasses?: ec2.InstanceClass[]; - /** * The execution Role that instances launched by this Compute Environment will use. * @@ -626,13 +631,68 @@ export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedComputeEnvi readonly placementGroup?: ec2.IPlacementGroupRef; } +/** + * A ManagedComputeEnvironment that uses EC2 instances. + */ +interface IManagedEc2ComputeEnvironment extends IManagedComputeEnvironment { + /** + * The instance types that this Compute Environment can launch. + * Which one is chosen depends on the `AllocationStrategy` used. + */ + readonly instanceTypes: ec2.InstanceType[]; + + /** + * The instance types that this Compute Environment can launch. + * Which one is chosen depends on the `AllocationStrategy` used. + */ + readonly instanceClasses: ec2.InstanceClass[]; + + /** + * Add an instance type to this compute environment + */ + addInstanceType(instanceType: ec2.InstanceType): void; + + /** + * Add an instance class to this compute environment + */ + addInstanceClass(instanceClass: ec2.InstanceClass): void; +} + +/** + * A ManagedComputeEnvironment that uses EC2 instances. + * + */ +abstract class ManagedEc2ComputeEnvironment extends ManagedComputeEnvironmentBase implements IManagedEc2ComputeEnvironment { + public readonly instanceTypes: ec2.InstanceType[]; + public readonly instanceClasses: ec2.InstanceClass[]; + + constructor(scope: Construct, id: string, props: ManagedEc2ComputeEnvironmentProps) { + super(scope, id, props); + // Enhanced CDK Analytics Telemetry + addConstructMetadata(this, props); + + this.instanceTypes = props.instanceTypes ?? []; + this.instanceClasses = props.instanceClasses ?? []; + } + + @MethodMetadata() + public addInstanceType(instanceType: ec2.InstanceType): void { + this.instanceTypes.push(instanceType); + } + + @MethodMetadata() + public addInstanceClass(instanceClass: ec2.InstanceClass): void { + this.instanceClasses.push(instanceClass); + } +} + /** * A ManagedComputeEnvironment that uses ECS orchestration on EC2 instances. * * @resource AWS::Batch::ComputeEnvironment */ @propertyInjectable -export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBase implements IManagedEc2EcsComputeEnvironment { +export class ManagedEc2EcsComputeEnvironment extends ManagedEc2ComputeEnvironment implements IManagedEc2EcsComputeEnvironment { /** Uniquely identifies this class. */ public static readonly PROPERTY_INJECTION_ID: string = 'aws-cdk-lib.aws-batch.ManagedEc2EcsComputeEnvironment'; @@ -673,8 +733,6 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa public readonly allocationStrategy?: AllocationStrategy; public readonly spotBidPercentage?: number; public readonly spotFleetRole?: iam.IRole; - public readonly instanceTypes: ec2.InstanceType[]; - public readonly instanceClasses: ec2.InstanceClass[]; public readonly instanceRole?: iam.IRole; public readonly launchTemplate?: ec2.ILaunchTemplate; public readonly minvCpus?: number; @@ -701,8 +759,6 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa : undefined ); - this.instanceTypes = props.instanceTypes ?? []; - this.instanceClasses = props.instanceClasses ?? []; if (this.images?.find(image => image.imageType === EcsMachineImageType.ECS_AL2023) && (this.instanceClasses.includes(ec2.InstanceClass.A1) || this.instanceTypes.find(instanceType => instanceType.sameInstanceClassAs(ec2.InstanceType.of(ec2.InstanceClass.A1, ec2.InstanceSize.LARGE)))) @@ -763,22 +819,12 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedComputeEnvironmentBa public get placementGroup(): ec2.IPlacementGroup | undefined { return this._placementGroup ? asPlacementGroup(this._placementGroup, this) : undefined; } - - @MethodMetadata() - public addInstanceType(instanceType: ec2.InstanceType): void { - this.instanceTypes.push(instanceType); - } - - @MethodMetadata() - public addInstanceClass(instanceClass: ec2.InstanceClass): void { - this.instanceClasses.push(instanceClass); - } } /** * A ManagedComputeEnvironment that uses EKS orchestration on EC2 instances. */ -interface IManagedEc2EksComputeEnvironment extends IManagedComputeEnvironment { +interface IManagedEc2EksComputeEnvironment extends IManagedEc2ComputeEnvironment { /** * The namespace of the Cluster * @@ -833,18 +879,6 @@ interface IManagedEc2EksComputeEnvironment extends IManagedComputeEnvironment { */ readonly spotBidPercentage?: number; - /** - * The instance types that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - */ - readonly instanceTypes: ec2.InstanceType[]; - - /** - * The instance types that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - */ - readonly instanceClasses: ec2.InstanceClass[]; - /** * The execution Role that instances launched by this Compute Environment will use. * @@ -885,22 +919,12 @@ interface IManagedEc2EksComputeEnvironment extends IManagedComputeEnvironment { * @default - no placement group */ readonly placementGroup?: ec2.IPlacementGroup; - - /** - * Add an instance type to this compute environment - */ - addInstanceType(instanceType: ec2.InstanceType): void; - - /** - * Add an instance class to this compute environment - */ - addInstanceClass(instanceClass: ec2.InstanceClass): void; } /** * Props for a ManagedEc2EksComputeEnvironment */ -export interface ManagedEc2EksComputeEnvironmentProps extends ManagedComputeEnvironmentProps { +export interface ManagedEc2EksComputeEnvironmentProps extends ManagedEc2ComputeEnvironmentProps { /** * The namespace of the Cluster */ @@ -975,23 +999,6 @@ export interface ManagedEc2EksComputeEnvironmentProps extends ManagedComputeEnvi */ readonly spotBidPercentage?: number; - /** - * The instance types that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - * - * @default - the instances Batch considers will be used (currently C4, M4, and R4) - */ - readonly instanceTypes?: ec2.InstanceType[]; - - /** - * The instance types that this Compute Environment can launch. - * Which one is chosen depends on the `AllocationStrategy` used. - * Batch will automatically choose the instance size. - * - * @default - the instances Batch considers will be used (currently C4, M4, and R4) - */ - readonly instanceClasses?: ec2.InstanceClass[]; - /** * The execution Role that instances launched by this Compute Environment will use. * @@ -1040,7 +1047,7 @@ export interface ManagedEc2EksComputeEnvironmentProps extends ManagedComputeEnvi * @resource AWS::Batch::ComputeEnvironment */ @propertyInjectable -export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBase implements IManagedEc2EksComputeEnvironment { +export class ManagedEc2EksComputeEnvironment extends ManagedEc2ComputeEnvironment implements IManagedEc2EksComputeEnvironment { /** Uniquely identifies this class. */ public static readonly PROPERTY_INJECTION_ID: string = 'aws-cdk-lib.aws-batch.ManagedEc2EksComputeEnvironment'; public readonly kubernetesNamespace?: string; @@ -1052,8 +1059,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa public readonly images?: EksMachineImage[]; public readonly allocationStrategy?: AllocationStrategy; public readonly spotBidPercentage?: number; - public readonly instanceTypes: ec2.InstanceType[]; - public readonly instanceClasses: ec2.InstanceClass[]; public readonly instanceRole?: iam.IRole; public readonly launchTemplate?: ec2.ILaunchTemplate; public readonly minvCpus?: number; @@ -1079,8 +1084,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa throw new ValidationError(`ManagedEc2EksComputeEnvironment '${id}' uses invalid allocation strategy 'AllocationStrategy.BEST_FIT'`, this); } this.spotBidPercentage = props.spotBidPercentage; - this.instanceTypes = props.instanceTypes ?? []; - this.instanceClasses = props.instanceClasses ?? []; const { instanceRole, instanceProfile } = createInstanceRoleAndProfile(this, props.instanceRole); this.instanceRole = instanceRole; @@ -1138,16 +1141,6 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa public get placementGroup(): ec2.IPlacementGroup | undefined { return this._placementGroup ? asPlacementGroup(this._placementGroup, this) : undefined; } - - @MethodMetadata() - public addInstanceType(instanceType: ec2.InstanceType): void { - this.instanceTypes.push(instanceType); - } - - @MethodMetadata() - public addInstanceClass(instanceClass: ec2.InstanceClass): void { - this.instanceClasses.push(instanceClass); - } } /**