Amazon web services CloudFormation类型的参数可以为空

Amazon web services CloudFormation类型的参数可以为空,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,我正在尝试创建一个CloudFormation模板,该模板接受可选的SSH密钥对作为参数。我想使用AWS::EC2::KeyPair::KeyName类型,以便CloudFormation界面为用户提供如图所示的可用密钥列表 我遇到的问题是可选部分。如果用户将选择保留为空,则使用默认值,但认为该值无效。我得到: Parameter validation failed: parameter value for parameter name SSHKey does not exist. Rollb

我正在尝试创建一个CloudFormation模板,该模板接受可选的SSH密钥对作为参数。我想使用
AWS::EC2::KeyPair::KeyName
类型,以便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"
            }
          }
        ]
      }
    }
  }
}

AWS::EC2::KeyPair::KeyName
参数属于AWS特定的参数类型,根据AWS文档和建议,模板用户必须指定其帐户中的现有AWS值

在CloudFormation模板中不可能将SSHKey保留为空。请参阅。在该文件的章节下,您将看到以下内容:


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



AWS::EC2::KeyPair::KeyName参数属于AWS特定的参数类型,根据AWS文档和建议,模板用户必须指定其帐户中的现有AWS值

在CloudFormation模板中不可能将SSHKey保留为空。请参阅。在该文件的章节下,您将看到以下内容:


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



如果您的帐户中有少量SSH密钥,并且您不经常更改它们,那么可以使用
类型: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密钥时随时更新模板,但添加与您提到的类似的nice下拉列表,并且可以选择不按照您的请求配置密钥。

如果您的帐户中有少量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密钥时随时更新模板,但添加与您提到的类似的nice下拉列表,并且可以选择不按要求配置密钥。

请根据您的情况查找模板

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

请根据您的情况查找模板

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

很好的工作:一个建议,如果您使用条件函数,您可以通过设置KeyName,如
KeyName:{“Fn::if”:[“createlWithKeypair”,{“Ref”:“SSHKey”},{“Ref”:“AWS::NoValue”}]}
编辑现有答案而不是添加新答案。如果保留该键为空,则会出现相同的错误。仅当我同时选择一个键并选择不使用它时,此选项才有效。如果将
“默认值”:“
替换为
“最小长度”:1“
那么至少在我选择一个值之前,它不会让我开始创建堆栈。所以它可能会起作用,但我仍然希望有更好的方法。对,但这是目前CloudFormation中的一个限制。对,我可能会使用类似的方法,您可以将安全组策略设置为不允许来自任何地方的SSH。但是我仍然对更好的技巧抱有希望,或者AWS增加了对它的支持。很好的工作:一个建议,如果您使用条件函数,您可以通过设置KeyName,比如
KeyName:{“Fn::if”:[“createlWithKeyPair”,“Ref”:“SSHKey”},{“Ref”:“AWS::NoValue”}来将reduce设置为1启动配置
编辑现有答案而不是添加新答案如果我将密钥保留为空,则会出现相同的错误。仅当我同时选择一个键并选择不使用它时,此选项才有效。如果将
“默认值”:“
替换为
“最小长度”:1“
那么至少在我选择一个值之前,它不会让我开始创建堆栈。所以它可能会起作用,但我仍然希望有更好的方法。对,但这是目前CloudFormation中的一个限制。对,我可能会使用类似的方法,您可以将安全组策略设置为不允许来自任何地方的SSH。但我仍然希望有更好的技巧或AWS增加对它的支持。这是一个很酷的技巧,但在我的情况下,我需要它是通用的,因为我将在许多不同的帐户中使用它。很酷,然后可能结帐,因为它可能会得到你需要的东西这是一个很酷的技巧,但在我的情况下,我需要它是通用的,因为我将在许多不同的帐户中使用它。酷,然后可能结帐,因为它可能会得到你需要的