AWS Cloudformation - How to use If Else conditions

11,465

Define a condition in the conditions sections

Conditions:
  createResource: !Equals [ !Ref 'AWS::Region', 'eu-central-1']

Then on your resources

Resources:
  MyResourcesOnlyCreatedSometime:
    Condition: createResource
    Type: AWS::Blah::Blah
Share:
11,465

Related videos on Youtube

Elnur Mammadov
Author by

Elnur Mammadov

Updated on September 14, 2022

Comments

  • Elnur Mammadov
    Elnur Mammadov over 1 year

    I am new to writing AWS Cloudformation templates. I am trying to use If Else conditions on my CF template. How can I use if else statements with resources?

    If AWS::Region == eu-central-1 ==> create resource , else continue.
    
  • jarmod
    jarmod about 5 years
    Note to readers: you might reasonably interpret !Equals to mean "not equals" but it does not mean that. It's the YAML/CloudFormation way of saying "call the intrinsic function named Equals". If you wanted "not equals" you would have to chain both !Not and !Equals.
  • Elnur Mammadov
    Elnur Mammadov about 5 years
    Thank you for your answer. This works, only the AWS::Region must be a string ( 'AWS::Region' )
  • cementblocks
    cementblocks about 5 years
    Ah yes, that gets me every time. Edited.