Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions in cloud formation resource creation

I'm using a platform condition to control the type of environment that gets spun up on AWS. There are plenty of shared resources, but I need certain EC2 instances with pre-baked AMIs depending on a number conditions.

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

Then I set the conditions.

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

In a resource I'd like to use either linux or windows to trigger a Windows or Linux Ec2 creation, or use both to deploy every ec2 resource declared.

I've tried the following using fn:or in a few ways.

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

and...

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

I keep getting the following error when trying to deploying and validating using the 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.

Is it possible to evaluate multiple conditions to control resource creation? If not are there any alternatives I could try?

like image 256
Joe Gardiner Avatar asked Aug 22 '16 18:08

Joe Gardiner


People also ask

What is conditions in cloud formation?

With conditions, you can define which resources are created and how they're configured for each environment type. Conditions are evaluated based on predefined pseudo parameters or input parameter values that you specify when you create or update a stack.

Which AWS service provides you with the ability to create a template and use it multiple times to deploy the AWS resources?

About AWS CloudFormation AWS CloudFormation streamlines the deployment of key workloads on the AWS Cloud. With AWS CloudFormation, you model and provision all the resources needed for your applications across multiple Regions and accounts in an automated and secure manner.

Which section of CloudFormation does not allow condition?

According to the docs, Conditions should be used at the top level of the resource you want to conditionally create. Putting a Condition inside the Instance UserData section isn't supported. To use Conditions in your situation, you'd want separate Resources conditionally created based on the Parameter.

What happens when one of the resources in a CloudFormation stack Cannot be created successfully?

Q: What happens when one of the resources in a stack cannot be created successfully? By default, the “automatic rollback on error” feature is enabled. This will direct CloudFormation to only create or update all resources in your stack if all individual operations succeed.


1 Answers

I was looking for the same thing with different scenarios in the YAML format. Below is the YAML format for the reference.

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

example

---
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: []
like image 51
mahendra rathod Avatar answered Sep 20 '22 23:09

mahendra rathod