云形成资源创建的多个条件

Joe*_*ner 9 amazon-ec2 amazon-web-services aws-cloudformation

我正在使用平台条件来控制在AWS上运行的环境类型.有很多共享资源,但我需要某些EC2实例与预先制作的AMI,具体取决于数量条件.

"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"
},
Run Code Online (Sandbox Code Playgroud)

然后我设置了conditions.

"Conditions" : {
  "LinuxPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "linux"]},
  "WindowsPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "windows"]},
  "BothPlatform" : {"Fn::Equals" : [{"Ref" : "Platform"}, "both"]}
},
Run Code Online (Sandbox Code Playgroud)

在资源中,我想使用linux或windows来触发Windows或Linux Ec2创建,或者使用它们来部署所声明的每个ec2资源.

fn:or在几个方面尝试了以下使用方法.

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

和...

"Condition" : {
   "Fn::Or" : [
      {"Condition" : "LinuxPlatform"},
      {"Condition" : "BothPlatform"}
   ]
}
Run Code Online (Sandbox Code Playgroud)

尝试使用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.
Run Code Online (Sandbox Code Playgroud)

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

mah*_*hod 16

我在 YAML 格式的不同场景中寻找相同的东西。以下是供参考的 YAML 格式。

CreateResources: !Or [!Equals [!Ref "Environment", prod], !Equals [!Ref "Environment", dev], !Equals [!Ref "Environment", preprod], !Equals [!Ref "Environment", test]]
Run Code Online (Sandbox Code Playgroud)

例子

---
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: []
Run Code Online (Sandbox Code Playgroud)


Vor*_*Vor 8

尝试添加

"MyCondition": {"Fn::Or": [{"Condition": "LinuxPlatform"}, {"Condition": "BothPlatform" }]}
Run Code Online (Sandbox Code Playgroud)

在你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" }]}
    },
Run Code Online (Sandbox Code Playgroud)

  • @Efren 错误消息中的“条件”似乎表示“资源”下的“条件”,它必须是单个字符串。 (2认同)