Chethan S.
Chethan S.

Reputation: 556

Subnet address range determination in Azure VNET

I have a requirement wherein the subnet address range should be determined based on the existing one in the VNet. Typically, we have a subnet CIDR defined as 192.1.0.0/24. A new subnet to be added should get 192.1.1.0/24, 192.1.2.0/24 and so on. The Azure Portal takes care of this automatically when adding new subnets, which it does based on the defined VNet address space i.e., 192.1.0.0/16. However, "addressPrefix": is a required property for subnet creation ARM templates and unless a value is specified the subnet creation is failing. Is there a way for the ARM template deployment to figure out what the next subnet should be?

Upvotes: 1

Views: 2219

Answers (1)

RahulKumarShaw
RahulKumarShaw

Reputation: 4602

You can use the below code to automatically assign IP when adding new Subnet.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "functions": [],
    "variables": {},
    "resources": [
        {
    "name": "vedoXXXvnet",
    "type": "Microsoft.Network/VirtualNetworks",
    "apiVersion": "2019-09-01",
    "location": "uksouth",
    "properties": {
        "addressSpace": {
            "addressPrefixes": [
                "192.168.0.0/16"
            ]
        },
        "copy": [
            {
                "name": "subnets",
                "count": 3,
                "input": {
                    "name": "[concat('subnet-', copyIndex('subnets', 1))]",
                    "properties": {
                        "addressPrefix": "[concat('192.168.', copyIndex('subnets', 1), '.0/24')]"
                    }
                }
            }
        ]
    }
}
    ],
    "outputs": {}
}

OutPut--

enter image description here

Upvotes: 1

Related Questions