CloudFormation 类型参数可以为空

kic*_*hik 3 amazon-web-services aws-cloudformation

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

在此输入图像描述

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

Parameter validation failed: parameter value for parameter name SSHKey does not exist. Rollback requested by user.
Run Code Online (Sandbox Code Playgroud)

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

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

{
  "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"
            }
          }
        ]
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

{
   "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"
                  }
               }
            ]
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)