gpt4 book ai didi

typescript - 从 cfnParameter.valueAsString 分配 ec2.Vpc.cidr 值时,cdk 合成器上出现 '${Token[TOKEN.72]} is not valid' 错误

转载 作者:行者123 更新时间:2023-12-03 07:27:55 25 4
gpt4 key购买 nike

尝试使用 AWS CDK CfnParameter 对 ec2.Vpc 的 cidr 值进行参数化。目的是重复使用堆栈进行 VPC 创建,并将 VPC 的 CIDR 作为“可插入”值。

当以下代码片段合成堆栈 ($cdk Synth ) 时,会生成“${Token[TOKEN.72]} is not valid”错误:

        // 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 block 作为静态字符串传递,它有效:

        // 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"
}
  • 操作系统:OSX Mojave
  • 语言: typescript

最佳答案

不幸的是,似乎 ipToNum 函数需要对 CIDR 进行一些解析和数学运算才能将其转换为数字,因此它必须是静态的(在 synth 中了解) 时间)值。抱歉。

关于typescript - 从 cfnParameter.valueAsString 分配 ec2.Vpc.cidr 值时,cdk 合成器上出现 '${Token[TOKEN.72]} is not valid' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57425039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com