2017-09-17 102 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),因爲它可能會得到你需要的東西 –

相關問題