我想创建一个接受可选SSH密钥对作为参数的CloudFormation模板。我想使用AWS::EC2::KeyPair::KeyName
类型,因此CloudFormation界面为用户提供了图片中可用键的列表。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"
}
}
]
}
}
}
}
好工作:一个建议,如果你使用[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”}]}' –
编辑您现有的答案比添加新的 –
我得到相同的错误,如果我把钥匙留空。这只适用于我选择一个键并选择不使用它。如果用'“MinLength”:1“'替换'”Default“:”“',那么它至少不会让我开始创建堆栈,直到我选择一个值。所以它可能工作,但我仍然希望有一个更好的方法。 – kichik