Azure Classroom Series – 03/Jun/2020

Improving the template

  • In the last series we had created the following template
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "0.0.0.1",
    "resources": [
        {
            "name": "armvnet",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-04-01",
            "location": "eastus",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "192.168.0.0/16"
                    ]
                },
                "subnets": [
                    {
                        "name": "subnet1",
                        "properties": {
                            "addressPrefix": "192.168.0.0/24"
                        }
                    },
                    {
                        "name": "subnet2",
                        "properties": {
                            "addressPrefix": "192.168.1.0/24"
                        }
                    },
                    {
                        "name": "subnet3",
                        "properties": {
                            "addressPrefix": "192.168.2.0/24"
                        }
                    },
                    {
                        "name": "subnet4",
                        "properties": {
                            "addressPrefix": "192.168.3.0/24"
                        }
                    },

                    {
                        "name": "subnet5",
                        "properties": {
                            "addressPrefix": "192.168.4.0/24"
                        }
                    },
                    {
                        "name": "subnet6",
                        "properties": {
                            "addressPrefix": "192.168.5.0/24"
                        }
                    }

                ]
            }
        },
        {
            "name": "qtstoragedemoarm",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "tags": {
                "displayName": "qtstoragedemoarm"
            },
            "location": "eastus",
            "kind": "StorageV2",
            "sku": {
                "name": "Standard_RAGRS",
                "tier": "Standard"
            },
            "properties": {
                "accessTier": "Hot"
            }
        }
    ]

}
  • To make this script more generic, we need to parametrize, In arm template we use parameters to take input from users. Refer Here for docs
"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>"
    }
  }
}
  • To use the parameter the syntax is "[parameters(‘<parameter-name>’)]"
  • Adding parametres will change the template as shown below
{
    "$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": "eastus",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('vnetaddress')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[parameters('subnetnames')[0]]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetcidranges')[0]]"
                        }
                    },
                    {
                        "name": "[parameters('subnetnames')[1]]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetcidranges')[1]]"
                        }
                    },
                    {
                        "name": "[parameters('subnetnames')[2]]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetcidranges')[2]]"
                        }
                    },
                    {
                        "name": "[parameters('subnetnames')[3]]",
                        "properties": {
                            "addressPrefix": "[parameters('subnetcidranges')[3]]"
                        }
                    }
                ]
            }
        },
        {
            "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"
            }
        }
    ]

}
  • Deploy this template using portal (can be done from cli & powershell)
  • To make the script more effective lets look at Copy
  • To adopt we need to understand child resource Refer here
{
    "$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"

        },
        "subnetcount": {
            "type": "int",
            "defaultValue": 4
        },
        "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": "[parameters('subnetcount')]",
                "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 this script i have used depends on function

  • Next steps: Lets look at template funtions,varaibles, outputs and improvise the arm template

  • Exercise:

    1. Add network security group to the template
    2. Add public ip address to the template
    3. add network interface to the template

Leave a ReplyCancel reply

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%