diff --git a/packages/@aws-cdk/aws-eks/lib/managed-nodegroup.ts b/packages/@aws-cdk/aws-eks/lib/managed-nodegroup.ts index ccb48e4687b7c..69dd8223edc09 100644 --- a/packages/@aws-cdk/aws-eks/lib/managed-nodegroup.ts +++ b/packages/@aws-cdk/aws-eks/lib/managed-nodegroup.ts @@ -1,6 +1,6 @@ import { InstanceType, ISecurityGroup, SubnetSelection } from '@aws-cdk/aws-ec2'; import { IRole, ManagedPolicy, Role, ServicePrincipal } from '@aws-cdk/aws-iam'; -import { IResource, Resource, Annotations } from '@aws-cdk/core'; +import { IResource, Resource, Annotations, withResolved } from '@aws-cdk/core'; import { Construct } from 'constructs'; import { Cluster, ICluster } from './cluster'; import { CfnNodegroup } from './eks.generated'; @@ -321,12 +321,19 @@ export class Nodegroup extends Resource implements INodegroup { this.maxSize = props.maxSize ?? this.desiredSize; this.minSize = props.minSize ?? 1; - if (this.desiredSize > this.maxSize) { - throw new Error(`Desired capacity ${this.desiredSize} can't be greater than max size ${this.maxSize}`); - } - if (this.desiredSize < this.minSize) { - throw new Error(`Minimum capacity ${this.minSize} can't be greater than desired size ${this.desiredSize}`); - } + withResolved(this.desiredSize, this.maxSize, (desired, max) => { + if (desired === undefined) {return ;} + if (desired > max) { + throw new Error(`Desired capacity ${desired} can't be greater than max size ${max}`); + } + }); + + withResolved(this.desiredSize, this.minSize, (desired, min) => { + if (desired === undefined) {return ;} + if (desired < min) { + throw new Error(`Minimum capacity ${min} can't be greater than desired size ${desired}`); + } + }); if (props.launchTemplateSpec && props.diskSize) { // see - https://docs.aws.amazon.com/eks/latest/userguide/launch-templates.html diff --git a/packages/@aws-cdk/aws-eks/test/test.nodegroup.ts b/packages/@aws-cdk/aws-eks/test/test.nodegroup.ts index 75e0319d4fb48..2ffab57cd1d01 100644 --- a/packages/@aws-cdk/aws-eks/test/test.nodegroup.ts +++ b/packages/@aws-cdk/aws-eks/test/test.nodegroup.ts @@ -598,6 +598,58 @@ export = { test.throws(() => cluster.addNodegroupCapacity('ng', { desiredSize: 2, minSize: 3 }), /Minimum capacity 3 can't be greater than desired size 2/); test.done(); }, + 'can set minSize , maxSize and DesiredSize'(test: Test) { + // GIVEN + const { stack, vpc } = testFixture(); + const cluster = new eks.Cluster(stack, 'Cluster', { + vpc, + defaultCapacity: 0, + version: CLUSTER_VERSION, + }); + // WHEN + new eks.Nodegroup(stack, 'NodeGroup', { + cluster: cluster, + minSize: 2, + maxSize: 6, + desiredSize: 4, + }); + // THEN + expect(stack).to(haveResourceLike('AWS::EKS::Nodegroup', { + ScalingConfig: { + MinSize: 2, + MaxSize: 6, + DesiredSize: 4, + }, + }, + )); + test.done(); + }, + 'validation is not performed when using Tokens'(test: Test) { + // GIVEN + const { stack, vpc } = testFixture(); + const cluster = new eks.Cluster(stack, 'Cluster', { + vpc, + defaultCapacity: 0, + version: CLUSTER_VERSION, + }); + // WHEN + new eks.Nodegroup(stack, 'NodeGroup', { + cluster: cluster, + minSize: cdk.Lazy.number({ produce: () => 5 }), + maxSize: cdk.Lazy.number({ produce: () => 1 }), + desiredSize: cdk.Lazy.number({ produce: () => 20 }), + }); + // THEN + expect(stack).to(haveResourceLike('AWS::EKS::Nodegroup', { + ScalingConfig: { + MinSize: 5, + MaxSize: 1, + DesiredSize: 20, + }, + }, + )); + test.done(); + }, 'create nodegroup correctly with launch template'(test: Test) { // GIVEN const { stack, vpc } = testFixture();