Exercise - Deploy and export a Logic Apps workflow using an ARM template

In the preceding exercise, we deployed an app using a basic ARM template. The template was inflexible in that everything was hard-coded. In particular, to change the name of the workflow or the location to which the app was deployed, you would have to edit the template itself. In scenarios where you are doing multiple deployments in a scripted environment, editing the template by hand becomes cumbersome. A better approach is to supply values as parameters to customize resource deployment.

Update our template to use template parameters

  1. Make a copy of the basic template we used in the preceding exercise with the following cp command.

    cp basic-template.json template-with-params.json
    
  2. To edit the template, open it in the built-in editor with the following code command.

    code template-with-params.json
    

    The first thing we'll do is add parameters so we can easily customize the name of our app and the location where it is run.

  3. Replace the parameters section of the template with the following code to add two new parameters, logicAppName and location as shown in the following snippet.

    "parameters": {
        "logicAppName": {
            "type": "string",
            "metadata": {
              "description": "The name of the logic app to create."
            }
          },
        "location": {
        "type": "string",
        "defaultValue": "[resourceGroup().location]",
        "metadata": {
              "description": "Location for all resources."
            }
        }
    },
    

    Both parameters are strings. We don't supply a default value for the logicAppName parameter, which means supply one at deployment time. In contrast, the location is optional since we supply a default valuer.

    The default value for the location parameter is the location of the resource group into which the app is being deployed. We get that value by referencing the location property from the resource group returned by the resourceGroup() template function. Expressions start and end with brackets: [ and ], respectively. The value of the expression is evaluated when the template is deployed. An expression can return a string, integer, boolean, array, or object. The maximum number of parameters you can define in a template is 256.

    Now that we've defined our two new parameters, let's use them in the template by replacing hard-coded values with references to the new parameters.

  4. Replace the name and location fields in the resources section of the template to use our new parameters as shown in the following snippet.

    "name": "[parameters('logicAppName')]",
    "location": "[parameters('location')]",
    
  5. Replace the outputs section at the bottom of the template with the following code. We are updating the value of the logicAppUrl template variable to also use the logicAppName parameter as shown in the following snippet.

    "outputs": {
        "logicAppUrl": {
           "type": "string",
           "value": "[listCallbackURL(concat(resourceId('Microsoft.Logic/workflows/', parameters('logicAppName')), '/triggers/manual'), '2017-07-01').value]"
        }
     }
    
  6. Save all changes to template-with-params.json

Deploy our logic app using the parameterized template

There are two ways to supply parameters to our template during deployment using the--parameters flag in the az deployment group create command. We can pass in a URI of a remote parameters file, or the name of a local file. Let's use a local file.

Create a parameters JSON file

  1. Create a new file called params.json in the built-in code editor with the following command.
    code params.json
    
  2. Paste the following JSON into params.json and save your changes.
    { 
        "logicAppName": { 
            "value": "myLogicApp" 
        } 
    }
    

Validate our template

  1. From the Cloud Shell run az group deployment validate to validate the template.

    az group deployment validate \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --template-file template-with-params.json \
    --parameters @params.json
    

    The --template-file argument points to the local template. The template's filename is template-with-params.json.

    You see a large JSON block as output, which tells you that the template passed validation.

    Azure Resource Manager fills in the template parameters and checks whether the template would successfully run in your subscription.

    If validation failed, you would see a detailed description of the failure in the output.

Deploy template with parameters from a local file

  1. Run the following command in the Cloud Shell to deploy the logic app with the name of the app taken from the params.json file. The location parameter is not set in the params.json file, so the default is used.

    az deployment group create \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --template-file template-with-params.json \
    --parameters @params.json
    

    Deployment will take a few seconds and you can watch the progress in the Cloud Shell command line. When deployment is finished, you should see provisioningState in the JSON result with the value Succeeded.

  2. To see the app in action, find the logicAppUrl value in the JSON result. Select the URL and paste it into a new browser window. The page will display the Hello Logic Apps Template! message.

Deploy template with parameters from the command line

Instead of editing a parameters file every time we want to deploy from the command line, we can supply the parameters in a JSON string on the command line.

  1. Run the following command in the Cloud Shell to deploy the logic app with the name of the app and its location fed in as a JSON string on the command line.

    az deployment group create \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --template-file template-with-params.json \
    --parameters '{ "logicAppName": {"value":"MyLogicApp2"}, "location": {"value":"East US"}}'
    

    Deployment will take a few seconds and you can watch the progress in the Cloud Shell command line. When deployment is finished, you should see provisioningState in the JSON result with the value Succeeded.

  2. To see the app in action, find the logicAppUrl value in the JSON result. Select the URL and paste it into a new browser window. The page will display the Hello Logic Apps Template! message.

  3. Run the following command to list all Logic Apps workflows we've deployed so far.

    az resource list \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --resource-type Microsoft.Logic/workflows \
    --query [*].[name,location] --output tsv
    

    This command will list the three Logic Apps workflows we've deployed so far, all from a template.

Update the app action in the ARM template

Let's now turn our attention to making our app do a little more than just sending back a static message to us. We'll keep the app as an HTTP-triggered workflow and it will still return an HTTP response. But let's pass in some values with the request and have the app do a calculation for us. We'll do a simple area calculation. Assuming the inputs we pass in are height and width of a rectangle, we'll return the area. We'll then deploy the new app and see it in action.

  1. Open template-with-params.json in the built-in editor by running the following command in the Cloud Shell.

    code template-with-params.json
    
  2. Replace the relativePath field to the inputs section of our HTTP request trigger as shown in the following snippet.

    "inputs": {
        "method": "GET",
        "relativePath": "{width}/{height}",
        "schema": {}
    }
    

    The relativePath entry specifies the parameters that we want our HTTP endpoint URL to accept. In this case, we define two parameters, width and height. We'll use the values of these parameters to calculate an area and return the result.

  3. Update the body of the Response action with the following line.

    "body": "Response from @{workflow().name}  Total area = @{triggerBody()}@{mul( int(triggerOutputs()['relativePathParameters']['height'])  , int(triggerOutputs()['relativePathParameters']['width'])  )}",
    

    Our updated response does the following:

    • Prints out the name of the logic app. It makes a call to the workflow() function to return information about the workflow, and from that we reference the name property.
    • It returns the product (mul() function) of the integer equivalents (int() conversion function) of the height and width string values of the URL parameters.
  4. Save all changes to template-with-params.json

  5. Validate our template after these changes with the az group deployment validate command in the Cloud Shell. We set the name of the app in this instance to CalculateArea using an inline parameter.

    az group deployment validate \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --template-file template-with-params.json \
    --parameters '{ "logicAppName": {"value":"CalculateArea"}}'
    
  6. Run the following command to deploy our changes to a logic app named CalculateArea. We will omit a value for the location parameter and just use the default.

    az deployment group create \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --template-file template-with-params.json \
    --parameters '{ "logicAppName": {"value":"CalculateArea"}}'
    

    Deployment will take a few seconds and you can watch the progress in the Cloud Shell command line. When deployment is finished, you should see provisioningState in the JSON result with the value Succeeded.

  7. To see the app in action, find the logicAppUrl value in the JSON result. Select the URL and paste it into a new browser window.

  8. Update the URL in the browser, changing /triggers/manual/paths/invoke?api to /triggers/manual/paths/invoke/{width}/{height}?api, where {width} and {height} are integer values for the width and height of the area we want to calculate. For example, /triggers/manual/paths/invoke/6/7?api. The response from the app will list the name of the workflow and the calculated area, as shown in the following screenshot.

    Web browser displaying response from our app called calculate area.

  9. Run the following command to list all Logic Apps workflows we've deployed so far.

    az resource list \
    --resource-group <rgn>[sandbox resource group name]</rgn> \
    --resource-type Microsoft.Logic/workflows \
    --query [*].[name,location] --output tsv
    

In this exercise, we introduced more flexibility to our template with parameters. We supplied those parameters on the command line and using a local file. We also updated the action that our simple workflow executes, by directly changing the body of the response we send back.

You can download the parameters and template files from GitHub with the following curl commands in the Cloud Shell.

  curl https://github.com/MicrosoftDocs/mslearn-logic-apps-and-arm-templates/blob/master/calculate-area/params.json > params-final.json
  curl https://github.com/MicrosoftDocs/mslearn-logic-apps-and-arm-templates/blob/master/calculate-area/template.json > template-with-params-final.json