Azure website resource template error

28,946

Solution 1

Never fails, as soon as I write out the question I figure out the answer.

The error means that because this is a nested resource (the config object is nested inside the site object) the name needs to reflect this. So instead of config the name should be something like mysite/config. I also needed to add the dependsOn section. Here's the template that validated successfully:

"resources": [
    {
        "apiVersion": "2014-04-01",
        "type": "Microsoft.Web/sites/config",
        "name": "[concat(parameters('siteName'), '/config')]",
        "dependsOn": [
            "[concat('Microsoft.Web/sites/', parameters('siteName'))]"
        ],
        "properties": {
            "phpVersion": "",
            "netFrameworkVersion": "V4.5"
        }
    }
]

Solution 2

I hit the same issue and neither of the other answers worked for me, turns out there's actually slightly more to this than the other answers show. Firstly, for a root level resource, the docs specify it must:

...have one less segment in the name than in the resource type

In other words if you're creating a:

"type": "Microsoft.Web/sites"

Then as the name must have one less segment than the type, we can only use a single segment for the name in this example, i.e. one:

"name": "MySite"

For a nested resource the rule is:

the type and name have the same number of segments

However this assumes that you are shortening a nested resource's type e.g. creating a type of "Microsoft.Web/sites/config" as a nested resource within a parent of type "Microsoft.Web/sites" and for the nested resource specifying:

"type": "config"

So here you can also only specify a single segment name, e.g.:

"name": "MyConfig"

So putting that all together you have:

{
  "type": "Microsoft.Web/sites",
  "name": "MySite",
  "various other properties": ...,
  "resources": [
    {
      "type": "config",
      "name": "MyConfig"
      "various other properties": ...
    }
  ]
}

If on the other hand you specify the full type name in the nested resource (as shown in the accepted answer) you need to resort to the root naming convention of having one less segment in the name than the type! Converting the above you'd have:

{
  "type": "Microsoft.Web/sites",
  "name": "MySite",
  "various other properties": ...,
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "MySite/MyConfig"
      "various other properties": ...
    }
  ]
}

Solution 3

The 'incorrect segment lengths' error message is difficult to understand for non-English native so there's explanation in plain English/json: For example, you have a resource of type Microsoft.Network/trafficManagerProfiles resource and for some reason you need to define nested resource having type Microsoft.Network/trafficManagerProfiles/ExternalEndpoints as a separate resource.

The nested resource must have name parent_resource_name/nested_res_name

The correct (simplified) schema is:

{
  "type": "Microsoft.Network/trafficManagerProfiles",
  "name": "[variables('trafManagerProfileName')]",
   ...
},
{
  "type": "Microsoft.Network/trafficManagerProfiles/ExternalEndpoints",
  "name": "[concat(variables('trafManagerProfileName'), '/Endpoint', copyIndex())]",
  "dependsOn": [
    "[concat('Microsoft.Network/trafficManagerProfiles/', variables('trafManagerProfileName'))]",
    "[parameters('app_name')]" # where the endpoint should look at
  ],
   ...
}

p.s. you might be interested in this question as well if you need to generate nested resources dynamically based on count of third resource: How do I dynamically generate Traffic Manager endpoints in an ARM template?

Solution 4

Something like this happened to me with Vnet-peering. I used this template from Microsoft's documentation:

{
     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
     "contentVersion": "1.0.0.0",
     "parameters": {
     },
     "variables": {
     },
 "resources": [
         {
         "apiVersion": "2016-06-01",
         "type": "Microsoft.Network/virtualNetworks/virtualNetworkPeerings",
         "name": "myVnetA/myVnetAToMyVnetB",
         "location": "[resourceGroup().location]",
         "properties": {
         "allowVirtualNetworkAccess": true,
         "allowForwardedTraffic": false,
         "allowGatewayTransit": false,
         "useRemoteGateways": false,
             "remoteVirtualNetwork": {
             "id": "/subscriptions/<subscription ID>/resourceGroups/PeeringTest/providers/Microsoft.Network/virtualNetworks/myVnetB"
             }
         }
         }
     ]
}

But then I modified the name to be, for example, vneta-tovnetb and it gave the me error.

If I use the vneta/vnetb as the name it does validate.

Share:
28,946
BenV
Author by

BenV

.NET Developer

Updated on July 09, 2022

Comments

  • BenV
    BenV almost 2 years

    I'm attempting to use the AzureResourceManager PowerShell module to create and configure a website. I started with a template file generated by Visual Studio, which works fine when I use it via New-AzureResourceGroup -TemplateFile website.json.

    So now I'm trying to tweak the template file to configure the site. I'm trying to set the php and .NET Framework versions. According to the schema these properties are set via a config object in a resources array.

    Here's the website section of my json template. The "resources" section is what I added:

        {
            "apiVersion": "2014-06-01",
            "name": "[parameters('siteName')]",
            "type": "Microsoft.Web/sites",
            "location": "[parameters('siteLocation')]",
            "tags": {
                "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource"
            },
            "dependsOn": [
                "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
            ],
            "properties": {
                "name": "[parameters('siteName')]",
                "serverFarm": "[parameters('hostingPlanName')]"
            },
            "resources": [
                {
                    "apiVersion": "2014-04-01",
                    "type": "Microsoft.Web/sites/config",
                    "name": "config",
                    "properties": {
                        "name": "config",
                        "phpVersion": "",
                        "netFrameworkVersion": "V4.5"
                    }
                }
            ]
        },
    

    When I pass this template to Test-AzureResourceGroupTemplate I get this error:

    Code    : InvalidTemplate
    Message : Deployment template validation failed: 'The template resource 'config' for type 'Microsoft.Web/sites/config' has 
              incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root 
              resource type must have segment length one greater than its resource name'.
    

    I can't find any documentation on this. Does anyone know what this error means, or what I'm doing wrong?

  • OzBob
    OzBob over 7 years
    just in case anyone is having the same error while trying to add 'nested' appsettings of connectionstrings, 1 head the above '/' advice, or add some more website resources like here: stackoverflow.com/questions/40396154/…
  • Jeremy Caney
    Jeremy Caney over 3 years
    Welcome to Stack Overflow, and thank you for your contribution. I've updated your answer to provide improved formatting; please review the Markdown so you can use similar formatting in future posts. Can you verify that the solution here is to use vneta/vnetb for the name?
  • engzaz
    engzaz over 3 years
    Hi Jeremy, Thanks for modifying my answer. yes setting the name to be vneta/vnetb will make it work.
  • codemonkeh
    codemonkeh over 3 years
    I like how the documentation claims that "this makes sense" even though it is completely arbitrary and unintuitive
  • Dragas
    Dragas almost 3 years
    @codemonkeh Just microsoft things (light piano sounds)
  • Cribber
    Cribber over 2 years
    For anyone else needing it, here is the official documentation: docs.microsoft.com/en-us/azure/azure-resource-manager/templa‌​tes/…