ec2.SubnetType 部署到 2 个可用区而不是 3 个

ec2.SubnetType deploying to 2AZs rather than 3

我在尝试使用 AWS CDK 将 3 个子网部署到 VPC 时遇到问题。子网仅部署到 2x AZ 而不是默认的 3x

目标: 使用 AWS CDK

部署具有 3 个隔离子网的 AWS VPC

预期结果: 在 VPC 构造中使用 costruct prop subnetType: ec2.SubnetType.ISOLATED 创建 3x 隔离可用区

实际结果: 子网仅部署到 2AZs

代码:

import * as cdk from '@aws-cdk/core';
import ec2 = require('@aws-cdk/aws-ec2');

export class CdkWorkshopStack extends cdk.Stack {

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

   new ec2.Vpc(this, 'VPC', {
    cidr: '10.0.0.0/16',
    maxAzs: 3,
    subnetConfiguration: [
       {
         cidrMask: 28,
         name: 'Private Subnet',
         subnetType: ec2.SubnetType.ISOLATED,
       }
    ]
 });

  }
}

找到答案:

'In an environment-agnostic stack, any constructs that use availability zones will see two of them'

所以我必须明确定义堆栈的环境。

文档在这里 - https://docs.aws.amazon.com/cdk/latest/guide/environments.html

示例代码:

new TestStack(app, 'TestStack', {
env: {
    region: "eu-central-1",
    account: "XXXXXXXX"
    }
});