我想創建一個接受可選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