Azure website resource template error

2019-02-11 17:11发布

问题:

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?

回答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"
        }
    }
]


回答2:

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?