diff --git a/packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/handler.ts b/packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/handler.ts index 5db46ab5bd344..2a2f9ac2b3688 100644 --- a/packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/handler.ts +++ b/packages/@aws-cdk/aws-eks/lib/cluster-resource-handler/handler.ts @@ -4,6 +4,8 @@ import { IsCompleteResponse, OnEventResponse } from '@aws-cdk/custom-resources/l // eslint-disable-next-line import/no-extraneous-dependencies import * as aws from 'aws-sdk'; +const MAX_CLUSTER_NAME_LEN = 63; + export class ClusterResourceHandler { public get clusterName() { @@ -71,7 +73,7 @@ export class ClusterResourceHandler { throw new Error('"roleArn" is required'); } - const clusterName = this.newProps.name || `${this.logicalResourceId}-${this.requestId}`; + const clusterName = this.newProps.name || this.generateClusterName(); const resp = await this.eks.createCluster({ ...this.newProps, @@ -221,6 +223,13 @@ export class ClusterResourceHandler { }; } } + + private generateClusterName() { + // max length is 63 characters + const suffix = this.requestId.replace(/-/g, ''); // 32 chars + const prefix = this.logicalResourceId.substr(0, MAX_CLUSTER_NAME_LEN - suffix.length - 1); + return `${prefix}-${suffix}`; + } } export interface EksClient { @@ -265,3 +274,4 @@ function analyzeUpdate(oldProps: Partial, newProps updateLogging: JSON.stringify(newProps.logging) !== JSON.stringify(oldProps.logging), }; } + diff --git a/packages/@aws-cdk/aws-eks/test/test.cluster-resource-provider.ts b/packages/@aws-cdk/aws-eks/test/test.cluster-resource-provider.ts index a852a40a30166..9a0aabccc8041 100644 --- a/packages/@aws-cdk/aws-eks/test/test.cluster-resource-provider.ts +++ b/packages/@aws-cdk/aws-eks/test/test.cluster-resource-provider.ts @@ -24,7 +24,7 @@ export = { subnetIds: ['subnet1', 'subnet2'], securityGroupIds: ['sg1', 'sg2', 'sg3'] }, - name: 'MyResourceId-fake-request-id' + name: 'MyResourceId-fakerequestid' }); test.done(); @@ -200,7 +200,7 @@ export = { // THEN test.deepEqual(mocks.actualRequest.createClusterRequest!, { - name: 'MyResourceId-fake-request-id', + name: 'MyResourceId-fakerequestid', roleArn: 'arn:of:role', resourcesVpcConfig: { @@ -208,7 +208,7 @@ export = { securityGroupIds: ['sg1', 'sg2', 'sg3'] } }); - test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' }); + test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' }); test.done(); }, @@ -230,9 +230,9 @@ export = { })); const resp = await handler.onEvent(); - test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' }); + test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' }); test.deepEqual(mocks.actualRequest.createClusterRequest, { - name: 'MyResourceId-fake-request-id', + name: 'MyResourceId-fakerequestid', roleArn: 'arn:of:role', resourcesVpcConfig: { @@ -251,9 +251,9 @@ export = { })); const resp = await handler.onEvent(); - test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' }); + test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' }); test.deepEqual(mocks.actualRequest.createClusterRequest, { - name: 'MyResourceId-fake-request-id', + name: 'MyResourceId-fakerequestid', roleArn: 'new-arn' }); test.done(); @@ -295,9 +295,9 @@ export = { const resp = await handler.onEvent(); // THEN - test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fake-request-id' }); + test.deepEqual(resp, { PhysicalResourceId: 'MyResourceId-fakerequestid' }); test.deepEqual(mocks.actualRequest.createClusterRequest, { - name: 'MyResourceId-fake-request-id', + name: 'MyResourceId-fakerequestid', roleArn: 'new-arn' }); test.done();