可以为空的 CloudFormation 类型参数
CloudFormation typed parameter that can be empty
我正在尝试创建一个接受可选 SSH 密钥对作为参数的 CloudFormation 模板。我想使用 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 留空。请参阅该文档的 CloudFormation Parameter Syntax. Under the AWS Specific Parameter Types 部分,您会发现以下内容:
For AWS-specific parameter types, template users must specify existing
AWS values that are in their account. AWS CloudFormation supports the
following AWS-specific types
如果您的帐户中有少量 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 密钥时随时更新模板,但添加与您提到的类似的漂亮下拉列表,并且可以选择不配置密钥请求。
请根据您的情况寻找模板。
{
"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"
}
}
]
}
}
}
}
我正在尝试创建一个接受可选 SSH 密钥对作为参数的 CloudFormation 模板。我想使用 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 留空。请参阅该文档的 CloudFormation Parameter Syntax. Under the AWS Specific Parameter Types 部分,您会发现以下内容:
For AWS-specific parameter types, template users must specify existing AWS values that are in their account. AWS CloudFormation supports the following AWS-specific types
如果您的帐户中有少量 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 密钥时随时更新模板,但添加与您提到的类似的漂亮下拉列表,并且可以选择不配置密钥请求。
请根据您的情况寻找模板。
{
"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"
}
}
]
}
}
}
}