Skip to content

Commit 3485d53

Browse files
fix(batch): undeprecate useOptimalInstanceClasses property (#36353)
### Issue # (if applicable) Closes #36291. ### Reason for this change The `useOptimalInstanceClasses` property was deprecated in v2.220 in favor of `defaultInstanceClasses`, but there is no way to exclude the `optimal` instance type using `defaultInstanceClasses` alone. Users who want to specify only custom instance types (e.g., R4 only) without `optimal` being automatically added have no non-deprecated path forward. Setting `defaultInstanceClasses: []` (empty array) does not prevent `optimal` from being added because the current logic treats an empty array the same as `undefined`. ### Description of changes - Removed `@deprecated` annotation from `useOptimalInstanceClasses` property - Updated README to remove the deprecation warning and reorganized the "Choosing Your Instance Types" section with: - Clear subsections for each use case (Default Instance Classes, Specific Instance Types Only, Optimal Instance Classes) - A configuration reference table for quick lookup - Example showing how to use `useOptimalInstanceClasses: false` to exclude `optimal` #### Alternatives considered Three options were discussed in #36291: 1. **Remove deprecation** (this PR) - Restores `useOptimalInstanceClasses` as a non-deprecated property 2. **Treat empty `defaultInstanceClasses: []` as "exclude all defaults"** - Would introduces implicit behavior 3. **Change default behavior** - Breaking change, not acceptable Option 1 was chosen because: - Minimal code change (documentation only) - No risk of breaking existing users - `useOptimalInstanceClasses` provides explicit, clear control that `defaultInstanceClasses` cannot fully replace ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Existing tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2d5c210 commit 3485d53

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

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

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,68 +65,88 @@ For stateful or otherwise non-interruption-tolerant workflows, omit `spot` or se
6565

6666
#### Choosing Your Instance Types
6767

68-
Batch allows you to choose most up-to-date instance classes based on your region.
69-
This example configures your `ComputeEnvironment` to use only ARM64 instance:
68+
There are several ways to configure instance types for your compute environment:
69+
70+
##### Using Default Instance Classes (Recommended)
71+
72+
AWS Batch provides default instance classes that automatically select cost-effective, up-to-date instances based on your region.
73+
This is the recommended approach for new projects:
7074

7175
```ts
72-
const vpc = new ec2.Vpc(this, 'VPC');
76+
const vpc = new ec2.Vpc(this, 'Vpc');
7377

74-
new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
78+
// Use ARM64 instances (e.g., m6g, c6g, r6g, c7g families)
79+
new batch.ManagedEc2EcsComputeEnvironment(this, 'Arm64Ec2ComputeEnv', {
7580
vpc,
7681
defaultInstanceClasses: [batch.DefaultInstanceClass.ARM64],
7782
});
78-
```
79-
80-
This example configures your `ComputeEnvironment` to use only the `M5AD.large` instance:
81-
82-
```ts
83-
const vpc = new ec2.Vpc(this, 'VPC');
8483

85-
new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
84+
// Use x86_64 instances (e.g., m6i, c6i, r6i, c7i families)
85+
new batch.ManagedEc2EcsComputeEnvironment(this, 'X86_64Ec2ComputeEnv', {
8686
vpc,
87-
instanceTypes: [ec2.InstanceType.of(ec2.InstanceClass.M5AD, ec2.InstanceSize.LARGE)],
87+
defaultInstanceClasses: [batch.DefaultInstanceClass.X86_64],
8888
});
8989
```
9090

91-
Batch allows you to specify only the instance class and to let it choose the size, which you can do like this:
91+
The `default_x86_64` and `default_arm64` categories are dynamically updated by AWS as new instance families become available in your region.
92+
93+
##### Using Specific Instance Types Only
94+
95+
To use only specific instance types without any automatic defaults, set `useOptimalInstanceClasses: false`:
9296

9397
```ts
94-
const vpc = new ec2.Vpc(this, 'VPC');
98+
const vpc = new ec2.Vpc(this, 'Vpc');
9599

96-
declare const computeEnv: batch.IManagedEc2EcsComputeEnvironment
97-
computeEnv.addInstanceClass(ec2.InstanceClass.M5AD);
98-
// Or, specify it on the constructor:
99-
new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
100+
// Use only R4 instance class (Batch chooses the size)
101+
new batch.ManagedEc2EcsComputeEnvironment(this, 'R4Ec2ComputeEnv', {
100102
vpc,
103+
useOptimalInstanceClasses: false,
101104
instanceClasses: [ec2.InstanceClass.R4],
102105
});
106+
107+
// Use only a specific instance type
108+
new batch.ManagedEc2EcsComputeEnvironment(this, 'M5AdLargeEc2ComputeEnv', {
109+
vpc,
110+
useOptimalInstanceClasses: false,
111+
instanceTypes: [ec2.InstanceType.of(ec2.InstanceClass.M5AD, ec2.InstanceSize.LARGE)],
112+
});
103113
```
104114

105-
> [!WARNING]
106-
> `useOptimalInstanceClasses` is deprecated! Use `defaultInstanceClasses` instead.
115+
##### Using Optimal Instance Classes
116+
117+
By default, `useOptimalInstanceClasses` is `true`, which adds the `optimal` instance type.
118+
119+
**Note**: Since November 2025, `optimal` behaves the same as `default_x86_64` and is dynamically updated as AWS introduces new instance families. Both options automatically select cost-effective x86_64 instance types (from the m6i, c6i, r6i, and c7i families) based on your region.
107120

108-
Unless you explicitly specify `useOptimalInstanceClasses: false`, this compute environment will use `'optimal'` instances,
109-
which tells Batch to pick an instance from the C4, M4, and R4 instance families.
110-
*Note*: Batch does not allow specifying instance types or classes with different architectures.
111-
For example, `InstanceClass.A1` cannot be specified alongside `'optimal'`,
112-
because `A1` uses ARM and `'optimal'` uses x86_64.
113-
You can specify both `'optimal'` alongside several different instance types in the same compute environment:
121+
You can combine this with additional instance types:
114122

115123
```ts
116124
declare const vpc: ec2.IVpc;
117125

118-
const computeEnv = new batch.ManagedEc2EcsComputeEnvironment(this, 'myEc2ComputeEnv', {
119-
instanceTypes: [ec2.InstanceType.of(ec2.InstanceClass.M5AD, ec2.InstanceSize.LARGE)],
120-
useOptimalInstanceClasses: true, // default
126+
const computeEnv = new batch.ManagedEc2EcsComputeEnvironment(this, 'Ec2ComputeEnv', {
121127
vpc,
128+
instanceTypes: [ec2.InstanceType.of(ec2.InstanceClass.M5AD, ec2.InstanceSize.LARGE)],
129+
// useOptimalInstanceClasses: true (default)
122130
});
123-
// Note: this is equivalent to specifying
124-
computeEnv.addInstanceType(ec2.InstanceType.of(ec2.InstanceClass.M5AD, ec2.InstanceSize.LARGE));
125-
computeEnv.addInstanceClass(ec2.InstanceClass.C4);
126-
computeEnv.addInstanceClass(ec2.InstanceClass.M4);
127-
computeEnv.addInstanceClass(ec2.InstanceClass.R4);
131+
// Result: ['m5ad.large', 'optimal']
128132
```
129133

134+
##### Instance Type Configuration Reference
135+
136+
| Goal | Configuration |
137+
|------|---------------|
138+
| Use latest x86_64 instances | `defaultInstanceClasses: [DefaultInstanceClass.X86_64]` or no configuration (default) |
139+
| Use latest ARM64 instances | `defaultInstanceClasses: [DefaultInstanceClass.ARM64]` |
140+
| Use only specific instance classes | `useOptimalInstanceClasses: false` + `instanceClasses: [...]` |
141+
| Use only specific instance types | `useOptimalInstanceClasses: false` + `instanceTypes: [...]` |
142+
| Use optimal + additional instances | `instanceClasses: [...]` or `instanceTypes: [...]` |
143+
144+
**Note**: Batch does not allow specifying instance types or classes with different architectures.
145+
For example, `InstanceClass.A1` (ARM) cannot be specified alongside `optimal` (x86_64).
146+
When using ARM-based instances (e.g., Graviton), use `defaultInstanceClasses: [DefaultInstanceClass.ARM64]`, or set `useOptimalInstanceClasses: false` and explicitly specify ARM instance classes/types.
147+
148+
**Note**: `useOptimalInstanceClasses` and `defaultInstanceClasses` cannot be used together.
149+
130150
#### Configure AMIs
131151

132152
You can configure Amazon Machine Images (AMIs). This example configures your `ComputeEnvironment` to use Amazon Linux 2023.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ export interface ManagedEc2EcsComputeEnvironmentProps extends ManagedComputeEnvi
518518
* C4, M4, and R4 instance classes. You can specify other instance classes
519519
* (of the same architecture) in addition to the optimal instance classes.
520520
*
521-
* @deprecated use defaultInstanceClasses instead
522521
* @default true
523522
*/
524523
readonly useOptimalInstanceClasses?: boolean;
@@ -942,7 +941,6 @@ export interface ManagedEc2EksComputeEnvironmentProps extends ManagedComputeEnvi
942941
* C4, M4, and R4 instance classes. You can specify other instance classes
943942
* (of the same architecture) in addition to the optimal instance classes.
944943
*
945-
* @deprecated use defaultInstanceClasses instead
946944
* @default true
947945
*/
948946
readonly useOptimalInstanceClasses?: boolean;
@@ -1073,7 +1071,7 @@ export class ManagedEc2EksComputeEnvironment extends ManagedComputeEnvironmentBa
10731071
addConstructMetadata(this, props);
10741072

10751073
if (props.defaultInstanceClasses && props.useOptimalInstanceClasses) {
1076-
throw new ValidationError('cannot use `defaultInstanceClasses` with `useOptimalInstanceClasses`. Please remove deprecated `useOptimalInstanceClasses`', this);
1074+
throw new ValidationError('cannot use `defaultInstanceClasses` with `useOptimalInstanceClasses`.', this);
10771075
}
10781076

10791077
this.kubernetesNamespace = props.kubernetesNamespace;

0 commit comments

Comments
 (0)