aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws-cdk-lib: exporting a class that implements IVpc but only looks up a vpc by Id

Open quaverBit opened this issue 2 years ago • 5 comments

Describe the feature

Having a class that is able to lookup a Vpc by it's Id and implementing IVpc so it can be extended with own methods and properties.

Use Case

Doing class MyVpc extends Vpc will try to deploy a new Vpc to aws and that is not intended. I want to be able to extend a class that looks up Vpc by it's id in something like:

class MyVpc extends VpcLookup {
  constructor(scope: Construct, id: string, props?: VpcLookupOptions) {
    super(scope, id, props);
  }

  get someSubnetGroup() {
    this.selectSubnets({
      ...
    })
  }
}

Proposed Solution

Maybe it's possible to move Vpc.fromLookup code/logic to inside of a seperated class constructor?

Other Information

Alternative solution would be export LookedUpVpc?

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

CDK version used

2

Environment details (OS name and version, etc.)

linux

quaverBit avatar Oct 19 '23 18:10 quaverBit

Given we already have LookedUpVpc, it's unclear to me why we need a new class in aws-ec2 ? Can you elaborate more on it?

pahud avatar Oct 20 '23 19:10 pahud

Thank you for your prompt reply!

We don't need a new class, but consider the following: First issue is LookedUpVpc is not exported. so right now it's impossible to do:

import { LookedUpVpc } from "aws-cdk-lib/aws-ec2"

Second issue (or more like improvement) is the arguments in constructor of LookedUpVpc, it would be rather nice to have it the same as Vpc.fromLookup.

Maybe extending the use case would clarity of my intended use, consider the following class extention:

class MyVpc extends LookedUpVpc {
  constructor(scope: Construct, id: string, props: VpcLookupOptions) { // VpcLookup as in arguments of Vpc.fromLookup
    super(scope, id, props);                                           // this is cunrrently wrong, but it's the desired functionality
  }

  get someSubnetGroup() {
    return this.selectSubnets({
      ...
    })
  }
}

then In my stack I can do something like:

class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const exampleVpc = new MyVpc(this, "my-vpc", { vpcId: "exampleVpc" });

    new NodejsFunction(this, "my-function", {
      vpc: exampleVpc,
      vpcSubnets: exampleVpc.someSubnetGroup,
    })
  }
}

I can exand further the utility in creating sharable constructs with intended default properties like:

class MyNodejsFunction extends NodejsFunction {
  constructor(scope: Construct, id: string, props: NodejsFunctionProps ){
    const vpcSubnets = props.vpc instanceof MyVpc ? props.vpc.someSubnetGroup : undefined
    super(scope, id, { vpcSubnets, ...props });
  }
}

As there is no class exported with a looked up vpc, the best approach so far would be something of sorts:

class MyVpc extends Construct {
  private vpc: IVpc;

  constructor(scope: Construct, id: string, vpcId: string) {
    super(scope, id);
    this.vpc = Vpc.fromLookup(this, "vpc-lookup", { vpcId });
  }
}

But as MyVpc is not an implementation of IVpc no longer can be used directly in aws-cdk constructs.

quaverBit avatar Oct 20 '23 22:10 quaverBit

Why not just have your class implement IVpc?

gshpychka avatar Oct 23 '23 17:10 gshpychka

@quaverBit Please confirm if this feature request is still valid and if you are unblocked.

ashishdhingra avatar Jun 06 '24 20:06 ashishdhingra

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Jun 09 '24 00:06 github-actions[bot]