从cfnParameter.valueAsString分配ec2.Vpc.cidr值时,在cdk合成上获取“ $ {Token [TOKEN.72]}无效”错误

时间:2019-08-09 07:09:25

标签: typescript amazon-cloudformation aws-cdk

尝试使用AWS CDK CfnParameter参数设置ec2.Vpc的cidr值。目的是将用于VPC的堆栈重新用于VPC的CIDR,作为“ plugabble”值。

为以下代码段合成堆栈($ cdk synth)时,会生成“ $ {Token [TOKEN.72]}无效”错误:

        // 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,
                },
            ],
        });

我尝试将cidr块作为静态字符串传递,并且可以正常工作:

        // VPC Congfiguration
        const vpc = new ec2.Vpc(this, "vpcName", {
            cidr: "10.0.0.0/16",
            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,
                },
            ],
        });

预期:如上例所示,传递给ec2.Vpc构造的cidr属性的vpcCidr.valueAsString应该与设置cidr:“ cidr ip / netmask”相同。

实际:$ {Token [TOKEN.72]}无效。 看起来network-util.js中的以下函数引发了错误

    /**
     * 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);
    }

环境:

  • AWS CDK CLI版本: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
  • 语言:打字稿

1 个答案:

答案 0 :(得分:0)

不幸的是,似乎<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <nav class="navbar navbar-expand-lg navbar-expand-sm j" id="mainNav"> <div class="col-4" id="mainNav_1"> </div> <div class="col-4" id="mainNav_2"> </div> <div class="col-4" id="mainNav_3"> </div> </nav> <div class="topnav" id="myTopnav"> <a href="#A" class="forMargin">A</a> <a href="#B" class="forMargin">B</a> <a href="#C" class="forMargin">B</a> <div class="dropdown"> <button class="dropbtn" id="dropDown-nav" class="forMargin">D <i class="fa fa-caret-down"></i> </button> <div class="dropdown-content" id="submenu"> <a href="#link1">Link 1</a> <a href="#link2">Link 2</a> <a href="#link3">Link 3</a> </div> </div> <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a> </div> <!-- <div class="jumbotron"> <h1>Bootstrap Tutorial</h1> <p>Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile-first projects on the web.</p> </div> -->函数需要对CIDR进行一些解析和数学运算才能将其转换为数字,因此您必须是静态的(在ipToNum时知道)值。抱歉。