在堆栈之间共享 VPC

问题描述 投票:0回答:1

我正在写下面这样的代码

export interface MyEc2StackProps extends StackProps {
}
export class MyEc2Stack extends Stack {  
  public readonly vpc: ec2.Vpc;
  constructor(scope: Construct, id: string, props?: MyEc2StackProps) {
    const vpc = ec2.Vpc.fromLookup(this, "myvpc", {
      isDefault: true,
    });
    this.vpc = vpc; #  this can't be compiled

我想获取 vpc 并导出 vpc 以在其他堆栈中使用。

但是这显示错误,

IVpc has no property from Vpc....

amazon-web-services aws-cdk amazon-vpc
1个回答
0
投票

将您的会员类型从

ec2.Vpc
切换到
ec2.IVpc
。你需要这样做,因为
ec2.Vpc.fromLookup
返回
ec2.IVpc
。它不是创建一个真正的 VPC,而是查找一个。并非所有操作都可用,因为它不是在您的代码中创建的。但是你应该能够使用
IVpc
来做大多数事情。

© www.soinside.com 2019 - 2024. All rights reserved.