Edgar Sanchez
Jun 28, 2023

--

AWS: Multiple If/Else Statements in CloudFormation/SAM Template

The other day I needed to build a complex IF / ELSE block in CloudFormation/SAM template. Finding a working example was hard so here’s what I created.

This was in the context of creating a Lambda function that was triggered/executed by a CloudWatch Rule cron job for which I wanted to have an Event type “Schedule” with a JSON input.

Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: MyLambdaName_other_lambda_properties_are_ommitted
Events:
MyCloudWatchEventRuleToTriggerMyLambda
Type: Schedule
Properties:
Schedule: "rate(1 hour)"
Name: MyRuleScheduleName
Description: Below is the IF, ELSE block with JSON example
Enabled: True
Input: !If
- CreateDevResources
- >-
{
"actionRequired": "TestData",
"thisManyMinutesApart": "10"
}
- !If
- CreateTestResources
- >-
{
"actionRequired": " TestData ",
"thisManyMinutesApart": "20"
}
- !If
- CreateProdResources
- >-
{
"actionRequired": " TestData ",
"thisManyMinutesApart": "30"
}
- !Ref AWS::NoValue

--

--

Edgar Sanchez

A great architect - humble in spirit. dangerous with production access.