Azure IaaC – Part2 (Azure Resource Manager (ARM) Template)

Santosh Gaikwad

Santosh Gaikwad

Connect on LinkedIn      Follow SCI Page

Write to admin@sharecareinspire.com
Santosh Gaikwad

Latest posts by Santosh Gaikwad (see all)

ARM      << Azure IaC – Part 1      >> Related Articles

What is ARM (Azure Resource Manager) Template

Azure Resource Templates are JSON files that capture infrastructure as code. You can capture all the Azure resources your application needs in a single JSON document that you can consistently deploy to different environments. All resources defined in an Azure Resource Template are provisioned within a Resource Group, which is a logical group for managing related Azure resources.

To create and revise templates, you need a JSON editor. Visual Studio Code is a lightweight, open-source, cross-platform code editor. It supports creating and editing Resource Manager Templates through an extension, however, if you have another JSON editor (like Visual Studio), you can use that editor.

ARM Template JSON format

In its simplest structure, a template contains the following elements:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "",
    "parameters": {  },
    "variables": {  },
    "resources": [  ],
    "outputs": {  }
}

 

Element nameRequiredDescription
$schemaYesLocation of the JSON schema file that describes the version of the template language. Use the URL shown in the preceding example.
contentVersionYesVersion of the template (such as 1.0.0.0). You can provide any value for this element. When deploying resources using the template, this value can be used to make sure that the right template is being used.
parametersNoValues that are provided when deployment is executed to customize resource deployment.
variablesNoValues that are used as JSON fragments in the template to simplify template language expressions.
resourcesYesResource types those are deployed or updated in a resource group.
outputsNoValues that are returned after deployment.

Guidelines for creating ARM Template
Limit the size of your template to 1 MB, and each parameter file to 64 KB. The 1-MB limit applies to the final state of the template after it has been expanded with iterative resource definitions, and values for variables and parameters.

Full Syntax of ARM Template
Each element contains properties you can set. The following example contains the full syntax for a template:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "",
    "parameters": { 
        "<parameter-name>" : {
            "type" : "<type-of-parameter-value>",
            "defaultValue": "<default-value-of-parameter>",
            "allowedValues": [ "<array-of-allowed-values>" ],
            "minValue": <minimum-value-for-int>,
            "maxValue": <maximum-value-for-int>,
            "minLength": <minimum-length-for-string-or-array>,
            "maxLength": <maximum-length-for-string-or-array-parameters>,
            "metadata": {
                "description": "<description-of-the parameter>"
            }
        }
    },
    "variables": { 
        "<variable-name>": "<variable-value>",
        "<variable-name>": {
            <variable-complex-type-value>
        }
    },
    "resources": [
      {
          "condition": "<boolean-value-whether-to-deploy>",
          "apiVersion": "<api-version-of-resource>",
          "type": "<resource-provider-namespace/resource-type-name>",
          "name": "<name-of-the-resource>",
          "location": "<location-of-resource>",
          "tags": {
              "<tag-name1>": "<tag-value1>",
              "<tag-name2>": "<tag-value2>"
          },
          "comments": "<your-reference-notes>",
          "copy": {
              "name": "<name-of-copy-loop>",
              "count": "<number-of-iterations>",
              "mode": "<serial-or-parallel>",
              "batchSize": "<number-to-deploy-serially>"
          },
          "dependsOn": [
              "<array-of-related-resource-names>"
          ],
          "properties": {
              "<settings-for-the-resource>",
              "copy": [
                  {
                      "name": ,
                      "count": ,
                      "input": {}
                  }
              ]
          },
          "resources": [
              "<array-of-child-resources>"
          ]
      }
    ],
    "outputs": {
        "<outputName>" : {
            "type" : "<type-of-output-value>",
            "value": "<output-value-expression>"
        }
    }
}

Create your first Azure Resource Manager template

 Microsoft along with community contributors is maintaining repository of quick start ARM templates in github at following location.

https://github.com/Azure/azure-quickstart-templates

Steps to create ARM Template using VS Code tool

1. Open visual studio code and press ctrl+P to search for extensions paste following command to get the extensions and install ARM extension.

ext install msazurermtools.azurerm-vscode-tools

To know more about visual studio code read here

Visual Studio Code
Visual Studio Code

2. Create a file

3. Copy and paste following JSON syntax in file

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {  },
  "variables": {  },
  "resources": [  ],
  "outputs": {  }
}

4. Save this file as azuredeploy.json.

5. Let’s create this template to add storage account
Storage account is a resource, hence we need to add the code to resources section.

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {  },
  "variables": {  },
  "resources": [
    {
      "name": "string",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2016-05-01",
      "sku": {
        "name": "string"
      },
      "kind": "Storage",
      "location": "[resourceGroup().location]",
      "tags": {},
      "properties": {
      }
    }
  ],
  "outputs": {  }
}

6. In the above template I have most of the properties of storage resource in template, but let’s get name and sku values from parameters, so that users will have option to provide the name of storage account. But storage name should be unique in azure, hence there would be chances of failing script. Hence to avoid script failure create unique storage name, lets collect storage name prefix from users and attach unique resource groupID to create unique name.

7. Create parameter storageNamePrefix

"parameters": {
  "storageNamePrefix": {
    "type": "string",
    "maxLength": 11,
    "defaultValue": "storage",
    "metadata": {
      "description": "The value to use for starting the storage account name."
    }
  }
},

8. Construct variable storageName using parameter storageNamePrefix and resourceGroupID

"variables": {
  "storageName": "[concat(parameters('storageNamePrefix'), uniqueString(resourceGroup().id))]"
},

9. Now in the resources section update value of name property with variable as follows.

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

10. Now let’s update sku plan for azure storage account. Let’s plan to get the value from user, hence need to add one more parameter (named storageSKU) in parameters section. As sku values are very much internal to Azure and those have to spelled accurately, hence lets add the list of allowed values and set the default value so that user get help while executing template.

"parameters": {
  "storageNamePrefix": {
    "type": "string",
    "maxLength": 11,
    "defaultValue": "storage",
    "metadata": {
      "description": "The value to use for starting the storage account name."
    }
  },
  "storageSKU": {
    "type": "string",
    "allowedValues": [
      "Standard_LRS",
      "Standard_ZRS",
      "Standard_GRS",
      "Standard_RAGRS",
      "Premium_LRS"
    ],
    "defaultValue": "Standard_LRS",
    "metadata": {
      "description": "The type of replication to use for the storage account."
    }
  }
},

11. Now get back to the resources section and update the sku value with parameter ‘storageSKU’

"sku": {
  "name": "[parameters('storageSKU')]"
},

12. Save file.

Final ARM Template
After completing all the steps, final template looks as follows.

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageNamePrefix": {
      "type": "string",
      "maxLength": 11,
      "defaultValue": "storage",
      "metadata": {
        "description": "The value to use for starting the storage account name."
      }
    },
    "storageSKU": {
      "type": "string",
      "allowedValues": [
        "Standard_LRS",
        "Standard_ZRS",
        "Standard_GRS",
        "Standard_RAGRS",
        "Premium_LRS"
      ],
      "defaultValue": "Standard_LRS",
      "metadata": {
        "description": "The type of replication to use for the storage account."
      }
    }
  },
  "variables": {
    "storageName": "[concat(parameters('storageNamePrefix'), uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "name": "[variables('storageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2016-05-01",
      "sku": {
        "name": "[parameters('storageSKU')]"
      },
      "kind": "Storage",
      "location": "[resourceGroup().location]",
      "tags": {},
      "properties": {
      }
    }
  ],
  "outputs": {  }
}
Final ARM Template
Final ARM Template

Deploy Resources to Azure using ARM Template using PowerShell

1. Execute ARM template with default values

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleGroup -TemplateFile c:\MyTemplates\azuredeploy.json
PowerShell Azure Resource Group Deployment
PowerShell Azure Resource Group Deployment

You can see storage account got created in Azure ARM Portal

Storage Account created in ARM Portal
Storage Account created in ARM Portal

2. Execute ARM template with specific values as parameters

New-AzureRmResourceGroupDeployment -Name ExampleDeployment2 -ResourceGroupName ExampleGroup -TemplateFile c:\MyTemplates\azuredeploy.json -storageNamePrefix "sci" -storageSKU "Standard_GRS"

3. Execute ARM Template with Parameters file
Create Parameters file and save it as azuredeploy.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
     "storageNamePrefix": {
         "value": "scistorage"
     },
     "storageSKU": {
         "value": "Standard_GRS"
     }
  }
}

Execute following command on PowerShell command prompt.

New-AzureRmResourceGroupDeployment -Name ExampleDeployment2 -ResourceGroupName ExampleGroup -TemplateFile c:\MyTemplates\azuredeploy.json –TemplateParameterFile c:\MyTemplates\azuredeploy.parameters.json
PowerShell Azure Resource Group Deployment with parameters file
PowerShell Azure Resource Group Deployment with parameters file

 

ARM     << Azure IaC – Part 1      >> Related Articles


Check Articles From Categories      Health and Parenting      Inspiring Stories      Technology      Microsoft Azure      SharePoint O365

Leave a Reply

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