aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.36k stars 3.77k forks source link

(ec2.Vpc): (max_azs=3 does not take effect) #30732

Open syangdh opened 5 days ago

syangdh commented 5 days ago

Describe the bug

I am trying to create a VPC with 3 AZs, but it turns out it only used 2 AZ, i.e., only have 6 subnets instead of 9 subnets

class VPCStack(core.Stack):

    def __init__(self, scope: Construct, id: str, environment: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        self.vpc = ec2.Vpc(self, "SharedVPC",
            ip_addresses=ec2.IpAddresses.cidr("10.0.0.0/16"),
            max_azs=3,
            nat_gateways = 1,
            subnet_configuration=[
                ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PUBLIC,
                    name="Ingress",
                    cidr_mask=24
                ),
                ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS,
                    name="Application",
                    cidr_mask=24
                ),
                ec2.SubnetConfiguration(
                    subnet_type=ec2.SubnetType.PRIVATE_ISOLATED,
                    name="Database",
                    cidr_mask=28
                )
            ]
        )

Expected Behavior

It should have created 9 subnets, but instead it created 6 subnets (My region is us-west-2)

Current Behavior

only 2 Az were used for the subnet creation

Reproduction Steps

Code is posted above

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.147.1 (build d3695d4)

Framework Version

No response

Node.js Version

v22.2.0

OS

mac OS

Language

Python

Language Version

Python

Other information

No response

khushail commented 4 days ago

Hi @syangdh , thanks for reaching out. I tried to deploy the VPC in the us-west-2 region and 9 subnets were successfully created. Sharing the code snippet in typescript (deplying in python meanwhile) and Subnets snapshot -

const vpc = new ec2.Vpc(this, 'Ecissue01Vpc', {
      ipAddresses :ec2.IpAddresses.cidr('10.0.0.0/16'),
        maxAzs: 3, 
        natGateways: 1, 
        subnetConfiguration: [
          {
            cidrMask: 24,
            name: 'public',
            subnetType: ec2.SubnetType.PUBLIC,
          },
          {
            cidrMask: 24,
            name: 'private',
            subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
          },
          {
            cidrMask: 24,
            name: 'isolated',
            subnetType: ec2.SubnetType.PRIVATE_ISOLATED
        }]
     });

const app = new cdk.App();
new Ecissue01Stack(app, 'Ecissue01Stack', {
   env: { account: '************', region: 'us-west-2' },
});

Subnets created in us-west-2 -

Screenshot 2024-07-02 at 1 20 49 PM

Could you please check the code and see if something is missing from your side?

khushail commented 4 days ago

works well for python as well - 9 subnets created -

Screenshot 2024-07-02 at 1 50 46 PM
github-actions[bot] commented 2 days ago

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.