Repository: udondan/cdk-ssm-document (language: python)

Issue:
Problem with BooleanEquals
Similar issue to #14, this is valid JSON for a choice step assuming that `DoSomething` was defined as Boolean input parameter:

```json
             {
                "name": "DoSomethingCheck",
                "action": "aws:branch",
                "inputs": {
                   "Choices": [
                      {
                         "NextStep": "DoSomething",
                         "Variable": "{{DoSomething}}",
                         "BooleanEquals": true
                      },
                      {
                         "NextStep": "DoSomethingElse",
                         "Variable": "{{DoSomething}}",
                         "BooleanEquals": false
                      }
                   ]
                }
             },
```

This fails for the same reason: "Failed to update resource. Input true is of type String, but expected type is Boolean."


Discussion / hints:
Hi Charles,

does this actually work when you define the doc in the console? After I fixed #14 I was able to create documents with boolean parameters, but was unable to execute them. When executing, it complained that Boolean is an invalid parameter type... I contacted the AWS support and their response was quite interesting:

> We stated it in our documentation that you can use Boolean type but have not seen it work

So as I see it, fixing this will only allow you to create a document which cannot be executed.

AWS support suggested to only use String parameters and limit the possible values to "true" and "false".

Daniel


Ah wait, `aws:branch`... that's an Automation document. I haven't tried that. Above experience is related to Command documents. Maybe boolean types are supported in Automation documents.

Fix patch:
diff --git a/lambda/index.ts b/lambda/index.ts
index 988341c..938beb5 100644
--- a/lambda/index.ts
+++ b/lambda/index.ts
@@ -11,6 +11,15 @@ interface DocumentEvent extends LambdaEvent {
           default?: any;
         };
       };
+      mainSteps?: [
+        {
+          name: string;
+          action: string;
+          inputs: {
+            [key: string]: any;
+          };
+        }
+      ];
     };
   };
 }
@@ -291,16 +300,36 @@ function missingTags(newTags: AWS.SSM.TagList) {
  * @returns DocumentEvent
  */
 function fixBooleanParameters(event: DocumentEvent) {
-  if (typeof event.ResourceProperties.Content.parameters !== 'object') {
-    return event;
-  }
-  for (let [key, param] of Object.entries(
-    event.ResourceProperties.Content.parameters
-  )) {
-    if (param.type == 'Boolean' && 'default' in param) {
-      event.ResourceProperties.Content.parameters[key].default =
-        param.default == 'true';
+  // fixing Boolean input parameters
+  if (typeof event.ResourceProperties.Content.parameters == 'object') {
+    for (let [key, param] of Object.entries(
+      event.ResourceProperties.Content.parameters
+    )) {
+      if (param.type == 'Boolean' && 'default' in param) {
+        event.ResourceProperties.Content.parameters[key].default =
+          param.default == 'true';
+      }
     }
   }
+
+  // fixing BooleanEquals in aws:branch https://docs.aws.amazon.com/systems-manager/latest/userguide/automation-action-branch.html
+  if (Array.isArray(event.ResourceProperties.Content.mainSteps)) {
+    event.ResourceProperties.Content.mainSteps.forEach((step, i) => {
+      if (
+        step.action == 'aws:branch' &&
+        'Choices' in step.inputs &&
+        Array.isArray(step.inputs.Choices)
+      ) {
+        step.inputs.Choices.forEach((choice, j) => {
+          if ('BooleanEquals' in choice) {
+            event.ResourceProperties.Content.mainSteps[i].inputs.Choices[
+              j
+            ].BooleanEquals = choice.BooleanEquals == 'true';
+          }
+        });
+      }
+    });
+  }
+
   return event;
 }


Test patch:
diff --git a/test/documents/automation/automation-document.yml b/test/documents/automation/automation-document.yml
index 8fad38b..5963f5e 100644
--- a/test/documents/automation/automation-document.yml
+++ b/test/documents/automation/automation-document.yml
@@ -1,8 +1,25 @@
 ---
 description: Custom Automation Backup and Recovery Sample
 schemaVersion: "0.3"
+parameters:
+  doSomething:
+    type: Boolean
+    description: Do something
+    default: true
 mainSteps:
-  - name: createImage
+  - name: DoSomethingCheck
+    action: aws:branch
+    inputs:
+      Choices:
+        - NextStep: createImage1
+          Variable: "{{ doSomething }}"
+          BooleanEquals: true
+
+        - NextStep: createImage2
+          Variable: "{{ doSomething }}"
+          BooleanEquals: false
+
+  - name: createImage1
     action: aws:executeAwsApi
     onFailure: Abort
     inputs:
@@ -15,3 +32,17 @@ mainSteps:
       - Name: newImageId
         Selector: "$.ImageId"
         Type: String
+
+  - name: createImage2
+    action: aws:executeAwsApi
+    onFailure: Abort
+    inputs:
+      Service: ec2
+      Api: CreateImage
+      InstanceId: i-0987654321
+      Name: Image
+      NoReboot: false
+    outputs:
+      - Name: newImageId
+        Selector: "$.ImageId"
+        Type: String