2017-09-17 98 views
-2

我想创建一个接受可选SSH密钥对作为参数的CloudFormation模板。我想使用AWS::EC2::KeyPair::KeyName类型,因此CloudFormation界面为用户提供了图片中可用键的列表。CloudFormation键入的参数可以为空

enter image description here

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

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" 
      } 
      } 
     ] 
     } 
    } 
    } 
} 

回答

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" 
        } 
       } 
      ] 
     } 
     } 
    } 
} 
+0

好工作:一个建议,如果你使用[Fn :: If ](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions- if)条件函数,可以将reduce设置为1 LaunchConfiguration KeyName:{“Fn :: If”:[“CreateLCWithKeyPair”,{“Ref”:“SSHKey”},{“Ref”:“AWS :: NoValue”}]}' –

+0

编辑您现有的答案比添加新的 –

+0

我得到相同的错误,如果我把钥匙留空。这只适用于我选择一个键并选择不使用它。如果用'“MinLength”:1“'替换'”Default“:”“',那么它至少不会让我开始创建堆栈,直到我选择一个值。所以它可能工作,但我仍然希望有一个更好的方法。 – kichik

1

AWS::EC2::KeyPair::KeyName参数落在下AWS特定参数类型,并按照AWS的文档和建议,模板用户必须指定现有AWS值是在他们的帐户。

无法在您的CloudFormation模板中将SSHKey留空。请参阅CloudFormation Parameter Syntax。根据该文件的AWS Specific Parameter Types部分,你会发现以下内容:


对于AWS特定参数类型,模板用户必须指定现有 AWS值是在他们的帐户。 AWS CloudFormation支持 以下AWS-特定类型


1

如果您的帐户中有少数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密钥的任何时间,但增加的不错下拉类似于你提到的,并具有选择不按照您的要求配置密钥。

+0

这是一个很酷的伎俩,但在我的情况下,我需要它是通用的,因为我将在许多不同的帐户中使用它。 – kichik

+0

酷,那么也许结帐[azhagiri的解决方案](https://stackoverflow.com/a/46271413/1017797),因为它可能会得到你需要的东西 –

相关问题