Skip to content

Commit 14b28bf

Browse files
Merge branch 'main' into fix/provisioned-poller-config-types-and-docs
2 parents 6f6cb4f + 689c661 commit 14b28bf

File tree

9 files changed

+477
-24
lines changed

9 files changed

+477
-24
lines changed

packages/aws-cdk-lib/aws-batch/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
164164
});
165165
```
166166

167+
If your image needs GPU resources, specify `ECS_AL2023_NVIDIA`:
168+
169+
```ts
170+
declare const vpc: ec2.IVpc;
171+
172+
new batch.ManagedEc2EcsComputeEnvironment(this, 'myGpuComputeEnv', {
173+
vpc,
174+
images: [
175+
{
176+
imageType: batch.EcsMachineImageType.ECS_AL2023_NVIDIA,
177+
},
178+
],
179+
});
180+
```
181+
167182
#### Allocation Strategies
168183

169184
| Allocation Strategy | Optimized for | Downsides |

packages/aws-cdk-lib/aws-batch/lib/managed-compute-environment.ts

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import type * as eks from '../../aws-eks';
77
import * as iam from '../../aws-iam';
88
import type { IRole } from '../../aws-iam';
99
import type { Duration, ITaggable } from '../../core';
10-
import { ArnFormat, Lazy, Resource, Stack, TagManager, TagType, Token, ValidationError } from '../../core';
10+
import { ArnFormat, FeatureFlags, Lazy, Resource, Stack, TagManager, TagType, Token, ValidationError } from '../../core';
1111
import { memoizedGetter } from '../../core/lib/helpers-internal';
1212
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
1313
import { lit } from '../../core/lib/private/literal-string';
1414
import { propertyInjectable } from '../../core/lib/prop-injectable';
15+
import * as cxapi from '../../cx-api';
1516

1617
/**
1718
* Represents a Managed ComputeEnvironment. Batch will provision EC2 Instances to
@@ -255,7 +256,8 @@ export interface IManagedEc2EcsComputeEnvironment extends IManagedComputeEnviron
255256
* Leave this `undefined` to allow Batch to choose the latest AMIs it supports for each instance that it launches.
256257
*
257258
* @default
258-
* - ECS_AL2 compatible AMI ids for non-GPU instances, ECS_AL2_NVIDIA compatible AMI ids for GPU instances
259+
* - ECS_AL2 compatible AMI ids for non-GPU instances, ECS_AL2_NVIDIA compatible AMI ids for GPU instances.
260+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, ECS_AL2023 will be used instead of ECS_AL2.
259261
*/
260262
readonly images?: EcsMachineImage[];
261263

@@ -384,7 +386,8 @@ export interface EcsMachineImage extends MachineImage {
384386
/**
385387
* Tells Batch which instance type to launch this image on
386388
*
387-
* @default - 'ECS_AL2' for non-gpu instances, 'ECS_AL2_NVIDIA' for gpu instances
389+
* @default - 'ECS_AL2' for non-gpu instances, 'ECS_AL2_NVIDIA' for gpu instances.
390+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, 'ECS_AL2023' will be used instead of 'ECS_AL2'.
388391
*/
389392
readonly imageType?: EcsMachineImageType;
390393
}
@@ -396,7 +399,8 @@ export interface EksMachineImage extends MachineImage{
396399
/**
397400
* Tells Batch which instance type to launch this image on
398401
*
399-
* @default - 'EKS_AL2' for non-gpu instances, 'EKS_AL2_NVIDIA' for gpu instances
402+
* @default - 'EKS_AL2' for non-gpu instances, 'EKS_AL2_NVIDIA' for gpu instances.
403+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, 'EKS_AL2023' will be used instead of 'EKS_AL2'.
400404
*/
401405
readonly imageType?: EksMachineImageType;
402406
}
@@ -420,6 +424,11 @@ export enum EcsMachineImageType {
420424
* Tells Batch that this machine image runs on GPU instances
421425
*/
422426
ECS_AL2_NVIDIA = 'ECS_AL2_NVIDIA',
427+
428+
/**
429+
* Tells Batch that this machine image runs on GPU AL2023 instances
430+
*/
431+
ECS_AL2023_NVIDIA = 'ECS_AL2023_NVIDIA',
423432
}
424433

425434
/**
@@ -435,6 +444,16 @@ export enum EksMachineImageType {
435444
* Tells Batch that this machine image runs on GPU instances
436445
*/
437446
EKS_AL2_NVIDIA = 'EKS_AL2_NVIDIA',
447+
448+
/**
449+
* Tells Batch that this machine image runs on non-GPU AL2023 instances
450+
*/
451+
EKS_AL2023 = 'EKS_AL2023',
452+
453+
/**
454+
* Tells Batch that this machine image runs on GPU AL2023 instances
455+
*/
456+
EKS_AL2023_NVIDIA = 'EKS_AL2023_NVIDIA',
438457
}
439458

440459
/**
@@ -551,12 +570,15 @@ export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedEc2ComputeE
551570
/**
552571
* Configure which AMIs this Compute Environment can launch.
553572
* If you specify this property with only `image` specified, then the
554-
* `imageType` will default to `ECS_AL2`. *If your image needs GPU resources,
555-
* specify `ECS_AL2_NVIDIA`; otherwise, the instances will not be able to properly
556-
* join the ComputeEnvironment*.
573+
* `imageType` will default to `ECS_AL2` (or `ECS_AL2023` if the
574+
* `@aws-cdk/aws-batch:defaultToAL2023` feature flag is set).
575+
* *If your image needs GPU resources,
576+
* specify `ECS_AL2_NVIDIA` or `ECS_AL2023_NVIDIA`; otherwise, the instances
577+
* will not be able to properly join the ComputeEnvironment*.
557578
*
558579
* @default
559-
* - ECS_AL2 for non-GPU instances, ECS_AL2_NVIDIA for GPU instances
580+
* - ECS_AL2 for non-GPU instances, ECS_AL2_NVIDIA for GPU instances.
581+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, ECS_AL2023 will be used instead of ECS_AL2.
560582
*/
561583
readonly images?: EcsMachineImage[];
562584

@@ -782,11 +804,26 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedEc2ComputeEnvironmen
782804
: undefined
783805
);
784806

785-
if (this.images?.find(image => image.imageType === EcsMachineImageType.ECS_AL2023) &&
786-
(this.instanceClasses.includes(ec2.InstanceClass.A1) ||
787-
this.instanceTypes.find(instanceType => instanceType.sameInstanceClassAs(ec2.InstanceType.of(ec2.InstanceClass.A1, ec2.InstanceSize.LARGE))))
788-
) {
789-
throw new ValidationError(lit`AmazonLinuxSupportInstances`, 'Amazon Linux 2023 does not support A1 instances.', this);
807+
const hasA1Instances = this.instanceClasses.includes(ec2.InstanceClass.A1) ||
808+
this.instanceTypes.some(instanceType => instanceType.sameInstanceClassAs(ec2.InstanceType.of(ec2.InstanceClass.A1, ec2.InstanceSize.LARGE)));
809+
810+
const willUseAL2023 = (!this.images || this.images.length === 0)
811+
? FeatureFlags.of(this).isEnabled(cxapi.BATCH_DEFAULT_AL2023)
812+
: this.images.some(image => {
813+
const resolved = image.imageType ?? (
814+
FeatureFlags.of(this).isEnabled(cxapi.BATCH_DEFAULT_AL2023)
815+
? EcsMachineImageType.ECS_AL2023
816+
: EcsMachineImageType.ECS_AL2
817+
);
818+
return [EcsMachineImageType.ECS_AL2023, EcsMachineImageType.ECS_AL2023_NVIDIA].includes(resolved);
819+
});
820+
821+
if (willUseAL2023 && hasA1Instances) {
822+
throw new ValidationError(
823+
lit`AmazonLinuxSupportInstances`,
824+
'Amazon Linux 2023 does not support A1 instances. To use A1 instances, explicitly set imageType to ECS_AL2 or disable the @aws-cdk/aws-batch:defaultToAL2023 feature flag.',
825+
this,
826+
);
790827
}
791828

792829
const { instanceRole, instanceProfile } = createInstanceRoleAndProfile(this, props.instanceRole);
@@ -821,7 +858,11 @@ export class ManagedEc2EcsComputeEnvironment extends ManagedEc2ComputeEnvironmen
821858
ec2Configuration: this.images?.map((image) => {
822859
return {
823860
imageIdOverride: image.image?.getImage(this).imageId,
824-
imageType: image.imageType ?? EcsMachineImageType.ECS_AL2,
861+
imageType: image.imageType ?? (
862+
FeatureFlags.of(this).isEnabled(cxapi.BATCH_DEFAULT_AL2023)
863+
? EcsMachineImageType.ECS_AL2023
864+
: EcsMachineImageType.ECS_AL2
865+
),
825866
};
826867
}),
827868
placementGroup: props.placementGroup?.placementGroupRef.groupName,
@@ -868,7 +909,8 @@ interface IManagedEc2EksComputeEnvironment extends IManagedEc2ComputeEnvironment
868909
* Configure which AMIs this Compute Environment can launch.
869910
*
870911
* @default
871-
* EKS_AL2 for non-GPU instances, EKS_AL2_NVIDIA for GPU instances,
912+
* EKS_AL2 for non-GPU instances, EKS_AL2_NVIDIA for GPU instances.
913+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, EKS_AL2023 will be used instead of EKS_AL2.
872914
*/
873915
readonly images?: EksMachineImage[];
874916

@@ -985,9 +1027,10 @@ export interface ManagedEc2EksComputeEnvironmentProps extends ManagedEc2ComputeE
9851027
*
9861028
* @default
9871029
* If `imageKubernetesVersion` is specified,
988-
* - EKS_AL2 for non-GPU instances, EKS_AL2_NVIDIA for GPU instances,
1030+
* - EKS_AL2 for non-GPU instances, EKS_AL2_NVIDIA for GPU instances.
9891031
* Otherwise,
990-
* - ECS_AL2 for non-GPU instances, ECS_AL2_NVIDIA for GPU instances,
1032+
* - ECS_AL2 for non-GPU instances, ECS_AL2_NVIDIA for GPU instances.
1033+
* If the '@aws-cdk/aws-batch:defaultToAL2023' feature flag is set, EKS_AL2023 / ECS_AL2023 will be used instead.
9911034
*/
9921035
readonly images?: EksMachineImage[];
9931036

@@ -1144,11 +1187,23 @@ export class ManagedEc2EksComputeEnvironment extends ManagedEc2ComputeEnvironmen
11441187
bidPercentage: this.spotBidPercentage,
11451188
launchTemplate: this.launchTemplate ? {
11461189
launchTemplateId: this.launchTemplate?.launchTemplateId,
1190+
userdataType: this.images?.some(image => {
1191+
const resolved = image.imageType ?? (
1192+
FeatureFlags.of(this).isEnabled(cxapi.BATCH_DEFAULT_AL2023)
1193+
? EksMachineImageType.EKS_AL2023
1194+
: EksMachineImageType.EKS_AL2
1195+
);
1196+
return [EksMachineImageType.EKS_AL2023, EksMachineImageType.EKS_AL2023_NVIDIA].includes(resolved);
1197+
}) ? 'EKS_NODEADM' : undefined,
11471198
} : undefined,
11481199
ec2Configuration: this.images?.map((image) => {
11491200
return {
11501201
imageIdOverride: image.image?.getImage(this).imageId,
1151-
imageType: image.imageType ?? EksMachineImageType.EKS_AL2,
1202+
imageType: image.imageType ?? (
1203+
FeatureFlags.of(this).isEnabled(cxapi.BATCH_DEFAULT_AL2023)
1204+
? EksMachineImageType.EKS_AL2023
1205+
: EksMachineImageType.EKS_AL2
1206+
),
11521207
};
11531208
}),
11541209
placementGroup: props.placementGroup?.placementGroupRef.groupName,

0 commit comments

Comments
 (0)