【问题标题】:Multiple conditions in cloud formation resource creation云形成资源创建中的多种条件
【发布时间】:2016-12-29 09:47:29
【问题描述】:

我正在使用平台条件来控制在 AWS 上启动的环境类型。有很多共享资源,但我需要某些带有预烘焙 AMI 的 EC2 实例,具体取决于数量条件。

"Parameters": {
"Platform": {
  "Description": "Select platform type - linux or windows",
  "Default": "linux",
  "Type": "String",
  "AllowedValues": [ "linux", "windows", "both" ],
  "ConstraintDescription": "Must enter either linux, windows, or both"
},

然后我设置conditions

"Conditions" : {
  "LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
  "WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
  "BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]}
},

在资源中,我想使用 linux 或 windows 来触发 Windows 或 Linux Ec2 创建,或同时使用这两者来部署声明的每个 ec2 资源。

我使用fn:or 尝试了以下几种方法。

"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }],

还有……

"Condition" : {
   "Fn::Or" : [
      {"Condition" : "LinuxPlatform"},
      {"Condition" : "BothPlatform"}
   ]
}

尝试使用 aws cli 进行部署和验证时,我不断收到以下错误。

aws cloudformation validate-template --template-body       file://./cloudformation/deploy.json

A client error (ValidationError) occurred when calling the ValidateTemplate operation: Template format error: Every Condition member must be a string.

是否可以评估多个条件来控制资源创建?如果没有,我可以尝试其他选择吗?

【问题讨论】:

  • 我在使用 Fn::And 时遇到了同样的问题 - 只想添加此评论,以便下一个使用谷歌搜索“Fn::And”的人会发现这个页面比我更容易:)

标签: amazon-web-services amazon-ec2 amazon-cloudformation


【解决方案1】:

如果您想为 CloudFormation 模板中的参数传递多个值并应用接受值列表的 Fn::Equals:,那么您的代码将如下所示:

S3NotificationProvided: !Not [!Equals [!Join ['', !Ref S3NotificationArn], '']]

其中S3NotificationArnCommaDelimitedList 类型的参数。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

我在 YAML 格式中寻找具有不同场景的相同内容。 以下是 YAML 格式供参考。

CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]

例子

---
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS cloudformation template bucket. '
Parameters:
  Environment:
    Description: Enter the environmet name from allowed values
    Type: String
    AllowedValues:
      - qa
      - dev
      - prod
      - stage
Conditions:
    Prod: !Equals [ !Ref Environment, production]
    dev: !Equals [ !Ref Environment, dev]
    stage: !Equals [ !Ref Environment, stage]
    qa: !Equals [ !Ref Environment, qa]
    CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]

Resources:
  RenderEngineEFSSG:
    Type: AWS::EC2::SecurityGroup
    Condition: CreateResources
    Properties:
      GroupDescription:  test SG. 
      GroupName: !Join [ "-", [ !Ref Environment, sgname ] ]
      VpcId: vpc-0e4d5cad992b8d65b
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 2049
          ToPort: 2049
          CidrIp: 0.0.0.0/0
          Description: Ingress Rule for Lambda to access EFS.
      SecurityGroupEgress: []

【讨论】:

    【解决方案3】:

    尝试添加

    "MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}
    

    到你的Conditions 底部这样:

        "Conditions" : {
            "LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
            "WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
            "BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]},
            "MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}
        },
    

    【讨论】:

    • 谢谢,我会尽快尝试。结构是,如果我在堆栈创建期间从下拉列表中选择窗口,它将在模板中创建 50% 的 EC2 资源。如果我选择 linux 它是另外 50%。我希望能够选择两者来部署所有实例,主要用于开发和测试目的。使用上面的方法可以让我这样做,还是需要将窗口添加到 fn::or?
    • 提议的添加内容与问题中的添加内容有何不同?
    • @Efren 错误信息中的“Condition”似乎是指“Resource”下的“Condition”,必须是SINGLE字符串。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2020-01-11
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多