Understanding Logic Apps Arm Templates

The infrastructure for your application is typically made up of many components – may be a virtual machine, storage account, and virtual network, or a web app, database, databaseserver,and 3rd party services. You do not see these components as separate entities, instead you see them as related and interdependent parts of a single entity.You want to deploy, manage and monitor them as a group. Azure Resource Manager enables you to work with the resources in your solution as a group. You can deploy, update, or delete all the resources for your solution in a single coordinated operation. You use a template for deployment and that template can work for different environments such as testing, staging and production. 
Resource Manager provides security, auditing, and tagging features to help you manage your resources after deployment Azure Resource Manager is the interface for managing and organizing cloud resources. Think of Resource Manager as a way to deploy cloud resources. Azure resource groups enable you to treat sets of related resources as a single unit. Resource Manager is what organizes the resource groups that let you deploy, manage, and delete all of the resources together in a single action. 
Think about the financial models you run for your analysts. To run a model, you might need one or more VMs, a database to store data, and a virtual network to enable connectivity between everything. With Resource Manager, you deploy these assets into the same resource group and manage and monitor them together. When you're done, you can delete all of the resources in a resource group in one operation.


     Resource - A manageable item that is available through Azure. Some common resources area virtual machine, storage account, web app, database  and virtual network  and many more.
     Resource group - A container that holds related resources for an Azure solution. The resource group can  include all the resources for the solution, or only those resources that you want to manages a group. You decide how you want to allocate resources to resource groups based on what makes the most sense for your Organization.
     Resource provider - A service that supplies the resources you can deploy and manage through Resource
     Manager. Each resource provider offers operations for working with the resources that are deployed. Some common resource providers are 
Microsoft. Compute, which supplies the virtual machine resource 
Microsoft. Storage, which supplies the storage account resource and 
Microsoft. Web, which supplies resources related to web apps.
Resource Manager template - A JavaScript Object Notation (JSON) file that defines one or more resources to deploy to a resource group. It also defines the dependencies between the deployed resources. The template can be used to deploy the resources consistently and repeatedly.
Declarative syntax -Syntax that lets you state "Here is what I intend to create" without having to write the sequence of programming commands to create it. The Resource Manager template is an example of declarative syntax. In the file you define the properties for the infrastructure to deploy to Azure.

What are Resource Manager templates? 
    A Resource Manager template precisely defines all the Resource Manager resources in a deployment. You can deploy a Resource Manager template into a resource group as a single operation. A Resource Manager template is a JSON file, making it a form of declarative automation. Declarative automation means that you define what resources you need but not how to create them. Put another way, you define what you need and it is Resource Manager's responsibility to ensure that resources are deployed correctly.
Why use Resource Manager templates?    Using Resource Manager templates will make your deployments faster and more repeatable. For example, you no longer have to create a VM in the portal, wait for it to finish, then create the next VM, and so on. Resource Manager takes care of the entire deployment for you. Here are some other benefits to consider.
Templates improve consistency Resource Manager templates provide a common language for you and others to describe your deployments. Regardless of the tool or SDK used to deploy the template, the structure, format, and expressions inside the template remain the same.
Templates help express complex deployments  Templates enable you to deploy multiple resources in the correct order. For example, you wouldn't want to deploy a virtual machine before creating OS disk or network interface. Resource Manager maps out each resource and its dependent resources and creates dependent resources first. Dependency mapping helps ensure that the deployment is carried out in the correct order.
Templates reduce manual, error-prone tasks   Manually creating and connecting resources can be time consuming, and it's easy to make mistakes along the way. Resource Manager ensures that the deployment happens the same way every time.

How to Create ARM Template file..
  Whenever you create Any Cloud related project in visual studio it will by default add a template.json file . It  can contain the following sections. These sections are expressed using JSON notation. The template has the following sections.
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "",  
   "parameters": {  },
    "variables": {  },    
"functions": [  ],
    "resources": [  ],  
  "outputs": {  }
}

$Schema: Specifies the location of the JSON schema file. The schema file describes the properties that are available within a template. For example, the schema defines resources as one of the valid properties for a template. Don't worry that the date for the schema is 2019-04-01. This schema version is up to date and includes all of the latest features. The schema date hasn't been changed because there have been no breaking changes since its introduction.
content Version: Specifies the version of the template (such as 1.0.0.0). You can provide any value for this element. Use this value to document significant changes in your template. When deploying resources using the template, this value can be used to make sure that the right template is being used.
resources: Contains the resources you want to deploy or update. Currently, it's empty, but you'll add resources later.
Parameters - Provide values during deployment that allow the same template to be used with different environments.
Variables - Define values that are reused in your templates. They can be constructed from parameter values.
User-defined functions - Create customized functions that simplify your template.
Resources - Specify the resources to deploy.  Under definition section paste your logic App json code in order to deploy..
Outputs - Return values from the deployed resources.
Within your template, you can write template expressions that extend the capabilities of JSON. These expressions make use of the functions provided by Resource Manager.

How to deploy...

Sign in to Azure   you can deploy visual studio o powershell command..   
    Using    PowerShell            Connect-AzAccount 
If you have multiple Azure subscriptions, select the subscription you want to use:  
    Set-AzContext [SubscriptionID/SubscriptionName] 
Create resource group      When you deploy a template, you specify a resource group that will contain the resources.  
           New-AzResourceGroup “ -Name myResourceGroup” -Location "Central US" 
Deploy template  Use the resource group you created. Give a name to the deployment so you can easily identify it in the deployment history. For convenience, also create a variable that stores the path to the template file. This variable makes it easier for you to run the deployment commands because you don't have to retype the path every time you deploy.
     $templateFile = "{provide-the-path-to-the-template-file}" 
      New-AzResourceGroupDeployment  “-Name blanktemplate “ -ResourceGroupName myResourceGroup  “-TemplateFile $templateFile                            
     The deployment command returns results. Verify your deployment to portal..


Adding resource to Template..

Place the cursor in the template resources block, type in storage, and select the arm-storage snippet. Adding Resource..
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"Resources":
[{
 "type": "Microsoft.Storage/storageAccounts", 
"apiVersion": "2019-04-01", 
"name": "{provide-unique-name}",
 "location": "eastus",
 "sku": { "name": "Standard_LRS" },
 "kind": "StorageV2",
 "properties": { "supportsHttpsTrafficOnly": true } }
]}

Array of multiple objects because internaly into schema of arm template it is an array type not a single object..
how one object will look like…
"resources":[
             {},
             {},   ]
Each resource in Resources can have..
apiVersion- Required     This is how api knows how to interpret and deploy the actual resource..
type – required                type of virtual machine  e.g  Microsoft.compute/Virtual machine
Name.                                Name of the VM 
location- required            Where this particular resource will be deployed ..EAST US ,WEST US
Tags                                     Categorising of VM.. e.g Account,HR,Admin
Dependson..                       Conditiinal dependency you can define if one resource depends on another
Properties                           Hardware profile,storage profile

How to Add Parameter.
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
      "type": "string",
      "minLength": 3,
      "maxLength": 24
    },
    "storageSKU": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Standard_ZRS",
        "Premium_LRS",
        "Premium_ZRS",
        "Standard_GZRS",
        "Standard_RAGZRS"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[parameters('storageName')]",
      "location": "eastus",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "StorageV2",
      "properties": {
        "supportsHttpsTrafficOnly": true
      }
    }
  ]
}

The storageSKU parameter has a default value. This value is used when a value isn't specified during the deployment. It also has a list of allowed values. These values match the values that are needed to create a storage account. You don't want users of your template to pass in SKUs that don't work.

Use variable

The following example highlights the changes to add a variable to your template that creates a unique storage account name. Copy the whole file and replace your template with its contents.

JSONCopy

{

  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

  "contentVersion": "1.0.0.0",

  "parameters": {

    "storagePrefix": {

      "type": "string",

      "minLength": 3,

      "maxLength": 11

    },

    "storageSKU": {

      "type": "string",

      "defaultValue": "Standard_LRS",

      "allowedValues": [

        "Standard_LRS",

        "Standard_GRS",

        "Standard_RAGRS",

        "Standard_ZRS",

        "Premium_LRS",

        "Premium_ZRS",

        "Standard_GZRS",

        "Standard_RAGZRS"

      ]

    },

    "location": {

      "type": "string",

      "defaultValue": "[resourceGroup().location]"

    }

  },

  "variables": {

    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"

  },

  "resources": [

    {

      "type": "Microsoft.Storage/storageAccounts",

      "apiVersion": "2019-04-01",

      "name": "[variables('uniqueStorageName')]",

      "location": "[parameters('location')]",

      "sku": {

        "name": "[parameters('storageSKU')]"

      },

      "kind": "StorageV2",

      "properties": {

        "supportsHttpsTrafficOnly": true

      }

    }

  ]

}

Notice that it includes a variable named uniqueStorageName. This variable uses four functions to construct a string value.

 

How to Add Function…

{

  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

  "contentVersion": "1.0.0.0",

  "parameters": {

    "storageName": {

      "type": "string",

      "minLength": 3,

      "maxLength": 24

    },

    "storageSKU": {

      "type": "string",

      "defaultValue": "Standard_LRS",

      "allowedValues": [

        "Standard_LRS",

        "Standard_GRS",

        "Standard_RAGRS",

          ]

    },

 "location": { "type": "string", "defaultValue": "[resourceGroup().location]" }

  },

  "resources": [

    {

      "type": "Microsoft.Storage/storageAccounts",

      "apiVersion": "2019-04-01",

      "name": "[parameters('storageName')]",

 "location": "[parameters('location')]",

      "sku": {

        "name": "[parameters('storageSKU')]"

      },

      "kind": "StorageV2",

      "properties": {

        "supportsHttpsTrafficOnly": true

      }

    }

  ]

}

Add outputs

You can use outputs to return values from the template. For example, it might be helpful to get the endpoints for your new storage account.The following example highlights the change to your template to add an output value. Copy the whole file and replace your template with its contents.

{

  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

  "contentVersion": "1.0.0.0",

  "parameters": {

    "storagePrefix": {

      "type": "string",

      "minLength": 3,

      "maxLength": 11

    },

    "storageSKU": {

      "type": "string",

      "defaultValue": "Standard_LRS",

      "allowedValues": [

        "Standard_LRS",

        "Standard_GRS",

        "Standard_RAGRS",

        "Standard_ZRS",

        "Premium_LRS",

        "Premium_ZRS",

        "Standard_GZRS",

        "Standard_RAGZRS"

      ]

    },

    "location": {

      "type": "string",

      "defaultValue": "[resourceGroup().location]"

    }

  },

  "variables": {

    "uniqueStorageName": "[concat(parameters('storagePrefix'), uniqueString(resourceGroup().id))]"

  },

  "resources": [

    {

      "type": "Microsoft.Storage/storageAccounts",

      "apiVersion": "2019-04-01",

      "name": "[variables('uniqueStorageName')]",

      "location": "[parameters('location')]",

      "sku": {

        "name": "[parameters('storageSKU')]"

      },

      "kind": "StorageV2",

      "properties": {

        "supportsHttpsTrafficOnly": true

      }

    }

  ],

  "outputs": {

    "storageEndpoint": {

      "type": "object",

      "value": "[reference(variables('uniqueStorageName')).primaryEndpoints]"

    }

  }

}

There are some important items to note about the output value you added. The type of returned value is set to object, which means it returns a JSON object. It uses the reference function to get the runtime state of the storage account. To get the runtime state of a resource, you pass in the name or ID of a resource. In this case, you use the same variable you used to create the name of the storage account. Finally, it returns the primaryEndpoints property from the storage account

 

Deploy the template

Open the integrated Visual Studio Code terminal using the ctrl + ` key combination and use either the Azure CLI or Azure PowerShell module to deploy the template.

Deploy template

You're ready to deploy the template and look at the returned value.

New-AzResourceGroupDeployment `

  -Name addoutputs `

  -ResourceGroupName myResourceGroup `

  -TemplateFile $templateFile `

  -storagePrefix "store" `

  -storageSKU Standard_LRS

 

 

In the output for the deployment command, you'll see an object similar to the following example only if the output is in JSON format:

{

    "dfs": "https://storeluktbfkpjjrkm.dfs.core.windows.net/",

    "web": "https://storeluktbfkpjjrkm.z19.web.core.windows.net/",

    "blob": "https://storeluktbfkpjjrkm.blob.core.windows.net/",

    "queue": "https://storeluktbfkpjjrkm.queue.core.windows.net/",

    "table": "https://storeluktbfkpjjrkm.table.core.windows.net/",

    "file": "https://storeluktbfkpjjrkm.file.core.windows.net/"

}

 

az deployment group create --resource-group arm-vscode --template-file azuredeploy.json --parameters azuredeploy.parameters.json

 

Clean up resources

When the Azure resources are no longer needed, use the Azure CLI or Azure PowerShell module to delete the  resource group.

az group delete --name arm-vscode

 

 

 

 

 

 

 

 

 

 

 

 

How to Deploy Logic Apps Solution…

When you create Logic Apps project you will get by defaults power shell script and a logic app json file Which will have below sections..

 

Step1: As per recommendation develop your logic App into portal itself first and test it. Save/download json file of logic app..

Step2:  Create a project in visual studio using logic app template then copy that Json file

Under resources expand it you have a place holder this is where you have to place your logic App code.

What is Azure Resource Manager Template..

Azure Resource Manager allows you to provision your applications using a declarative template. In a single template, you can deploy multiple services along with their dependencies. You use the same template to repeatedly deploy your application during every stage of the application life cycle.There are many predefined templates available for use please refers below link as per your requirement you can add into your project…

https://azure.microsoft.com/en-gb/resources/templates/

Create Storage Account and Blob Container

"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

  "contentVersion": "1.0.0.0",

  "parameters": {

    "storageAccountName": {

      "type": "string",

      "metadata": {

        "description": "Specifies the name of the Azure Storage account."

      }

    },

    "containerName": {

      "type": "string",

      "defaultValue": "logs",

      "metadata": {

        "description": "Specifies the name of the blob container."

      }

    },

    "location": {

      "type": "string",

      "defaultValue": "[resourceGroup().location]",

      "metadata": {

        "description": "Specifies the location in which the Azure Storage resources should be deployed."

      }

    }

  },

  "resources": [

    {

      "type": "Microsoft.Storage/storageAccounts",

      "apiVersion": "2019-06-01",

      "name": "[parameters('storageAccountName')]",

      "location": "[parameters('location')]",

      "sku": {

        "name": "Standard_LRS",

        "tier": "Standard"

      },

      "kind": "StorageV2",

      "properties": {

        "accessTier": "Hot"

      },

      "resources": [

        {

          "type": "blobServices/containers",

          "apiVersion": "2019-06-01",

          "name": "[concat('default/', parameters('containerName'))]",

          "dependsOn": [

            "[parameters('storageAccountName')]"

          ]

        }

      ]

    }

  ]

}

 

 


Comments

Post a Comment