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

Multiple conditions in cloud formation resource creation

我正在使用平台条件来控制在 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.

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

尝试添加

"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" }]}
    },

我在 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: []

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

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

其中 S3NotificationArn 是类型 CommaDelimitedList 的参数。