Skip to content

Commit 55bf451

Browse files
authored
fix(eks-v2-alpha): prevent IAM role creation when node pools are empty (#33894)
When node pools are disabled (by setting an empty array in nodePools), the IAM role will not be created, preventing deployment failures with the error 'When Compute Config nodeRoleArn is not null or empty, nodePool value(s) must be provided. ### Issue # (if applicable) Fixes #33771 ### Reason for this change When using EKS Auto Mode with empty node pools (by setting `nodePools: []`), the IAM role was still being created by the L2 construct, causing stack deployment failures. The AWS service returns an error stating that when `nodeRoleArn` is provided, node pool values must also be provided. ### Description of changes Modified the `computeConfig` section in the `CfnCluster` resource to check if `nodePools` is empty before assigning `nodeRoleArn`. If `nodePools` is empty, `nodeRoleArn` will be set to `undefined` to prevent the unnecessary creation of the IAM role. The change ensures that when users explicitly disable node pools by providing an empty array, the IAM role won't be created, allowing the cluster to be provisioned successfully. Added a test case to verify that when node pools are empty: 1. The nodeRoleArn is not included in the CloudFormation template 2. No IAM role resource is created for node pools ### Describe any new or updated permissions being added No new or updated IAM permissions are being added. This change actually prevents the creation of an IAM role when it's not needed. ### Description of how you validated changes Added a new test case in `automode.test.ts` that verifies: - The `nodeRoleArn` property is not included in the CloudFormation template when node pools are empty - No IAM role resource is created for node pools when they are disabled The test passes, confirming that our fix works as expected. ### 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 0787840 commit 55bf451

13 files changed

+5769
-1078
lines changed

packages/@aws-cdk/aws-eks-v2-alpha/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ const cluster = new eks.Cluster(this, 'EksAutoCluster', {
148148

149149
For more information, see [Create a Node Pool for EKS Auto Mode](https://docs.aws.amazon.com/eks/latest/userguide/create-node-pool.html).
150150

151+
### Disabling Default Node Pools
152+
153+
You can disable the default node pools entirely by setting an empty array for `nodePools`. This is useful when you want to use Auto Mode features but manage your compute resources separately:
154+
155+
```ts
156+
const cluster = new eks.Cluster(this, 'EksAutoCluster', {
157+
version: eks.KubernetesVersion.V1_32,
158+
defaultCapacityType: eks.DefaultCapacityType.AUTOMODE,
159+
compute: {
160+
nodePools: [], // Disable default node pools
161+
},
162+
});
163+
```
164+
165+
When node pools are disabled this way, no IAM role will be created for the node pools, preventing deployment failures that would otherwise occur when a role is created without any node pools.
166+
151167
### Node Groups as the default capacity type
152168

153169
If you prefer to manage your own node groups instead of using Auto Mode, you can use the traditional node group approach by specifying `defaultCapacityType` as `NODEGROUP`:

packages/@aws-cdk/aws-eks-v2-alpha/lib/cluster.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,11 @@ export class Cluster extends ClusterBase {
11841184
enabled: autoModeEnabled,
11851185
// If the computeConfig enabled flag is set to false when creating a cluster with Auto Mode,
11861186
// the request must not include values for the nodeRoleArn or nodePools fields.
1187+
// Also, if nodePools is empty, nodeRoleArn should not be included to prevent deployment failures
11871188
nodePools: !autoModeEnabled ? undefined : props.compute?.nodePools ?? ['system', 'general-purpose'],
1188-
nodeRoleArn: !autoModeEnabled ? undefined : props.compute?.nodeRole?.roleArn ?? this.addNodePoolRole(`${id}nodePoolRole`).roleArn,
1189+
nodeRoleArn: !autoModeEnabled || (props.compute?.nodePools && props.compute.nodePools.length === 0) ?
1190+
undefined :
1191+
props.compute?.nodeRole?.roleArn ?? this.addNodePoolRole(`${id}nodePoolRole`).roleArn,
11891192
},
11901193
storageConfig: {
11911194
blockStorage: {

packages/@aws-cdk/aws-eks-v2-alpha/test/automode.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,43 @@ describe('eks auto mode', () => {
235235
},
236236
});
237237
});
238+
239+
test('does not include nodeRoleArn when nodePools is empty', () => {
240+
const { stack } = testFixtureNoVpc();
241+
242+
new eks.Cluster(stack, 'Cluster', {
243+
version: CLUSTER_VERSION,
244+
defaultCapacityType: eks.DefaultCapacityType.AUTOMODE,
245+
compute: {
246+
nodePools: [],
247+
},
248+
});
249+
250+
// Verify that nodeRoleArn is not included in the CloudFormation template
251+
Template.fromStack(stack).hasResourceProperties('AWS::EKS::Cluster', {
252+
ComputeConfig: {
253+
Enabled: true,
254+
NodePools: [],
255+
},
256+
});
257+
258+
// Verify that nodeRoleArn is not present in the ComputeConfig
259+
const template = Template.fromStack(stack);
260+
const cluster = template.findResources('AWS::EKS::Cluster');
261+
const clusterLogicalId = Object.keys(cluster)[0];
262+
const computeConfig = cluster[clusterLogicalId].Properties.ComputeConfig;
263+
264+
expect(computeConfig).not.toHaveProperty('NodeRoleArn');
265+
266+
// Verify that no IAM role resource is created for node pools
267+
// The role would typically have a logical ID like 'ClusterClusternodePoolRole...'
268+
const iamRoles = template.findResources('AWS::IAM::Role');
269+
const nodePoolRoleKeys = Object.keys(iamRoles).filter(key =>
270+
key.includes('nodePoolRole'),
271+
);
272+
273+
expect(nodePoolRoleKeys.length).toBe(0);
274+
});
238275
});
239276

240277
describe('network configuration', () => {

packages/@aws-cdk/aws-eks-v2-alpha/test/integ.eks-auto.js.snapshot/eks-auto-mode-empty-nodepools-stack.assets.json

+90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)