Azure Classroom Series – 06/Jun/2020

ARM Template Functions

  • Functions that can be used in ARM Templates Refer Here
  • You can define your own functions using User defined functions Refer
  • Let use some functions. Now observe the below parameters where we have two parameters defined
"subnetcount": {
            "type": "int",
            "defaultValue": 4
        },
"subnetnames": {
    "type": "array",
    "metadata": {
        "description": "Names of the subnets"
    },
    "defaultValue": [
        "ntier/web",
        "ntier/app",
        "ntier/db",
        "ntier/management"
    ]
},
  • subnetcount is used as a parameter in copy
{
            "name": "[parameters('subnetnames')[copyIndex()]]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetcidranges')[copyIndex()]]"
            },
            "copy": {
                "name": "subnetcopy",
                "count": "[parameters('subnetcount')]",
                "mode": "serial"
            },
            "dependsOn": [
                "[parameters('vnetname')]"
            ]
        },
  • We can eliminate this extra parameter by a very simple function length
  • The specific change is
{
            "name": "[parameters('subnetnames')[copyIndex()]]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetcidranges')[copyIndex()]]"
            },
            "copy": {
                "name": "subnetcopy",
                "count": "[length(parameters('subnetcidranges'))]",
                "mode": "serial"
            },
            "dependsOn": [
                "[parameters('vnetname')]"
            ]
        }
  • With this change, the arm template look like
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "0.0.0.2",
    "parameters": {
        "vnetname": {
            "type": "string",
            "metadata": {
                "description": "This is virtual network name"
            },
            "defaultValue": "ntier"

        },
        "subnetnames": {
            "type": "array",
            "metadata": {
                "description": "Names of the subnets"
            },
            "defaultValue": [
                "ntier/web",
                "ntier/app",
                "ntier/db",
                "ntier/management"
            ]
        },
        "vnetaddress": {
            "type": "string",
            "metadata": {
                "description": "description"
            },
            "defaultValue": "192.168.0.0/16",
            "allowedValues": [
                "192.168.0.0/16",
                "10.0.0.0/16",
                "172.16.0.0/16"
            ]
        },
        "subnetcidranges": {
            "type": "array",
            "metadata": {
                "description": "subnet ranges"
            },
            "defaultValue": [
                "192.168.0.0/24",
                "192.168.1.0/24",
                "192.168.2.0/24",
                "192.168.3.0/24"
            ]
        },
        "storageaccountname": {
            "type": "string",
            "metadata": {
                "description": "storage account name"
            },
            "defaultValue": "qtstoragedemoarm.com"
        }

    },
    "resources": [
        {
            "name": "[parameters('vnetname')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-04-01",
            "location": "eastus",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('vnetaddress')]"
                    ]
                }
            }
        },
        {
            "name": "[parameters('subnetnames')[copyIndex()]]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetcidranges')[copyIndex()]]"
            },
            "copy": {
                "name": "subnetcopy",
                "count": "[length(parameters('subnetcidranges'))]",
                "mode": "serial"
            },
            "dependsOn": [
                "[parameters('vnetname')]"
            ]
        },
        {
            "name": "[parameters('storageaccountname')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "tags": {
                "displayName": "qtstoragedemoarm"
            },
            "location": "eastus",
            "kind": "StorageV2",
            "sku": {
                "name": "Standard_RAGRS",
                "tier": "Standard"
            },
            "properties": {
                "accessTier": "Hot"
            }
        }
    ]

}
  • In some cases we need to refer resource related functions Refer Here
  • Rather than hardcoding location we can use "location": "[resourceGroup().location]"
  • Including all the above changes the template is
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "0.0.0.2",
    "parameters": {
        "vnetname": {
            "type": "string",
            "metadata": {
                "description": "This is virtual network name"
            },
            "defaultValue": "ntier"

        },
        "subnetnames": {
            "type": "array",
            "metadata": {
                "description": "Names of the subnets"
            },
            "defaultValue": [
                "web",
                "app",
                "db",
                "management"
            ]
        },
        "vnetaddress": {
            "type": "string",
            "metadata": {
                "description": "description"
            },
            "defaultValue": "192.168.0.0/16",
            "allowedValues": [
                "192.168.0.0/16",
                "10.0.0.0/16",
                "172.16.0.0/16"
            ]
        },
        "subnetcidranges": {
            "type": "array",
            "metadata": {
                "description": "subnet ranges"
            },
            "defaultValue": [
                "192.168.0.0/24",
                "192.168.1.0/24",
                "192.168.2.0/24",
                "192.168.3.0/24"
            ]
        },
        "storageaccountname": {
            "type": "string",
            "metadata": {
                "description": "storage account name"
            },
            "defaultValue": "qtstoragedemoarm.com"
        }

    },
    "resources": [
        {
            "name": "[parameters('vnetname')]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-04-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('vnetaddress')]"
                    ]
                }
            }
        },
        {
            "name": "[concat(parameters('vnetname'),'/',parameters('subnetnames')[copyIndex()])]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2020-04-01",
            "properties": {
                "addressPrefix": "[parameters('subnetcidranges')[copyIndex()]]"
            },
            "copy": {
                "name": "subnetcopy",
                "count": "[length(parameters('subnetcidranges'))]",
                "mode": "serial"
            },
            "dependsOn": [
                "[parameters('vnetname')]"
            ]
        },
        {
            "name": "[parameters('storageaccountname')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "tags": {
                "displayName": "qtstoragedemoarm"
            },
            "location": "[resourceGroup().location]",
            "kind": "StorageV2",
            "sku": {
                "name": "Standard_RAGRS",
                "tier": "Standard"
            },
            "properties": {
                "accessTier": "Hot"
            }
        }
    ]

}

Add variables to your ARM template

  • Variables are values which are not provided by user executing the ARM template rather they are values which can be calculated with in template Refer Here
  • Parameters are values which user has to provide and cannot be computed in a template and Variables are values which can be computed in a template
[variable('location')]
  • Now lets add vm creation

Other Template Deployments for reusability

  • Nested Templates:
    • With in your template you define some other template Refer Here
  • Linked Templates
    • With in your template you link of the other template Refer Here

Azure Blueprints

  • Deploy applications in azure /any cloud we need to
    • Role assignments (IAM)
    • Follow some policies (Azure Policy)
    • Create infrastructure ( ARM Template)
    • Resource Groups
  • Azure has a service which exactly does this and is called as Azure Blue print
  • Lets create a azure blue print Preview Preview Preview Preview Preview Preview Preview Preview Preview

Azure Data Migration

  • Azure has created a very interesting documentation around database migrations Refer Here

VMWare to Azure

Exercise:

  • Write an ARM Template to Create Virtual Machine Scale Set in Azure Refer Here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin