【问题标题】:CloudFormation typed parameter that can be emptyCloudFormation 类型化参数,可以为空
【发布时间】:2017-09-19 00:11:47
【问题描述】:

我正在尝试创建一个接受可选 SSH 密钥对作为参数的 CloudFormation 模板。我想使用 AWS::EC2::KeyPair::KeyName 类型,因此 CloudFormation 界面为用户提供了可用键的列表,如图所示。

我遇到的问题是可选部分。如果用户将选择留空,则使用默认值但不认为是有效的。我明白了:

Parameter validation failed: parameter value for parameter name SSHKey does not exist. Rollback requested by user.

有没有办法定义一个可以留空但具有非泛型类型的参数?

这是一个显示问题的示例模板:

{
  "Parameters": {
    "SSHKey": {
      "Type": "AWS::EC2::KeyPair::KeyName",
      "Description": "Leave empty to disable SSH",
      "Default": ""
    }
  },
  "Conditions": {
    "EnableSSH": {
      "Fn::Not": [
        {
          "Fn::Equals": [
            "",
            {
              "Ref": "SSHKey"
            }
          ]
        }
      ]
    }
  },
  "Resources": {
    "LaunchConfig": {
      "Type": "AWS::AutoScaling::LaunchConfiguration",
      "Properties": {
        "ImageId": "ami-9eb4b1e5",
        "InstanceType": "t2.micro",
        "KeyName": {
          "Fn::If": [
            "EnableSSH",
            {
              "Ref": "SSHKey"
            },
            {
              "Ref": "AWS::NoValue"
            }
          ]
        },
        "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/xvda",
            "Ebs": {
              "VolumeSize": "8"
            }
          }
        ]
      }
    }
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    请根据您的情况查找模板。

    {
       "Parameters":{
          "SSHKey":{
             "Type":"AWS::EC2::KeyPair::KeyName",
             "Description":"select the keypair SSH",
             "Default":""
          },
          "KeyPairRequired":{
             "Type":"String",
             "AllowedValues":[
                "yes",
                "no"
             ],
             "Description":"Select yes/no whether to Add key pair to instance or not."
          }
       },
       "Conditions":{
          "CreateLCWithKeyPair":{
             "Fn::Equals":[
                {
                   "Ref":"KeyPairRequired"
                },
                "yes"
             ]
          },
          "CreateLCWithoutKeyPair":{
             "Fn::Equals":[
                {
                   "Ref":"KeyPairRequired"
                },
                "no"
             ]
          }
       },
       "Resources":{
          "LaunchConfigWithKey":{
             "Condition":"CreateLCWithKeyPair",
             "Type":"AWS::AutoScaling::LaunchConfiguration",
             "Properties":{
                "ImageId":"ami-9eb4b1e5",
                "InstanceType":"t2.micro",
                "KeyName":{
                   "Ref":"SSHKey"
                },
                "BlockDeviceMappings":[
                   {
                      "DeviceName":"/dev/xvda",
                      "Ebs":{
                         "VolumeSize":"8"
                      }
                   }
                ]
             }
          },
          "LaunchConfigWithoutKey":{
             "Condition":"CreateLCWithoutKeyPair",
             "Type":"AWS::AutoScaling::LaunchConfiguration",
             "Properties":{
                "ImageId":"ami-9eb4b1e5",
                "InstanceType":"t2.micro",
                "BlockDeviceMappings":[
                   {
                      "DeviceName":"/dev/xvda",
                      "Ebs":{
                         "VolumeSize":"8"
                      }
                   }
                ]
             }
          }
       }
    }
    

    【讨论】:

    • 干得好:一个建议,如果你使用Fn::If条件函数,你可以通过设置KeyName比如KeyName: {"Fn::If": ["CreateLCWithKeyPair", {"Ref": "SSHKey"}, {"Ref": "AWS::NoValue"}]}来将reduce设置为1 LaunchConfiguration
    • 编辑现有答案而不是添加新答案
    • 如果我将密钥留空,我会得到同样的错误。这仅在我同时选择一个键并选择不使用它时才有效。如果用"MinLength": 1" 替换"Default": "",那么它至少不会让我开始创建堆栈,直到我选择一个值。所以它可能会起作用,但我仍然希望有更好的方法。
    • 对,但目前这是 CloudFormation 的一个限制。
    • 对,我可能最终会使用类似的东西,您可以将安全组策略设置为不允许来自任何地方的 SSH。但我仍然对更好的技巧或 AWS 增加对此的支持抱有希望。
    【解决方案2】:

    如果您的帐户中有少量 SSH 密钥,并且您不经常更改它们,您可以做的一件事是使用 Type: String,并在其中包含一个 AllowedValues 属性。例如:

    "Parameters": {
      "SSHKey": {
        "Type": "String",
        "Description": "Leave empty to disable SSH",
        "Default": "",
        "AllowedValues: ["","Project1Beanstalk","Project2Beanstalk"]
      }
    },
    "Conditions": {
      "EnableSSH": {
        "Fn::Not": [
          {
            "Fn::Equals": [
              "",
              {
                "Ref": "SSHKey"
              }
            ]
          }
        ]
      }
    

    这意味着您必须在添加新的 SSH 密钥时更新模板,但添加与您提到的类似的漂亮下拉菜单,并且可以选择不按您的要求配置密钥。

    【讨论】:

    • 这是一个很酷的技巧,但就我而言,我需要它是通用的,因为我将在许多不同的帐户中使用它。
    • 酷,然后也许结帐azhagiri's solution,因为它可能会为您提供所需的内容
    【解决方案3】:

    AWS::EC2::KeyPair::KeyName 参数属于 AWS 特定参数类型,根据 AWS 文档和建议,模板用户必须指定其账户中的现有 AWS 值。

    不能在 CloudFormation 模板中将 SSHKey 留空。请参考CloudFormation Parameter Syntax。在该文档的AWS Specific Parameter Types 部分下,您将找到以下内容:


    对于 AWS 特定的参数类型,模板用户必须指定现有的 账户中的 AWS 值。 AWS CloudFormation 支持 遵循 AWS 特定的类型


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2022-06-10
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多