aws-quickstart / cdk-eks-blueprints

AWS Quick Start Team
Apache License 2.0
447 stars 198 forks source link

Error referencing cross stack vpc #373

Closed daveschmidt86 closed 2 years ago

daveschmidt86 commented 2 years ago

I have two stacks in my project, InfraStack and EksStack. My VPC is created in the InfraStack and my goal is to reference that VPC in the EKS Stack as a DirectVpcProvider. I am receiving the follow error when doing so (code included below).

Error: "Argument of type 'import("/Users//Desktop/k8s/eks_blueprint_cdk_testing/node_modules/aws-cdk-lib/aws-ec2/lib/vpc").IVpc' is not assignable to parameter of type 'import("/Users//Desktop/k8s/eks_blueprint_cdk_testing/node_modules/@aws-quickstart/eks-blueprints/node_modules/aws-cdk-lib/aws-ec2/lib/vpc").IVpc'. Types of property 'publicSubnets' are incompatible. Type 'import("/Users//Desktop/k8s/eks_blueprint_cdk_testing/node_modules/aws-cdk-lib/aws-ec2/lib/vpc").ISubnet[]' is not assignable to type 'import("/Users//Desktop/k8s/eks_blueprint_cdk_testing/node_modules/@aws-quickstart/eks-blueprints/node_modules/aws-cdk-lib/aws-ec2/lib/vpc").ISubnet[]'. Type 'import("/Users//Desktop/k8s/eks_blueprint_cdk_testing/node_modules/aws-cdk-lib/aws-ec2/lib/vpc").ISubnet' is not assignable to type"

InfraStack:

import { Stack, StackProps } from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import * as eks_blueprints from '@aws-quickstart/eks-blueprints';

export class InfraStack extends Stack {
  eksvpc: ec2.IVpc;

  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    this.eksvpc = new ec2.Vpc(this, 'eks-blueprint-vpc', {
      cidr: "10.0.0.0/16"
    });
  }
}

EksStack:

import { Stack, StackProps } from 'aws-cdk-lib';
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import * as eks_blueprints from '@aws-quickstart/eks-blueprints';
import { InfraStack } from './infra_stack';

interface ClusterProps extends cdk.StackProps {
  vpc: ec2.IVpc
}

export class EksBlueprintStack extends Stack {
  constructor(scope: Construct, id: string, props: ClusterProps) {
    super(scope, id, props);

  const addOns: Array<eks_blueprints.ClusterAddOn> = [
    new eks_blueprints.addons.ArgoCDAddOn,
    new eks_blueprints.addons.CalicoAddOn,
    new eks_blueprints.addons.MetricsServerAddOn,
    new eks_blueprints.addons.ClusterAutoScalerAddOn,
    new eks_blueprints.addons.ContainerInsightsAddOn,
    new eks_blueprints.addons.AwsLoadBalancerControllerAddOn(),
    new eks_blueprints.addons.VpcCniAddOn(),
    new eks_blueprints.addons.CoreDnsAddOn(),
    new eks_blueprints.addons.KubeProxyAddOn(),
    new eks_blueprints.addons.XrayAddOn()
  ];

  const builder = eks_blueprints.EksBlueprint.builder()
    .resourceProvider(eks_blueprints.GlobalResources.Vpc, new eks_blueprints.DirectVpcProvider(props.vpc))
  }
}

CDK Main File:

#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { EksBlueprintStack } from '../lib/eks_stack';
import { InfraStack } from '../lib/infra_stack';

const app = new cdk.App();
const EksInfraStack = new InfraStack(app, 'EKSInfraStack', {});
const EksStack = new EksBlueprintStack(app, 'EksBlueprintCdkTestingStack', {
  vpc: EksInfraStack.eksvpc
});

EksStack.addDependency(EksInfraStack);
shapirov103 commented 2 years ago

I am observing that the parameter types referenced in the error message are identical: basically the error message states that IVpc is not assignable to IVpc. As you can see they are pointing to different CDK instances. Such issues can happen if the version of the CDK used in your project is different from the version leveraged for the blueprints.

We have turned off version range on CDK dependency in the blueprints, since we ran into breaking changes. So if you leverage CDK version 2.20.0 the issue should be fixed.

You can either reinstall CDK globally at that version, or you can override the version of CDK for the project using overrides (provided your NPM version is 8.4 or above). You can find the example of overrides here.

daveschmidt86 commented 2 years ago

@shapirov103 I checked, I am running 2.20.0 already. So given the reference of IVpc is different, should I reference the Blueprint one over the standard cdk interface?

Update: figured out my issue, forgot to update aws-cdk-lib to version 2.200 as well. Project is synthesizing now, will update if any further issues.