Amazon web services 如果在cloudformation脚本的参数中未指定任何安全组,如何创建安全组?

Amazon web services 如果在cloudformation脚本的参数中未指定任何安全组,如何创建安全组?,amazon-web-services,amazon-cloudformation,Amazon Web Services,Amazon Cloudformation,我有一个安全组的参数: "Parameters" : { "SecurityGroup" : { "Description" : "Name of an existing EC2 Security Group ", "Type" : "String", "Default" : "default", "MinLength": "1", "MaxLength": "64", "AllowedPattern" : "[-_ a-zA-Z0-9]*", "C

我有一个安全组的参数:

"Parameters" : {
 "SecurityGroup" : {
   "Description" : "Name of an existing EC2 Security Group ",
   "Type" : "String",
   "Default" : "default", 
   "MinLength": "1",
   "MaxLength": "64",
   "AllowedPattern" : "[-_ a-zA-Z0-9]*",
   "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
  },
},
"Resources": {
  "NewSecurityGroup": {
    "Type" : "AWS::EC2::SecurityGroup",
    "Condition" : "ShouldCreateSecurityGroup" 
    "Properties" : {
       "SecurityGroupEgress" : [ Security Group Rule, ... ],
       "SecurityGroupIngress" : [ Security Group Rule, ... ],
    }
  }
}

但是,如果没有指定参数,我希望创建一个参数,而不是使用默认值。这可能吗?

否-云形成是一种声明性语言,因此不幸的是没有“if/else”

您甚至不能通过创建自己的参数,然后在脚本执行之前对参数部分进行分析和计算时,向参数传递一个“Ref”


你可以用嵌套的云形成脚本来做一些事情,但我还没有充分考虑到这是否可行。所以对我来说,你有一个脚本,它接受一个参数。如果提供了参数,它将直接传递给第二个云结构,但如果没有提供,则执行第一个云结构并返回新创建的安全组的名称,然后将该名称传递给第二个云结构。

否-云结构是一种声明性语言,因此没有“If/else”不幸的是

您甚至不能通过创建自己的参数,然后在脚本执行之前对参数部分进行分析和计算时,向参数传递一个“Ref”


你可以用嵌套的云形成脚本来做一些事情,但我还没有充分考虑到这是否可行。所以对我来说,你有一个脚本,它接受一个参数。如果提供了参数,它将直接传递给第二个云结构,但如果没有提供,则执行第一个云结构并返回新创建的安全组的名称,并将该名称传递给第二个云结构。

这些可能是在提出此问题后添加的,但对于现在遇到这种情况的任何人来说,这都可以通过云信息来完成

所以如果我们从你的参数声明开始

"Parameters" : {
 "SecurityGroup" : {
   "Description" : "Name of an existing EC2 Security Group ",
   "Type" : "String",
   "Default" : "default", 
   "MinLength": "1",
   "MaxLength": "64",
   "AllowedPattern" : "[-_ a-zA-Z0-9]*",
   "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
  },
},
我们可以添加一个
Conditions
声明,其中包含一个条件
ShouldCreateSecurityGroup

"Conditions" : {
  "ShouldCreateSecurityGroup" : {"Fn::Equals" : [{"Ref" : "SecurityGroup"}, "default"]}
},
此条件现在可用于告知CloudFormation是否创建安全组:

"Parameters" : {
 "SecurityGroup" : {
   "Description" : "Name of an existing EC2 Security Group ",
   "Type" : "String",
   "Default" : "default", 
   "MinLength": "1",
   "MaxLength": "64",
   "AllowedPattern" : "[-_ a-zA-Z0-9]*",
   "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
  },
},
"Resources": {
  "NewSecurityGroup": {
    "Type" : "AWS::EC2::SecurityGroup",
    "Condition" : "ShouldCreateSecurityGroup" 
    "Properties" : {
       "SecurityGroupEgress" : [ Security Group Rule, ... ],
       "SecurityGroupIngress" : [ Security Group Rule, ... ],
    }
  }
}
然后,当您在此处引用该值时,可以使用条件函数说明是要使用
SecurityGroup
参数中的值还是
NewSecurityGroup
资源中的值。例如,为了将值传递到EC2实例的
SecurityGroups
参数中,我们可以使用
{“Fn::If}
如下:

"Server": {
  {
    "Type" : "AWS::EC2::Instance",
    "Properties" : {
        ...
      "SecurityGroups" : [ {"Fn::If": ["ShouldCreateSecurityGroup", {"Ref": "NewSecurityGroup"}, {"Ref": "SecurityGroup"}]} ],
    }
  }
}

这些可能是在提出这个问题后添加的,但对于现在遇到这个问题的任何人来说,都可以在云信息中完成

所以如果我们从你的参数声明开始

"Parameters" : {
 "SecurityGroup" : {
   "Description" : "Name of an existing EC2 Security Group ",
   "Type" : "String",
   "Default" : "default", 
   "MinLength": "1",
   "MaxLength": "64",
   "AllowedPattern" : "[-_ a-zA-Z0-9]*",
   "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
  },
},
我们可以添加一个
Conditions
声明,其中包含一个条件
ShouldCreateSecurityGroup

"Conditions" : {
  "ShouldCreateSecurityGroup" : {"Fn::Equals" : [{"Ref" : "SecurityGroup"}, "default"]}
},
此条件现在可用于告知CloudFormation是否创建安全组:

"Parameters" : {
 "SecurityGroup" : {
   "Description" : "Name of an existing EC2 Security Group ",
   "Type" : "String",
   "Default" : "default", 
   "MinLength": "1",
   "MaxLength": "64",
   "AllowedPattern" : "[-_ a-zA-Z0-9]*",
   "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
  },
},
"Resources": {
  "NewSecurityGroup": {
    "Type" : "AWS::EC2::SecurityGroup",
    "Condition" : "ShouldCreateSecurityGroup" 
    "Properties" : {
       "SecurityGroupEgress" : [ Security Group Rule, ... ],
       "SecurityGroupIngress" : [ Security Group Rule, ... ],
    }
  }
}
然后,当您在这里引用值时,您可以使用条件函数来说明是要使用
SecurityGroup
参数中的值还是
NewSecurityGroup
资源中的值。例如,要将值传递到EC2实例的
SecurityGroup
参数中,我们可以使用
{Fn::If}
类似:

"Server": {
  {
    "Type" : "AWS::EC2::Instance",
    "Properties" : {
        ...
      "SecurityGroups" : [ {"Fn::If": ["ShouldCreateSecurityGroup", {"Ref": "NewSecurityGroup"}, {"Ref": "SecurityGroup"}]} ],
    }
  }
}

顺便说一句,截至2013年11月,CFN现在支持条件:顺便说一句,截至2013年11月,CFN现在支持条件: