Share the love

I recently came up with a request to create (network interface) NIC with a static private IP. Well, it’s easy enough to do it. But the challenge was that the NIC’s IP should not be sent in the parameters.

Solution

It’s easy to achieve it. You just need to come up with a logic. Here’s the logic I applied.

  • Create a NIC with dynamic IP
  • Capture the current IP of the NIC and make it static

Sounds simple right? Let’s dig into the code. In my case I had to create multiple NICs and the same code has been added below for your reference.

Create dynamic IP

Following code is just a regular template to create a NIC. You may notice that padLeft is used since my naming convention included adding the index in a 3 digit format. For instance, following code would create NICs with name <prefix>001-NIC, <prefix>002-NIC and so on.

    {
      "name": "[concat(parameters('serverNamePrefix'),padLeft(copyIndex(),3,'0'),'-NIC')]",
      "type": "Microsoft.Network/networkInterfaces",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-09-01",
      "copy": {
        "name": "nicLoop",
        "count": "[parameters('serverInstanceCount')]"
      },
      "dependsOn": [],
      "tags": "[parameters('tagValues')]",
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }
        ],
        "enableAcceleratedNetworking": true
      }
    }

Change dynamic IP to static

To achieve this, we will create a nested template within the same template. This part of code fetches the current private of the NIC and makes it static.

    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2018-05-01",
      "name": "[concat('StaticIp', copyIndex())]",
      "dependsOn": [
        "nicLoop"
      ],
      "copy": {
        "name": "ipLoop",
        "count": "[parameters('ServerInstanceCount')]"
      },
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Network/networkInterfaces",
              "name": "[concat(parameters('ServerNamePrefix'),padLeft(copyIndex(),3,'0'),'-NIC')]",
              "apiVersion": "2018-03-01",
              "location": "[resourceGroup().location]",
              "tags": "[parameters('tagValues')]",
              "properties": {
                "ipConfigurations": [
                  {
                    "name": "ipconfig1",
                    "properties": {
                      "privateIPAllocationMethod": "Static",
                      "privateIPAddress": "[reference(concat(parameters('ServerNamePrefix'),padLeft(copyIndex(),3,'0'),'-NIC')).ipConfigurations[0].properties.privateIPAddress]",
                      "subnet": {
                        "id": "[variables('subnetRef')]"
                      }
                    }
                  }
                ],
                "enableAcceleratedNetworking": true
              }
            }
          ]
        }
      }
    }

Hopefully this will help you out if you’re also facing the same challenge. Remember to remove copy function if you don’t want to create multiple NICs. I have included it with copy function since I came across few folks who had somewhat hard time getting around loop while using nested template.

If you have some better logic, do post it in comments. It may help others.