question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

aws_cdk.aws_ec2.vpc only utilizes 2 availability zones no matter what max_azs is set to

See original GitHub issue

Note: for support questions, please first reference our documentation, then use Stackoverflow. This repository’s issues are intended for feature requests and bug reports.

  • I’m submitting a …

    • 🪲 bug report
    • 🚀 feature request
    • 📚 construct library gap
    • ☎️ security issue or vulnerability => Please see policy
    • ❓ support request => Please see note at the top of this template.
  • What is the current behavior? If the current behavior is a 🪲bug🪲: Please provide the steps to reproduce

The following code I would expect to utilize 3 azs, but only utilizes 2. We have a requirement to specifically utilize 3 azs and I don’t see a way to make that happen.

from aws_cdk import (
  core,
  aws_ec2 as ec2
)

app = core.App()

subnets = []
subnets.append(ec2.SubnetConfiguration(name = "public", subnet_type = ec2.SubnetType.PUBLIC, cidr_mask = 20))
subnets.append(ec2.SubnetConfiguration(name = "private", subnet_type = ec2.SubnetType.PRIVATE, cidr_mask = 20))
subnets.append(ec2.SubnetConfiguration(name = "isolated", subnet_type = ec2.SubnetType.ISOLATED, cidr_mask = 20))

vpc = ec2.Vpc(app, "MyVpc", subnet_configuration = subnets, max_azs = 3)

app.synth()
  • What is the expected behavior (or behavior of feature suggested)? I would expect the number of AZs to equal the number supplied to the max_azs variable.

  • What is the motivation / use case for changing the behavior or adding this feature?

  • Please tell us about your environment:

  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
roger-reedcommented, Jan 31, 2020

Workaround I noticed the CDK CLI was setting OS environment variables based on the passed profile on the CLI. Method to build CDK Environment off of environment variables:

def get_cdk_environment():
    return Environment(account=os.environ['CDK_DEFAULT_ACCOUNT'], 
        region=os.environ['CDK_DEFAULT_REGION'])

When initializing Stack set environment:

env=cdk_util.get_cdk_environment() Correct number of subnets are created with this method.

2reactions
yogincommented, Aug 23, 2019

I have the same issue using the Python framework. It only uses 2 AZs, and passing max_azs=3 didn’t help.

I invoke the cdk cli by specifying a config profile --profile xyz. My profile inherits from another profile and uses assume role to access another AWS account. The profile also sets the region to use (which is different from the source profile). I use this profile with the AWS cli and Terraform all the time so I know it’s correctly setup.

When deploying a stack with that profile, it is able to create the stack in the correct account and correct region. However it doesn’t use the expected number of availability zones.

After speaking to @skinny85 on gitter, he suggested passing the env to my stack and explicitly setting the account and region there

So instead of

NetworkStack(app, "Staging")

I have to use

NetworkStack(app, "Staging", env=core.Environment(account="123...", region="us-east-1"))

I think being able to override the environment for individual stack is awesome, so you can manage your infrastructure across multiple accounts and regions, but I expect it to be able to correctly use the default environment provided by my profile.

And it seems like it can do it, for the most part. It can create resources in the correct account and region defined in my profile, but for some reason, it can’t retrieve the full list of availability zones unless the account and region are explicitly passed to the Stack. And this seems like an issue to me. It obviously has the account and region information available from the profile since it is creating resources, so it should be able to determine AZs correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws_cdk.aws_ec2.vpc only utilizes 2 availability zones no ...
vpc only utilizes 2 availability zones no matter what max_azs is set to unless you specify account number in the Stack env attribute...
Read more >
Namespace Amazon.CDK.AWS.EC2
If region and account are not specified, the stack could be deployed anywhere and it will have to make a safe choice, limiting...
Read more >
The New AWS CDK - ADMIN Magazine
By changing the number of availability zones (AZs) that are being used according to environment type (e.g., the "dev" environment will only use...
Read more >
How to become an infrastructure-as-code ninja, using AWS CDK
Our first goal is simply to get an EC2 instance of any type up and running, in any VPC, subnet, availability zone -...
Read more >
awsec2 - Go Packages
The @aws-cdk/aws-ec2 package contains primitives for setting up networking and instances. import ec2 "github.com/aws/aws-cdk-go/awscdk" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found