Deploy Logic App using Powershell and ARM

To deploy a logic app via Powershell, I used the following approach;
Create a directory structure with two folders, i.e.:

Folder AAA-DeployResources contains reusable artefacts like a parameter file and a Powershell module with reusable code. In this case the reusable code uploads files for a Web App or API App to a FTP folder. I will come back to that later.The parameters file is named ParametersTst.ps1. That’s because I want to deploy a Logic App to a test environment. The parameters file can also be used for AppSettings. AppSettings are relevant for Web Apps and API Apps. I will come back to that later.
The folder ProcessPlanning contains artefacts related to Logic App ProcessPlanning. The artefacts are:DeployProcessPlanning.cmdDeployProcessPlanning.ps1ProcessPlanning.jsonProcessPlanningTst.parameters.json
DeployProcessPlanning.cmd has just one purpose: call DeployProcessPlanning.ps1 passing a parameter for the environment specific parameter file. Note that we can pass a different parameter file for different environments like Acc and Prod.

@Echo Off

Echo Changing PowerShell Execution policy…

powershell Get-Execution

Policypowershell Set-ExecutionPolicy Unrestricted
powershell %~dpn0.ps1 -paramFileName “ParametersTst.ps1”

DeployProcessPlanning.ps1 is the Powershell script that kicks off the Logic App deployment using both ProcessPlanning.json and ProcessPlanningTst.parameters.json. As we will see later ProcessPlanningTst.parameters.json – as the name suggests – is used for parameterization of the json file. In this case we renamed the file to ProessPlanningTst.parameters.json (note the Tst prefix), because this file performs paramterization of the Test version of the Logic App.
To use DeployProcessPlanning.ps1, we first have to add a parameter for the name of the Logic App to ParametersTst.ps1 in folder AAA-DeployResources.
# ProcessPlanning
$nameProcessPlanning = ‘TstProcessPlanning’

Now we can edit the contents of DeployProcessPlanning.ps1:

param (
[parameter(Mandatory = $true)][string] $paramFileName

Import-Module AzureRM.Resources

Write-Host “Login to Azure” –fore gray;

# $PSScriptRoot is null when you run from Powershell ISE
# Run from cmd file
$baseDir = $PSScriptRoot
$pos = $basedir.LastIndexOf(‘\’)
$baseDirParam = $baseDir.Substring(0,$pos)
$paramFile = $baseDirParam + “\AAA-DeployResources\” + $paramFileName

#region Load Parameter file
. $paramFile

$logicAppTemplate = $baseDir + ‘\ProcessPlanning’ + $environment + ‘.json’
$logicAppParameter = $baseDir + ‘\ProcessPlanning’ + $environment + ‘.parameters.json’

Write-Host “BaseDir: ” $baseDir –fore gray;
Write-Host “LogicAppTemplate: ” $logicAppTemplate –fore gray;
Write-Host “LogicAppParameter: ” $logicAppParameter –fore gray;
Write-Host “ParameterFile: ” $paramFile -fore gray;

# New-AzureRmResourceGroup -Name ahak-appservices-tst -Location “westeurope”
Write-Host $(Get-Date).ToString(“yyyyMMdd_HHmss”) ” Add logic app ” $nameProcessPlanning ” to Azure” –fore gray;
New-AzureRmResourceGroupDeployment -Name ‘DeployPlanningTst’ -ResourceGroupName $resourcegroup -TemplateFile $logicAppTemplate -TemplateParameterFile $logicAppParameter

# Check status of last command, i.e. deployment to Azure
if ($?)
Write-Host $(Get-Date).ToString(“yyyyMMdd_HHmss”) ” Add logic app ” $nameProcessPlanning ” to Azure succeeded” –fore green
Write-Host $(Get-Date).ToString(“yyyyMMdd_HHmss”) ” Add logic app ” $nameProcessPlanning ” to Azure failed” –fore red

# List Swagger
#Invoke-AzureRmResourceAction -ResourceGroupName ahak-appservices-tst -ResourceType Microsoft.Logic/workflows -ResourceName TstProcessAGPBeoordeling -Action listSwagger -ApiVersion 2016-06-01 -Force
# GET DevProcessAGPBeoordeling
Get-AzureRmResource -ResourceGroupName $resourcegroup -ResourceType Microsoft.Logic/workflows -ResourceName $nameProcessPlanning -ApiVersion 2016-06-01

There are a few things to note:

  • First you see how the parameter file can be passed to the ps1 script.
  • Next you see the LogicAppTemplate and the LogicAppParameter file is specified. Both the template file and the parameter file have a variable $environment which makes the parameter file specific per environment. If we pass parameter ParametersPrd.ps1 to theps1 file. We can add a file ParamterPrd.ps1 to folder AAA-DeployResources and files ProcessPlanningPrd.json and ProcessPlanningPrd.parameters.json to folder ProcessPlanning.
  • Next you see, I added parameter $nameProcessPlanning at multiple places.
  • Finally I renamed the deployment to DeployProcessPlanning in command New-AzureRmResourceGroupDeployment.

Now, let’s turn our attention to ProcessPlanning.json and ProcessPlanningTst.parameters.json. Both files are copied from the original ResourceGroup project containing the Logic App.In the json file we see there are some environment-specific settings:
Custom Api App: “”
Custom Api App: “{triggerBody()[‘Id’]}”
Reusable Workflow: “/subscriptions/1ea9735d-00df-4375-86d5-d0d35362dd7f/resourceGroups/ahak-appservices-dev/providers/Microsoft.Logic/workflows/DevUploadToSharePoint”

It’s obvious that you want one version of the Logic App.json file, but – depending on the environment – this Logic App has to call different Api Apps (Dev/Tst/Prd) and different child workflows. To make this possible we will have to definea few parameters at the top of the json file in section parameters:

“ahakStorageConnectorName”: {
“type”: “string”,
“metadata”: {        “description”: “Name of  the AHakStorageConnector”      }
“dspConnectorName”: {
“type”: “string”,
“metadata”: {        “description”: “Name of  the DSPConnector”      }
“logicAppUploadToSharepoint”: {
“type”: “string”,
“metadata”: {        “description”: “LogicApp UploadToSharepoint”      }
“rg”: {
“type”: “string”,
“metadata”: {        “description”: “Resourcegroup”      }

Note that we have to enter the definitions of the custom parameters twice. Look for the Parameter section between the sections Resources and Triggers. If we don’t enter the parameter definitions here, we will receive an error like: ‘The only declared parameters for this definition are ‘$connections’ (i.e. there are no custom parameters).

Also note that we added a variable rg to the variables section. I can’t tell why exactly this is necessary, but otherwise it won’t work.

{ “rg”: “[parameters(‘rg’)]”

The values for these parameters we have to add the environment specific parameters file, in this case: ProcessPlanningTst.parameters.json

“$schema”: “”,
“contentVersion”: “”,
“logicAppName”: {      “value”: “TstProcessPlanning”    },
“logicAppLocation”: {      “value”: “westeurope”    },
“ahakStorageConnectorName”: { “value”: “tstahakstorageconnector”      },
“dspConnectorName”: { “value”: “tstdspconnector”      },
“rg”: { “value”: “ahak-appservices-tst”
“logicAppUploadToSharepoint”: { “value”: “TstUploadToSharePoint”
}  }}

The final step, is to use the parameters in the json file:
Custom Api App: “https://@{encodeURIComponent(parameters(‘dspConnectorName’))}”
Custom Api App: “https://@{encodeURIComponent(parameters(‘ahakStorageConnectorName’))}{triggerBody()[‘Id’]}”
Reusable Workflow: “[concat(‘/subscriptions/1ea9735d-00df-4375-86d5-d0d35362dd7f/resourceGroups/’, variables(‘rg’), ‘/providers/Microsoft.Logic/workflows/’, parameters(‘logicAppUploadToSharepoint’))]”

We are almost done now. Go to directory ProcessPlanning, right-click DeployProcessPlanning.cmd and Run As Administrator. When things go well, the Logic App will be deployed.

Useful link Microsoft Azure site: ARM template for Logic App

Leave a Reply

Your email address will not be published. Required fields are marked *