Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ec2): descriptive error message when selecting 0 subnets #2025

Merged
merged 4 commits into from
Mar 21, 2019

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 15, 2019

Selecting 0 subnets is invalid for most CFN resources. We move the check
for this upstream to the VPC construct, which can throw a nicely
descriptive error message if it happens to not contain any subnets that
match the type we're looking for.

The alternative is that the error happens during CloudFormation
deployment, but then it's not very clear anymore what the cause for
the error was.

Fixes #2011.


Pull Request Checklist

  • Testing
    • Unit test added (prefer not to modify an existing test, otherwise, it's probably a breaking change)
    • CLI change?: coordinate update of integration tests with team
    • cdk-init template change?: coordinated update of integration tests with team
  • Docs
    • jsdocs: All public APIs documented
    • README: README and/or documentation topic updated
  • Title and Description
    • Change type: title prefixed with fix, feat will appear in changelog
    • Title: use lower-case and doesn't end with a period
    • Breaking?: last paragraph: "BREAKING CHANGE: <describe what changed + link for details>"
    • Issues: Indicate issues fixed via: "Fixes #xxx" or "Closes #xxx"
  • Sensitive Modules (requires 2 PR approvers)
    • IAM Policy Document (in @aws-cdk/aws-iam)
    • EC2 Security Groups and ACLs (in @aws-cdk/aws-ec2)
    • Grant APIs (only if not based on official documentation with a reference)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

Selecting 0 subnets is invalid for most CFN resources. We move the check
for this upstream to the VPC construct, which can throw a nicely
descriptive error message if it happens to not contain any subnets that
match the type we're looking for.

The alternative is that the error happens during CloudFormation
deployment, but then it's not very clear anymore what the cause for
the error was.

Fixes #2011.
@rix0rrr rix0rrr requested review from SoManyHs and a team as code owners March 15, 2019 12:53
packages/@aws-cdk/aws-ec2/lib/vpc-ref.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-ec2/lib/vpc-ref.ts Show resolved Hide resolved

const nets = this.subnets(placement);
if (nets.length === 0) {
throw new Error(`There are no ${describePlacement(placement)} in this VPC. Use a different VPC placement strategy.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe hint at what strategies would have matched something? Or are placement strategies too complex?

Copy link
Contributor Author

@rix0rrr rix0rrr Mar 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not necessarily, but what are you proposing?

"The VPC network contains 1 Public subnet group with name "Public" and 2 Private subnets groups with names "MyDatabases","MyLambdas"

?

Not sure that a longer error message is necessarily going to make things better. We could, of course.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean I wouldn't put the Use a different VPC placement strategy if you can't hint at which are "valid".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used that terminology on purpose because:

  • It's okay to state an error, but it's better to state a path to a solution.
  • I want to use the words "VPC placement" here on purpose, because previously asked questions have shown that people don't think of this term.

Although maybe the better solution is to change the parameter name to subnetSelection, or something, under the "s" where people that already know things about AWS and CFN are going to look for it.

packages/@aws-cdk/aws-ec2/lib/vpc-ref.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-ec2/lib/vpc-ref.ts Show resolved Hide resolved
if (placement.subnetName !== undefined) {
return `subnets named '${placement.subnetName}'`;
}
return '???';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you get there you've effectively reached the default behavior, I believe. Then you should probably return private subnets. If you are willing to assume that the defaults have always been reified before you get here, then why not throw instead of returning ????

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to throw, but at the same time I didn't want to block a customer from using this construct if we ever forget a code path. Rather have ugly behavior but allow the user to continue (so that we can fix the issue when we have capacity) rather than block them completely by throwing an error and now we have to scramble to get a fix out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but then at least avoid the question marks... Maybe JSON.stringify what you've been given.

packages/@aws-cdk/aws-ec2/test/test.vpc.ts Outdated Show resolved Hide resolved
@rix0rrr rix0rrr self-assigned this Mar 18, 2019
Rico Huijbers and others added 2 commits March 18, 2019 18:51
BREAKING CHANGE: `vpcPlacement` has been renamed to `vpcSubnets`
on all objects, `subnetsToUse` has been renamed to `subnetType`.
`natGatewayPlacement` has been renamed to `natGatewaySubnets`.
Copy link
Contributor

@RomainMuller RomainMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VpcNetwork should throw a descriptive error when selecting 0 subnets
3 participants