title: Deploy an AKS cluster inside an existing Vnet/sub-net using ARM templates
body: <p>I have an existing Virtual Network created with two subnets: <code>aks-subnet</code> and <code>persistence-subnet</code>.</p>
  
  <p>My goal is to create an Azure Kubernetes Cluster inside the <code>aks-subnet</code></p>
  
  <p>I am creating resource groups and resources <a href="https://docs.microsoft.com/bs-latn-ba/azure/azure-resource-manager/deploy-to-subscription" rel="nofollow noreferrer">at the subscription level</a>, using the <code>New-AzDeployment</code> command from PowerShell core.</p>
  
  <p>Like my idea is create a resource group and deploy resources to it, I have a nested template defining the resources to create in the resource group. </p>
  
  <p>So I have the resource group created from the ARM template</p>
  
  <ul>
  <li><strong>please find</strong> this json definition in the entire template showed below at the end of this thread</li>
  </ul>
  
  <pre><code>"type": "Microsoft.Resources/resourceGroups",
  </code></pre>
  
  <p>And I am using a Deployment resource in order to nest the template that contains the resources that I want to create inside the resource group.</p>
  
  <ul>
  <li><strong>please find inmediately after:</strong></li>
  </ul>
  
  <pre><code>"type": "Microsoft.Resources/deployments"
  </code></pre>
  
  <p>So inside, this <code>Microsoft.Resources/deployments</code> I am creating a Vnet with the two subnets previously mentioned, </p>
  
  <p><strong>and It works!</strong>, the Vnet and the subnet is created inside the resource group created in the same template.</p>
  
  <ul>
  <li><strong>Please find</strong> in the entire template showed below at the end of this thread</li>
  </ul>
  
  <pre><code>"type": "Microsoft.Network/virtualNetworks",
  </code></pre>
  
  <p>Now I want to add an Azure Kubernetes cluster inside the <code>aks-vnet</code>.</p>
  
  <ul>
  <li><strong>Please find</strong> in the entire template: </li>
  </ul>
  
  <pre><code>"dependsOn": [
      "Microsoft.Network/virtualNetworks/AssessmentVNet"
  ],
  "type": "Microsoft.ContainerService/managedClusters",
  </code></pre>
  
  <p>And then when I am associating the defaultpool (or virtual machine) to the <code>aks-subnet</code>, </p>
  
  <ul>
  <li><strong>Please find</strong> <code>vnetSubnetID</code> attribute of this way in the entire template showed below at the end of this thread:</li>
  </ul>
  
  <pre><code>"vnetSubnetID": "[resourceId(parameters('resourceGroupName'),'Microsoft.Network/virtualNetworks/subnets',parameters('vnetName'),parameters('subnet1Name'))]",
  </code></pre>
  
  <p>I am trying to access to the VnetSubnetID of this way, according to this <a href="https://github.com/Azure/azure-quickstart-templates/blob/master/101-aks-advanced-networking/azuredeploy.json#L157" rel="nofollow noreferrer">AKS advanced networking official link</a> suggest:</p>
  
  <p>According to the immediately above I am doing here the following:</p>
  
  <ul>
  <li>I got the <code>resourceId</code> from the <code>resourceGroupName</code> where is located the Vnet.</li>
  <li>I am indicating the type of resource subnet <code>Microsoft.Network/virtualNetworks/subnets</code></li>
  <li>And I am passing like parameters the name of the Vnet which have the subnet and the name of the subnet as well <code>parameters('vnetName'),parameters('subnet1Name'))</code></li>
  </ul>
  
  <p>But when I execute the template from Power shell I got the following error:</p>
  
  <pre><code>PS /home/bgarcial/projects/my-project/Deployments/ARMTemplates/ResourceGroup&gt; New-AzDeployment `
  &gt;&gt;  -Name SentiaAssessment `
  &gt;&gt;  -location westeurope `
  &gt;&gt;  -TemplateUri $templateUri `
  &gt;&gt;  -resourceGroupName $resourceGroupName `
  &gt;&gt;  -environmentName accp `
  &gt;&gt;  -dnsPrefix WordpressSentiaAssessment-dns `
  &gt;&gt;  -servicePrincipalClientId $servicePrincipalClientId `
  &gt;&gt;  -servicePrincipalClientSecret $servicePrincipalClientSecret
  
  New-AzDeployment : 10:20:02 PM - Resource Microsoft.Resources/deployments 'storageDeployment' failed with message '{
  
  
  "error": {
      "code": "InvalidTemplate",
      "message": "Unable to process template language expressions for resource '/subscriptions/9148bd11-f32b-4b5d-a6c0-5ac5317f29ca/resourceGroups/sentia-assessment/providers/Microsoft.Resources/deployments/storageDeployment' at line '150' and column '9'. 'The provided value 'sentia-assessment' is not valid subscription identifier. Please see https://aka.ms/arm-template-expressions/#resourceid for usage details.'",
      "additionalInfo": [
        {
          "type": "TemplateViolation",
          "info": {
            "lineNumber": 150,
            "linePosition": 9,
            "path": ""
          }
        }
      ]
    }
  }'
  At line:1 char:1
  + New-AzDeployment `
  + ~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [New-AzDeployment], Exception
  + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureDeploymentCmdlet
  
  New-AzDeployment : 10:20:02 PM - Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
  At line:1 char:1
  + New-AzDeployment `
  + ~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [New-AzDeployment], Exception
  + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureDeploymentCmdlet
  
  New-AzDeployment : 10:20:02 PM - Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
  At line:1 char:1
  + New-AzDeployment `
  + ~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [New-AzDeployment], Exception
  + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureDeploymentCmdlet
  
  
  DeploymentName          : MyDeployment
  Location                : westeurope
  
  
  ProvisioningState       : Failed
  Timestamp               : 10/23/19 8:19:57 PM
  Mode                    : Incremental
  TemplateLink            : 
  Parameters              : 
                            Name                            Type                       Value     
                            ==============================  =========================  ==========
                            resourceGroupName               String                     sentia-assessment
                            location                        String                     West Europe
                            vnetName                        String                     AssessmentVNet
                            vnetAddressPrefix               String                     10.0.0.0/8
                            subnet1Prefix                   String                     10.240.0.0/16
                            subnet1Name                     String                     aks-subnet
                            subnet2Prefix                   String                     10.241.0.0/27
                            subnet2Name                     String                     persistence-subnet
                            k8s_cluster_name                String                     WordpressSentiaAssessment
                            kubernetesVersion               String                     1.14.7    
                            dnsPrefix                       String                     WordpressSentiaAssessment-dns
                            nodeCount                       Int                        3         
                            agentVMSize                     String                     Standard_D2_v2
                            servicePrincipalClientId        SecureString                         
                            servicePrincipalClientSecret    SecureString                         
                            serviceCidr                     String                     100.0.0.0/16
                            dnsServiceIP                    String                     100.0.0.10
                            dockerBridgeCidr                String                     172.17.0.1/16
                            environmentName                 String                     accp      
  
  Outputs                 : 
  DeploymentDebugLogLevel : 
  
  
  PS /home/bgarcial/projects/my-project/Deployments/ARMTemplates/ResourceGroup&gt; 
  </code></pre>
  
  <p>Looks like I would need to include the suscriptionId value inside the resourceId template function that I am using, but currently is not clear for me how to do it despite that I am querying <a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#resourceid" rel="nofollow noreferrer">the template reference</a></p>
  
  <p>By the way, other detail is that I am using as a <code>networkPlugin</code>, kubenet.</p>
  
  <p>Do I need to use the <strong>Azure CNI</strong> as a network plugin in order to get it? </p>
  
  <p>This is the complete ARM template. I put it here in order to give an idea that what I am doing and how, and also to someone can find the json sections code definitions that I explain above.</p>
  
  <p>I would be very grateful if someone can point me in the right direction</p>
  
  <pre><code>{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "resourceGroupName": {
        "type": "string"  
      },
      "location": {
        "type": "string",
        "defaultValue": "West Europe",
        "metadata": {
          "description": "Geographic Location for all resources."
        }
      },
      "vnetName": {
        "type": "string",
        "defaultValue": "AssessmentVNet",
        "metadata": {
          "description": "Sentia Wordpress Assesment Vnet"
        }
      },
      "vnetAddressPrefix": {
        "type": "string",
        "defaultValue": "10.0.0.0/8",
        "metadata": {
          "description": "Address prefix"
        }
      },
      "subnet1Prefix": {
        "type": "string",
        "defaultValue": "10.240.0.0/16",
        "metadata": {
          "description": "AKS Subnet"
        }
      },
      "subnet1Name": {
        "type": "string",
        "defaultValue": "aks-subnet",
        "metadata": {
          "description": "aks-subnet"
        }
      },
      "subnet2Prefix": {
        "type": "string",
        "defaultValue": "10.241.0.0/27",
        "metadata": {
          "description": "Persistence subnet"
        }
      },
      "subnet2Name": {
        "type": "string",
        "defaultValue": "persistence-subnet",
        "metadata": {
          "description": "persistence-subnet"
        }
      },
      "k8s_cluster_name":{
        "type": "string",
        "defaultValue": "WordpressSentiaAssessment",
        "metadata": {
          "description": "The name of the Azure Kubernetes Service Cluster"
        }
      },
      "kubernetesVersion": {
        "type": "string",
          "defaultValue": "1.14.7",
          "metadata": {
            "description": "The version of the Azure Kubernetes Service Cluster"
          }
      },
      "dnsPrefix": {
        "type": "string",
        "metadata": {
            "description": "Optional DNS prefix to use with hosted Kubernetes API server FQDN."
        }
      },
      "nodeCount": {
        "type": "int",
        "defaultValue": 3,
        "metadata": {
            "description": "The number of nodes that should be created along with the cluster."
        },
        "minValue": 1,
        "maxValue": 100
      },
      "agentVMSize": {
        "type": "string",
        "defaultValue": "Standard_D2_v2",
        "metadata": {
          "description": "The size of the Virtual Machine."
        }
      },
      "servicePrincipalClientId": {
        "metadata": {
          "description": "Client ID (used by cloudprovider)."
        },
        "type": "securestring"
      },
      "servicePrincipalClientSecret": {
        "metadata": {
          "description": "The Service Principal Client Secret."
        },
        "type": "securestring"
      },
      "serviceCidr": {
        "type": "string",
          "metadata": {
              "description": "A CIDR notation IP range from which to assign service cluster IPs."
          },
          "defaultValue": "100.0.0.0/16"
      },
      "dnsServiceIP": {
        "type": "string",
          "metadata": {
            "description": "Containers DNS server IP address."
          },
          "defaultValue": "100.0.0.10"
      },
      "dockerBridgeCidr": {
        "type": "string",
        "metadata": {
            "description": "A CIDR notation IP for Docker bridge."
        },
        "defaultValue": "172.17.0.1/16"
      },
      "environmentName": {
        "type": "string",
        "metadata": {
          "description": "Environment name for tagging purposes, e.g. dev, accp, prod"
        }
      }
    },
    "variables": {
      "osDiskSizeGB": 0,
      "osType": "Linux",
      "maxPods": 110,
      "networkPlugin": "kubenet"
    },
    "resources": [
      {
        "type": "Microsoft.Resources/resourceGroups",
        "apiVersion": "2018-05-01",
        "location": "[parameters('location')]",
        "name": "[parameters('resourceGroupName')]",
        "properties": {}
      },
      {
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2018-05-01",
        "name": "storageDeployment",
        "resourceGroup": "[parameters('resourceGroupName')]",
        "dependsOn": [
            "[resourceId('Microsoft.Resources/resourceGroups/', parameters('resourceGroupName'))]"
        ],
        "properties": {
          "mode": "Incremental",
          "template": {
            "$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "parameters": {},
            "variables": {},
            "resources": [
              {
                "apiVersion": "2018-10-01",
                "type": "Microsoft.Network/virtualNetworks",
                "name": "[parameters('vnetName')]",
                "location": "[parameters('location')]",
                "properties": {
                  "subnets": [
                    {
                      "name": "[parameters('subnet1Name')]",
                      "properties": {
                        "addressPrefix": "[parameters('subnet1Prefix')]"
                      }
                    },
                    {
                      "name": "[parameters('subnet2Name')]",
                      "properties": {
                        "addressPrefix": "[parameters('subnet2Prefix')]",
                        "serviceEndpoints": [
                          {
                            "service": "Microsoft.Storage",
                            "locations": "[parameters('location')]"
                          },
                          {
                            "service": "Microsoft.Sql",
                            "locations": "[parameters('location')]"
                          }
                        ]
                      }
                    }
                  ],      
                  "addressSpace": {
                    "addressPrefixes": [
                      "[parameters('vnetAddressPrefix')]"
                    ]
                  }
                },
                "tags": {
                  "Environment": "[parameters('environmentName')]"
                }
              },
              {
                "apiVersion": "2019-06-01",
                "dependsOn": [
                  "Microsoft.Network/virtualNetworks/AssessmentVNet"
                ],
                "type": "Microsoft.ContainerService/managedClusters",
                "location":"[parameters('location')]",
                "name": "[concat(parameters('k8s_cluster_name'), '-aks')]",
                "properties":{
                  "kubernetesVersion":"[parameters('kubernetesVersion')]",
                  "enableRBAC": true,
                  "dnsPrefix": "[concat(parameters('k8s_cluster_name'),'-dns')]",
                  "agentPoolProfiles":[
                    {
                      "name":"defaultpool",
                      "osDiskSizeGB": "[variables('osDiskSizeGB')]",
                      "count":"[parameters('nodeCount')]",
                      "vmSize": "[parameters('agentVMSize')]",
                      "osType": "[variables('osType')]",
                      "storageProfile": "ManagedDisks",
                      "type": "VirtualMachineScaleSets",
                      "vnetSubnetID": "[resourceId(parameters('resourceGroupName'),'Microsoft.Network/virtualNetworks/subnets',parameters('vnetName'),parameters('subnet1Name'))]",
                      "maxPods": "[variables('maxPods')]"
                    }
                  ],
                  "servicePrincipalProfile": {
                      "ClientId": "[parameters('servicePrincipalClientId')]",
                      "Secret": "[parameters('servicePrincipalClientSecret')]"
                  },
                  "networkProfile": {
                    "networkPlugin": "[variables('networkPlugin')]",
                    "serviceCidr": "[parameters('serviceCidr')]",
                    "dnsServiceIP": "[parameters('dnsServiceIP')]",
                    "dockerBridgeCidr": "[parameters('dockerBridgeCidr')]"
                  }
                },
                "tags": {
                  "Environment": "[parameters('environmentName')]"
                }
              },
              {
  
              }  
            ]
          }
        }
      }    
    ]
  }
  
  </code></pre>