aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.66k stars 3.92k forks source link

eks: cannot deploy multiple EKS clusters within the same stack #31942

Open badmintoncryer opened 1 week ago

badmintoncryer commented 1 week ago

Describe the bug

We cannot use multiple EKS clusters within the same stack.

It seems that the cause is the hardcoded ID of the KubectlLayer.

Regression Issue

Last Known Working CDK Version

2.160.0

Expected Behavior

Deploys successfully.

Current Behavior

Fails to deploy.

Reproduction Steps

import * as eks from 'aws-cdk-lib/aws-eks';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { getClusterVersionConfig } from './integ-tests-kubernetes-version';

class MultipleEksClusterStack extends Stack {
  constructor(scope: App, id: string) {
    super(scope, id);

    const vpc = new ec2.Vpc(this, 'Vpc', { natGateways: 1 });
    new eks.Cluster(this, 'EnabledCluster', {
      vpc,
      ...getClusterVersionConfig(this, eks.KubernetesVersion.V1_30),
      bootstrapSelfManagedAddons: true,
    });
    new eks.Cluster(this, 'DisabledCluster', {
      vpc,
      ...getClusterVersionConfig(this, eks.KubernetesVersion.V1_30),
      bootstrapSelfManagedAddons: false,
    });
  }
}

Results

/Users/kazuhoshinozuka/git/aws-cdk/node_modules/constructs/lib/construct.js:375
            throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^

Error: There is already a Construct with name 'KubectlLayer' in MultipleEksClusterStack [EksClusterStack]
    at Node.addChild (/Users/kazuhoshinozuka/git/aws-cdk/node_modules/constructs/lib/construct.js:375:19)
    at new Node (/Users/kazuhoshinozuka/git/aws-cdk/node_modules/constructs/lib/construct.js:38:21)
    at new Construct (/Users/kazuhoshinozuka/git/aws-cdk/node_modules/constructs/lib/construct.js:426:21)
    at new Resource (/Users/kazuhoshinozuka/git/aws-cdk/packages/aws-cdk-lib/core/lib/resource.js:32:9)
    at new LayerVersion (/Users/kazuhoshinozuka/git/aws-cdk/packages/aws-cdk-lib/aws-lambda/lib/layers.js:57:9)
    at new KubectlV30Layer (/Users/kazuhoshinozuka/git/aws-cdk/node_modules/@aws-cdk/lambda-layer-kubectl-v30/lib/kubectl-layer.js:13:9)
    at getClusterVersionConfig (/Users/kazuhoshinozuka/git/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-eks/test/integ-tests-kubernetes-version.js:27:23)
    at new EksClusterStack (/Users/kazuhoshinozuka/git/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-eks/test/integ.eks-bootstrap-self-managed-addons.js:19:77)
    at Object.<anonymous> (/Users/kazuhoshinozuka/git/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-eks/test/integ.eks-bootstrap-self-managed-addons.js:25:15)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.160.0

Framework Version

No response

Node.js Version

v22.9.0

OS

macOS

Language

TypeScript

Language Version

No response

Other information

No response

xazhao commented 1 week ago

Hi @badmintoncryer thanks for raising this issue. It's one of current aws-eks limitations. See this for detail.

We are planning to address the issue in the new eksv2 though. This is the tracking RFC: https://github.com/aws/aws-cdk-rfcs/issues/605

badmintoncryer commented 1 week ago

@xazhao Thank you for your detail information! I understand very well.