Waseem Mir
Waseem Mir

Reputation: 303

If condition in cloud formation

Can you pleas advice if the below IF condition in CF is correct. It is to send the call the SNS topic, if the condition is true then send message to mentioned 3 arn.But I see that he If condition is not correct while using array

SnsTopicArns:
    !If [firstvalue,
      - '{{resolve:ssm:/monitoring/configurationar11:1}}'
      - '{{resolve:ssm:/monitoring/configurationarn2:1}}'
      - '{{resolve:ssm:/monitoring/configurationarn3:1}}',
      - !ImportValue someotheralarm]

Upvotes: 0

Views: 2938

Answers (2)

tyron
tyron

Reputation: 3865

If I understood correctly, your logic is:

  • if firstvalue is true, then send to the 3 ARNs
  • if firstvalue is false, then use the !ImportValue

If this is correct, then you will need to use a mix of AWS::NoValue and your functions, since the "If" is not a block in CloudFormation. It is a function that needs to be applied to each value on the array you are providing. The AWS::NoValue is a way of telling AWS that you actually don't want a value present there: if this gets use in a property, it behaves as if the property was not set; if it is used in an array item, it behaves as if the item didn't exist.

I'd suggest you try this:

SnsTopicArns:
  - !If [ firstvalue, '{{resolve:ssm:/monitoring/configurationarn1:1}}', !Ref AWS::NoValue ]
  - !If [ firstvalue, '{{resolve:ssm:/monitoring/configurationarn2:1}}', !Ref AWS::NoValue ]
  - !If [ firstvalue, '{{resolve:ssm:/monitoring/configurationarn3:1}}', !Ref AWS::NoValue ]
  - !If [ !Not [ firstvalue ], !ImportValue someotheralarm, !Ref AWS::NoValue ]

UPDATE: I was doing some testing and I believe this works too:

SnsTopicArns:
  Fn::If:
    - firstvalue
    - - '{{resolve:ssm:/monitoring/configurationarn1:1}}'
      - '{{resolve:ssm:/monitoring/configurationarn2:1}}'
      - '{{resolve:ssm:/monitoring/configurationarn3:1}}'
    - - !ImportValue someotheralarm

Upvotes: 1

Marcin
Marcin

Reputation: 238169

Sadly, its not correct as the syntax is:

!If [condition_name, value_if_true, value_if_false]

Therefore, you can try the following:

SnsTopicArns:
    !If
      - firstvalue,
      - - '{{resolve:ssm:/monitoring/configurationar11:1}}'
        - '{{resolve:ssm:/monitoring/configurationarn2:1}}'
        - '{{resolve:ssm:/monitoring/configurationarn3:1}}'
      - !ImportValue someotheralarm

Note, the your template can still fail, depending on what is someotheralarm and the remaining context of your template which is not shown in the question.

Upvotes: 0

Related Questions