错误:此 VPC 中没有“公共”子网组。可用类型:

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

我正在创建一个自定义 Vpc()(即没有子网配置的新 Vpc())

    this.vpc = new Vpc(this, 'default', {
      vpcName: `${this.props.company.prefix}-vpc`,
      ipAddresses: IpAddresses.cidr('10.0.0.0/16'),
      natGateways: 0,
      createInternetGateway: false,
      enableDnsSupport: true,
      enableDnsHostnames: true,
      **subnetConfiguration**: [], // no subnets
      maxAzs: 3,
    });

...
    //
    // Create Route Tables
    //
    const publicRouteTable = this.createRouteTable("public");
    //
    // Create Public Subnets
    //
    this.createSubnet("public", "10.0.0.0/24", 0, publicRouteTable);

  //
  // create public route table
  //
  private createRouteTable(type: string): CfnRouteTable {
    //
    // Create Public Route Table
    //
    const routeTable = new CfnRouteTable(
      this,
      lnJoin(this.props.company.prefix, type, 'rt'),
      {
        vpcId: this.vpc.vpcId,
        tags: [
          {
            key: 'Name',
            value: lnJoin(this.props.company.prefix, type, 'rt'),
          },
        ],
      }
    );
    //
    // Create a Route
    //
    if (type === 'public') {
      new CfnRoute(
        this,
        lnJoin(this.props.company.prefix, type, 'rt', 'route'),
        {
          routeTableId: routeTable.ref,
          destinationCidrBlock: '0.0.0.0/0',
          gatewayId: this.igw.ref,
        }
      );
    }

    return routeTable;
  }

  //
  // Create Public Subnets
  //
  private createSubnet(
    type: string,
    cidrBlock: string,
    subnetIndex: number,
    routeTable: CfnRouteTable
  ): CfnSubnet {
    
    if (type !== 'public' && type !== 'private') {
      throw new Error('Invalid subnet type.');
    }

    let subnetType =
      type === 'public'
        ? SubnetType.PUBLIC
        : type === 'private'
          ? SubnetType.PRIVATE_WITH_EGRESS
          : SubnetType.PRIVATE_ISOLATED;

    const subnet = new CfnSubnet(
      this,
      lnJoin(
        this.props.company.prefix,
        type,
        'subnet',
        `(${this.vpc.availabilityZones[subnetIndex]})`
      ),
      {
        availabilityZone: this.vpc.availabilityZones[subnetIndex],
        vpcId: this.vpc.vpcId,
        cidrBlock: cidrBlock,
        mapPublicIpOnLaunch: type === 'public',
        tags: [
          {
            key: 'Name',
            value: lnJoin(
              this.props.company.prefix,
              type,
              'subnet',
              `(${this.vpc.availabilityZones[subnetIndex]})`
            ),
          },
          {
            key: this.SUBNETNAME_TAG,
            value: defaultSubnetName(subnetType),
          },
          {
            key: this.SUBNETTYPE_TAG,
            value: subnetTypeTagValue(subnetType),
          },
        ],
      }
    );

    new CfnSubnetRouteTableAssociation(
      this,
      lnJoin(
        this.props.company.prefix,
        type,
        'rt',
        'subnet',
        'assoc',
        `${subnetIndex}`
      ),
      {
        subnetId: subnet.ref,
        routeTableId: routeTable.ref,      
      },
    );

    return subnet;
  }

在后续的构造中,我尝试通过 vpc.publicSubnets() 进行交互,这是我收到此错误的时候

“错误:此 VPC 中没有‘公共’子网组。可用类型:”

几乎就好像我不定义subnetConfiguration[],vpc 就看不到我以编程方式创建的子网...

我在某处读到,创建公共子网时必须使用特定的 Tag()。

  private SUBNETTYPE_TAG = 'aws-cdk:subnet-type';
  private SUBNETNAME_TAG = 'aws-cdk:subnet-name';

即使我用上面的标签标记每个子网

        tags: [
          {
            key: 'Name',
            value: lnJoin(
              this.props.company.prefix,
              type,
              'subnet',
              `(${this.vpc.availabilityZones[subnetIndex]})`
            ),
          },
**          {
            key: this.SUBNETNAME_TAG,
            value: defaultSubnetName(subnetType),
          },
          {
            key: this.SUBNETTYPE_TAG,
            value: subnetTypeTagValue(subnetType),
          },**
        ],

谁能告诉我为什么会出现此错误?更好的是,我可以做什么来解决它?

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

您的观察是正确的 - 如果您指定了适当的

Vpc
,则
publicSubnets
L2 构造仅填充
subnetConfiguration
属性。

如果您手动创建子网,则无论您添加了哪些子网,都不会填充该子网。该构造没有任何启发式方法来确定子网是公共子网还是私有子网 - 它仅在创建 VPC 时查看

subnetConfiguration
中的子网类型。

以下是填充

publicSubnets
属性的相关代码(这只发生在创建
Vpc
构造期间):https://github.com/aws/aws-cdk/blob/0fc07f3762f3af821d742338c7839618a73ced50/packages/aws -cdk-lib/aws-ec2/lib/vpc.ts#L1868

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