Attempting to utilize the AWS CDK CfnParameter to parameterize the CIDR value of ec2.Vpc. The aim is to make the stack reusable for VPC creation with the CIDR as a customizable value.
An error stating "${Token[TOKEN.72]} is not valid" occurs during synthesis ($cdk synth) of the stack for the following code snippet:
// Parameter
const vpcCidr = new cdk.CfnParameter(this, 'vpcCidr', {
type: 'String',
default: "10.0.0.0/16",
minLength: 10,
maxLength: 18,
allowedPattern: '(\\d{1,3})\.(\\d{1,3})\.(\\d{1,3})\.(\\d{1,3})/(\\d{1,2})'
});
// VPC Configuration
const vpc = new ec2.Vpc(this, "vpcName", {
cidr: vpcCidr.valueAsString,
maxAzs: 2,
vpnGateway: true,
subnetConfiguration: [
{
cidrMask: 19,
name: "Private",
subnetType: SubnetType.PRIVATE,
},
{
cidrMask: 20,
name: "Public",
subnetType: SubnetType.PUBLIC,
},
{
cidrMask: 21,
name: "Protected",
subnetType: SubnetType.ISOLATED,
},
],
});
When passing a static string for the CIDR block, it works fine:
// VPC Configuration
const vpc = new ec2.Vpc(this, "vpcName", {
cidr: "10.0.0.0/16",
maxAzs: 2,
vpnGateway: true,
subnetConfiguration: [
{
cidrMask: 19,
name: "Private",
subnetType: SubnetType.PRIVATE,
},
{
cidrMask: 20,
name: "Public",
subnetType: SubnetType.PUBLIC,
},
{
cidrMask: 21,
name: "Protected",
subnetType: SubnetType.ISOLATED,
},
],
});
Expected behavior: Setting vpcCidr.valueAsString for the CIDR property of ec2.Vpc should work the same as when setting cidr as a direct string value.
Actual error message: ${Token[TOKEN.72]} is not valid. The issue seems to be related to the function in network-util.js:
/**
* Converts a string IPv4 to a number
*
* Takes an IP Address (e.g. 174.66.173.168) and converts it to a number (e.g., 2923605416); currently only supports IPv4
*
* Uses the formula:
* (first octet * 256³) + (second octet * 256²) + (third octet * 256) +
* (fourth octet)
*
* @param {string} the IP address (e.g. 174.66.173.168)
* @returns {number} the integer value of the IP address (e.g., 2923605416)
*/
static ipToNum(ipAddress) {
if (!this.validIp(ipAddress)) {
throw new Error(`${ipAddress} is not valid`);
}
return ipAddress
.split('.')
.reduce((p, c, i) => p + parseInt(c, 10) * 256 ** (3 - i), 0);
}
Environment details:
- AWS CDK CLI Version: 1.3.0
"dependencies": {
"@aws-cdk/assert": "^1.2.0",
"@aws-cdk/aws-ec2": "^1.2.0",
"@aws-cdk/aws-ram": "^1.2.0",
"@aws-cdk/core": "^1.2.0"
}
- Operating System: OSX Mojave
- Programming Language: Typescript