Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation - How to use If Else conditions

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.
like image 526
Elnur Mammadov Avatar asked Feb 18 '19 18:02

Elnur Mammadov


People also ask

What is FN :: if?

Fn::If. Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false .

Which CloudFormation section does not allow for conditions?

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 is FN :: GetAtt in CloudFormation?

The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template. For more information about GetAtt return values for a particular resource, refer to the documentation for that resource in the Resource and property reference.

What is pseudo parameter in AWS CloudFormation?

Pseudo parameters are parameters that are predefined by AWS CloudFormation. You don't declare them in your template. Use them the same way as you would a parameter, as the argument for the Ref function.


1 Answers

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
like image 187
cementblocks Avatar answered Nov 25 '22 16:11

cementblocks