terraformazure-logic-appsazure-rm-templateterraform-template-file

terraform template used for logicapps - dynamically selection between 2 types of trigger: recurrence and http request


I have a terraform template used to deploy my logic apps. Because there I have many logic apps, I want to set somehow a part of them to use a trigger based by Recurrence and a part of them by HTTP request.

Currently, my all logic apps are using same template which start like below:

{
  "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
   ...
  },
  "triggers": {
    "recurrenceTrigger": {
      "type": "Recurrence",
      "recurrence": {
        "frequency": "Day",
        "interval": 1,
        "schedule": {
          "hours": [
            "${scheduleHour}"
          ],
          "minutes": [
            "${scheduleMinute}"
          ]
        },
        "timeZone": "${scheduleTimezone}"
      }
    }
  },
  "actions": {
    // Define your actions here
  },
  "outputs": {}
}

I am thinking how to modify this template to include a conditional output of a variable and to set it into my logic apps and based on this to select what type of trigger to use?

I was trying somehow like:

{
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      ...
    },
    "triggers": {
        "${use_http_trigger ? "HttpTrigger" : "Recurrence"}": ${
        use_http_trigger ? 
        {
          "type": "Http",
          "inputs": {
            "method": "POST",
            "uri": "http://www.example.com"
          }
        }
        :
        {
          "recurrence": {
            "frequency": "Day",
            "interval": 1,
            "schedule": {
              "hours": [
                "${scheduleHour}"
              ],
              "minutes": [
                "${scheduleMinute}"
              ]
            },
            "timeZone": "${scheduleTimezone}"
          },
          "evaluatedRecurrence": {
            "frequency": "Day",
            "interval": 1,
            "schedule": {
              "hours": [
                "${scheduleHour}"
              ],
              "minutes": [
                "${scheduleMinute}"
              ]
            },
            "timeZone": "${scheduleTimezone}"
          },
          "type": "Recurrence"
        }
      }
    },
    ...(rest of template)
 }

ERROR: Inconsistent conditional result types; The true and false result expressions must have consistent types. The 'true' value includes object attribute "inputs", which is absent in the 'false' value..

What do you see as the solution to my request? instead of the above, to use 2 separate templates? not wished, code duplicated... so on.

*my variable use_http_trigger is a boolean ;


Solution

  • I'm not a Terraform expert, but can't you use string representations of your Http and Recurrence JSON objects and convert them to JSON during your use_http_trigger check, something like this?

    {
      "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
       ...
      },
      "triggers": {
        "${use_http_trigger ? "HttpTrigger" : "Recurrence"}": ${
            use_http_trigger ? jsondecode("<your-JSON-string-goes-here>") : jsondecode("<your-other-JSON-string-goes-here>")
        },
        ...(rest of template)
     }