Skip to content

Getting '${Token[TOKEN.72]} is not valid' error on cdk synth, when assigning ec2.Vpc.cidr value from cfnParameter.valueAsString #3617

Closed
@rajdasmechrock

Description

  • I'm submitting a ...

    • 🪲 bug report
  • What is the current behavior?
    "${Token[TOKEN.72]} is not valid" error is generated when the stack is synthesized ($cdk synth) for the following code snippet:

  1. Create a cdk App with a stack (Language: typescript) i.e. cdk init --language typescript
  2. Add the following to the stack
       // 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 Congfiguration
        const vpc = new ec2.Vpc(this, "vpcName", {
            cidr: vpcCidr.valueAsString,
            maxAzs: 2,
            vpnGateway: true, // VPC can accept VPN connections
            subnetConfiguration: [
                {
                    cidrMask: 19,
                    name: "Private",
                    subnetType: SubnetType.PRIVATE,
                },
                {
                    cidrMask: 20,
                    name: "Public",
                    subnetType: SubnetType.PUBLIC,
                },
                {
                    cidrMask: 21,
                    name: "Protected",
                    subnetType: SubnetType.ISOLATED,
                },
            ],
        });
  1. cdk synth <stack-name> after cdk bootstrap
  2. synth fails with the following error:

${Token[TOKEN.72]} is not valid

  • What is the expected behavior (or behavior of feature suggested)?
    The expected behavior should be similar to passing the cidr value through the parameter. Similar to cidr: "10.0.0.0/16" where the value is set from the CfnParameter

  • What is the motivation / use case for changing the behavior or adding this feature?
    Trying to use the AWS CDK CfnParameter to parameter-ise the cidr value of ec2.Vpc. The intent is to re-use the stack for VPC creation with the CIDR for the VPC as a "plugabble" value.

  • Please tell us about your environment:

    • 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" }
    • OS: [ OSX Mojave ]
    • Language: [ TypeScript ]
  • Other information
    Stackoverflow: https://stackoverflow.com/questions/57425039/getting-tokentoken-72-is-not-valid-error-on-cdk-synth-when-assigning-ec2/57436021#57436021

Response on gitter from someone who has faces this issue as well:

I encountered this the other day as well. When you reference the vpcCidr.valueAsString it doesn't actually return "10.0.0.0/16" it returns "!Ref vpcCidr" which should be the intention for a normal cloudformation template, however as you are using the ec2.Vpc structure which goes of and also generates subnets it needs this value to be an actual cidr to split up into subnets. I think there are two ways around this, one is to use the ec2.CfnVPC structure, but then you need to create all the subnets and related resources yourself, or just not use CfnParameters and pass the parameters within CDK, resulting in a static cloudformation template.
--

Looks like the following function in network-util.js is throwing the error:

/**
     * Converts a string IPv4 to a number
     *
     * takes an IP Address (e.g. 174.66.173.168) and converts 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);
    }

Metadata

Assignees

Labels

@aws-cdk/aws-ec2Related to Amazon Elastic Compute CloudbugThis issue is a bug.good first issueRelated to contributions. See CONTRIBUTING.mdneeds-reproductionThis issue needs reproduction.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions