Deploy logic app using bicep - Convert JSON to valid Bicep

Viewed 2903

I want to generate a bicepfor building a Logic App. The boilerplate for this would be

resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
  name: 'lapp-${options.suffix}'
  location: options.location
  properties: {
    definition: {
      // here comes the definition
    }
  }
}

My comment shows the point where the definition of the app itself would be placed. If I know take a JSON from an existing logic app (I spared some stuff for brevity):

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {},
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {

                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

you would have to tranform this to something like this:

{
    definition: {
        '$schema': "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#"
        actions: {}
        contentVersion: '1.0.0.0'
        outputs: {}
        parameters: {}
        triggers: {
            'manual': {
                inputs: {

                }
                kind: 'Http'
                type: 'Request'
            }
        }
    }
    parameters: {}
}

That means for instance:

  • remove trailing commas
  • remove quotes on properties
  • single quote some properties like the schema or custom action names
  • escape single quotes when they appear in values which is common in Logic apps

Is there any converter which can transform a JSON structure into a valid bicep? I do not mean bicep decompile because this assumes that you've got an already valid ARM template.

2 Answers

One approach is to keep your definition in a separate file and pass the json as a parameter.

main.bicep:

// Parameters
param location string = resourceGroup().location
param logicAppName string
param logicAppDefinition object

// Basic logic app
resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicAppName
  location: location
  properties: {
    state: 'Enabled'
    definition: logicAppDefinition.definition
    parameters: logicAppDefinition.parameters
  }
}

Then you can deploy your template like that (using az cli and powershell here):

$definitionPath="full/path/of/the/logic/app/definition.json"
az deployment group create `
  --resource-group "resource group name" `
  --template-file "full/path/of/the/main.bicep" `
  --parameters logicAppName="logic app name" `
  --parameters logicAppDefinition=@$definitionPath
  

With this approach you dont have to modify your "infra-as-code" every time you update the logic app.

Adding an another approach. Load the workflow file inside the bicep file using loadTextContent(file.json) and parse it as JSON using json() method and access the definition and parameters straight inside the bicep file, avoiding passing the workflow file as parameter to the CLI

param location string = resourceGroup().location
param logicAppName string
var logicAppDefinition = json(loadTextContent('LogicApp.workflow.json'))


// Basic logic app
resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
  name: logicAppName
  location: location
  properties: {
    state: 'Enabled'
    definition: logicAppDefinition.definition
    parameters: logicAppDefinition.parameters
  }
}
Related