Azure Classroom Series – 15/Feb/2020

Creating Multiple Resources

Exercise 7: Create 4 subnets by writing one resource of subnet

  • For this we use copy and _copyIndex() and array parameters or variables
  • The template is as shown below
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "0.0.0.1",
    "parameters": {
        "subnetnames": {
            "type": "array",
            "defaultValue": ["web", "app", "db", "management"]
        },
        "networkcidr": {
            "type": "string",
            "defaultValue": "10.10.0.0/16"
        },
        "subnetcidrs": {
            "type": "array",
            "defaultValue": ["10.10.0.0/24", "10.10.1.0/24", "10.10.2.0/24", "10.10.3.0/24"]
        }
         
    },
    "variables": {
        "subnetcount": "[length(parameters('subnetnames'))]"
    },
    "resources": [
        {
            "name": "ntier",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2019-11-01",
            "location":"[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes":[ "[parameters('networkcidr')]" ]
                }
            }
        },
        {
            "name": "[concat('ntier', '/',parameters('subnetnames')[copyIndex()])]",
            "type": "Microsoft.Network/virtualNetworks/subnets",
            "apiVersion": "2019-11-01",
            "properties": {
                "addressPrefix": "[parameters('subnetcidrs')[copyIndex()]]"
            },
            "copy": {
                "name": "subnetcountvar",
                "count": "[variables('subnetcount')]",
                "mode": "Serial"
            },
            "dependsOn": ["ntier"]

        }
        
    ]

}

Exercise 8: Deploy this template using azure cli

  • To Create a deployment from Cli
# Create a resource group
az group create --name armfromcli --location 'Central US'

# Create a deployment with in this resource group

az group deployment create --resource-group armfromcli --template-file main.json

# if you want to change any parameter
az group deployment create --resource-group armfromcli --template-file main.json --parameters vnetname='ntier1'
  • Refer Here for azure resource manager deployment from cli

Exercise 9: Deploy this template using azure powershell

Exercise 10: Create two vnets as shown below by using copy on vnet

Preview

  • Note: Refer Here for git repo maintained by microsoft for ARM templates

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner