Skip to content

Commit c92e9a9

Browse files
rix0rrrnija-at
authored andcommitted
fix(ec2): throw useful error when using lazy CIDR in VPC (#3739)
We can't use lazy (or deploy-time) CIDRs when creating a VPC, since we need to subdivide the CIDR for all the subnets. Throw a descriptive error message to inform users when they try to do this. Fixes #3617.
1 parent d8fcb50 commit c92e9a9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,10 @@ export class Vpc extends VpcBase {
825825
}
826826

827827
const cidrBlock = ifUndefined(props.cidr, Vpc.DEFAULT_CIDR_RANGE);
828+
if (Token.isUnresolved(cidrBlock)) {
829+
throw new Error(`'cidr' property must be a concrete CIDR string, got a Token (we need to parse it for automatic subdivision)`);
830+
}
831+
828832
this.networkBuilder = new NetworkBuilder(cidrBlock);
829833

830834
const enableDnsHostnames = props.enableDnsHostnames == null ? true : props.enableDnsHostnames;

packages/@aws-cdk/aws-ec2/test/test.vpc.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { countResources, expect, haveResource, haveResourceLike, isSuperObject } from '@aws-cdk/assert';
2-
import { Stack, Tag } from '@aws-cdk/core';
2+
import { Lazy, Stack, Tag } from '@aws-cdk/core';
33
import { Test } from 'nodeunit';
44
import { CfnVPC, DefaultInstanceTenancy, SubnetType, Vpc } from '../lib';
55

@@ -552,6 +552,17 @@ export = {
552552

553553
test.done();
554554
},
555+
556+
'CIDR cannot be a Token'(test: Test) {
557+
const stack = new Stack();
558+
test.throws(() => {
559+
new Vpc(stack, 'Vpc', {
560+
cidr: Lazy.stringValue({ produce: () => 'abc' })
561+
});
562+
}, /property must be a concrete CIDR string/);
563+
564+
test.done();
565+
},
555566
},
556567

557568
"When creating a VPC with a custom CIDR range": {

0 commit comments

Comments
 (0)