CloudFormation类型的参数,可以为空 [英] CloudFormation typed parameter that can be empty
问题描述
我正在尝试创建一个CloudFormation模板,该模板接受可选的SSH密钥对作为参数。我想使用 AWS :: EC2 :: KeyPair :: KeyName
类型,以便CloudFormation界面为用户提供可用密钥列表,如图片中所示。
I am trying to create a CloudFormation template that accepts an optional SSH key pair as a parameter. I want to use the AWS::EC2::KeyPair::KeyName
type so the CloudFormation interface gives the user a list of available keys like in the picture.
我遇到的问题是可选部分。如果用户将选择保留为空,则使用默认值,但默认值无效。我得到:
The problem I'm having is with the optional part. If the user leaves the selection empty, the default value is used but is not considered valid. I get:
Parameter validation failed: parameter value for parameter name SSHKey does not exist. Rollback requested by user.
是否可以定义可以保留为空但具有非泛型类型的参数?
Is there a way to define a parameter that can be left empty but has a non-generic type?
以下是显示问题的示例模板:
Here's a sample template that shows the problem:
{
"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"
}
}
]
}
}
}
}
推荐答案
请根据您的情况查找模板。
Kindly find the template based on your condition.
{
"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"
}
}
]
}
}
}
}
这篇关于CloudFormation类型的参数,可以为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!