【问题标题】:"Value of property CidrIp must be of type String" cloudformation“属性 CidrIp 的值必须是字符串类型” cloudformation
【发布时间】:2015-11-02 19:54:39
【问题描述】:

我的 CFN 模板出现问题。我在参数中定义了一个 CIDR 块,并希望将其用作安全组资源中的CidrIp。

但是,当我运行堆栈时,我收到 Value of property CidrIp must be of type String 错误并且堆栈正在回滚。

这是我最小的失败模板。我想使用VPCCidrBlock 来定义CidrIp。

有趣的是,AWS 的样本模板LAMP_Multi_AZ 做了完全相同的事情。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "A cloud VPC",
  "Metadata": {
  },
  "Resources": {
    "myvpc": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "VPCCidrBlock"
        }
      }
    },
    "SipserverSecurityGroup": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Enable VPC access",
        "VpcId": {
          "Ref": "myvpc"
        },
        "SecurityGroupIngress": [
          { "IpProtocol": "tcp", "FromPort": "22", "ToPort": "22", "CidrIp": { "Ref": "VPCCidrBlock" } },
          { "IpProtocol": "udp", "FromPort": "5060", "ToPort": "5060", "CidrIp": { "Ref:": "VPCCidrBlock" } }
        ]
      }
    }
  },
  "Parameters": {
    "VPCCidrBlock": {
      "Description": "Main CIDR block for the whole VPC",
      "Type": "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default": "10.13.0.0/16",
      "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
    }
  }
}

【问题讨论】:

  • 问题出在"Ref:" 而不是"Ref"。有趣的是它验证正常。

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

我也遇到了类似的问题,我能够指出这个问题。我使用“ref”而不是“Ref”来引用参数。

【讨论】:

    【解决方案2】:

    奇怪的问题。稍微玩了一下你的例子。

    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Description": "A cloud VPC",
      "Metadata": {
      },
      "Resources": {
        "myvpc": {
          "Type": "AWS::EC2::VPC",
          "Properties": {
            "CidrBlock": {
              "Ref": "VPCCidrBlock"
            }
          }
        },
        "SipserverSecurityGroup": {
          "Type": "AWS::EC2::SecurityGroup",
          "Properties": {
            "GroupDescription": "Enable VPC access",
            "VpcId": {
              "Ref": "myvpc"
            },
            "SecurityGroupIngress": [
              { "IpProtocol": "tcp", "FromPort": "22", "ToPort": "22", "CidrIp": {"Ref": "VPCCidrBlock"}},
              { "IpProtocol": "udp", "FromPort": "5060", "ToPort": "5060", "CidrIp": {"Ref": "VPCCidrBlock"}}
            ]
          }
        }
      },
      "Parameters": {
        "VPCCidrBlock": {
          "Description": "Main CIDR block for the whole VPC",
          "Type": "String",
          "MinLength": "9",
          "MaxLength": "18",
          "Default": "10.13.0.0/16",
          "AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
          "ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
        }
      }
    }
    

    为我工作。是否有可能是某些特殊字符/编码存在问题?

    【讨论】:

      【解决方案3】:

      我在另一个 YAML 文件中遇到了同样的问题“属性 CidrIp 的值必须是字符串类型”。事实证明,下面的双引号需要更改为单引号。

      HTTPTargetGroup:
          Type: AWS::ElasticLoadBalancingV2::TargetGroup
          Properties:
            Name: !Sub "${Foo}-${Bar}-TargetGroup" # -> Error
            Name: !Sub '${Foo}-${Bar}-TargetGroup' # -> Good
      

      【讨论】:

        【解决方案4】:

        我们很容易忽略的一件事是 YAML 配置中的数据类型定义。在我的 yaml cloudformation 模板中,我犯了同样的错误。在我的 ECS ElasticLoadBalancingV2 资源配置中指定 VpcId 之前,我输入了一个破折号。这使得 VpcId 属性看起来像一个值列表,而它实际上应该是一个字符串。反过来,我得到 cloudformation 错误“VpcId 应该是一个字符串”。

        不正确的定义:(第 5 行开头的破折号)

        TargetGroup:
          Type: AWS::ElasticLoadBalancingV2::TargetGroup
          Properties:
            VpcId:
              - Fn::ImportValue: !Join ['-', ["somestring", !Ref Environment, 'someregion', 'VPC']]
        

        正确定义:

        TargetGroup:
          Type: AWS::ElasticLoadBalancingV2::TargetGroup
          Properties:
            VpcId: Fn::ImportValue: !Join ['-', ["somestring", !Ref Environment, 'someregion', 'VPC']]
        

        【讨论】:

          猜你喜欢
          • 2021-12-05
          • 2020-03-25
          • 2020-11-06
          • 2020-07-14
          • 2019-04-22
          • 2021-11-23
          • 1970-01-01
          • 2017-04-16
          • 1970-01-01
          相关资源
          最近更新 更多