0
votes

I'm currently facing an issue with nested template. When I'm applying my template (detail below), I get this answer from Azure:

Azure Error: InvalidTemplate
Message: Deployment template validation failed: 'The template reference 'sandbox.test.portal' is not valid: could not find template resource or resource copy with this name. Please see https://aka.ms/arm-template-expressions/#reference for usage details.'.

However, I don't really understand why I get this issue, because for the content inside the nested template, I used what they provide in the documentation here: https://github.com/Azure/azure-quickstart-templates/blob/master/101-azure-dns-new-zone/azuredeploy.json

My ARM template:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "newZoneName": {
            "type": "string",
            "defaultValue": "sandbox.test.portal",
            "metadata": {
                "description": "The name of the DNS zone to be created.  Must have at least 2 segements, e.g. hostname.org"
            }
        },
        "newRecordName": {
            "type": "string",
            "defaultValue": "www",
            "metadata": {
                "description": "The name of the DNS record to be created.  The name is relative to the zone, not the FQDN."
            }
        }
    },
    "variables": {
        "publicIPAddressName": "[concat(resourceGroup().name, '-pip')]",
    },
    "resources": [
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[variables('publicIPAddressName')]",
            "location": "[parameters('location')]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "dnsSettings": {
                    "domainNameLabel": "[parameters('dnsLabelPrefix')]"
                }
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "nestedTemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "my-rg",
            "subscriptionId": "[subscription().subscriptionId]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                    },
                    "variables": {
                    },
                    "resources": [
                        {
                            "type": "Microsoft.Network/dnszones",
                            "name": "[parameters('newZoneName')]",
                            "apiVersion": "2016-04-01",
                            "location": "global",
                            "properties": {
                            }
                        },
                        {
                            "type": "Microsoft.Network/dnszones/a",
                            "name": "[concat(parameters('newZoneName'), '/', parameters('newRecordName'))]",
                            "apiVersion": "2016-04-01",
                            "location": "global",
                            "dependsOn": [
                                "[parameters('newZoneName')]"
                            ],
                            "properties": {
                                "TTL": 3600,
                                "ARecords": [
                                    {
                                        "ipv4Address": "1.2.3.4"
                                    },
                                    {
                                        "ipv4Address": "1.2.3.5"
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {
                        "nameServers": {
                            "type": "array",
                            "value": "[reference(parameters('newZoneName')).nameServers]"
                        }
                    }
                }
            }
        }
    ]
}
2

2 Answers

1
votes

basically, you need to remove the outputs from the nested inline template, so remove this bit:

"outputs": {
    "nameServers": {
        "type": "array",
        "value": "[reference(parameters('newZoneName')).nameServers]"
    }
}

long story short, nested inline deployments are bad. dont use them.

alternatively move those to the parent template and do a real lookup:

reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')))
0
votes

You have several minor mistakes in your template:

  1. Comma in variables '-pip')]",
  2. Undefined parameter dnsLabelPrefix

The general mistake in nested outputs. When you use nested templates azure don't find it in your main template. Therefore you must use a reference function with identifier and API: "[reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')), '2016-04-01').nameServers]".

I modified your template and validate in my subscription.

    {
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "newZoneName": {
            "type": "string",
            "defaultValue": "sandbox.test.portal",
            "metadata": {
                "description": "The name of the DNS zone to be created.  Must have at least 2 segements, e.g. hostname.org"
            }
        },
        "newRecordName": {
            "type": "string",
            "defaultValue": "www",
            "metadata": {
                "description": "The name of the DNS record to be created.  The name is relative to the zone, not the FQDN."
            }
        },
        "dnsLabelPrefix": {
            "type": "string",
            "defaultValue": "[concat('dns',uniqueString(resourceGroup().name))]"
        },
        "nestedResourceGroup": {
            "type": "string",
            "defaultValue": "my-rg",
            "metadata": {
                "description": "my-rg"
            }
        }
    },
    "variables": {
        "publicIPAddressName": "[concat(resourceGroup().name, '-pip')]"
    },
    "resources": [
        {
            "apiVersion": "2015-06-15",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[variables('publicIPAddressName')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "dnsSettings": {
                    "domainNameLabel": "[parameters('dnsLabelPrefix')]"
                }
            }
        },
        {
            "apiVersion": "2017-05-10",
            "name": "nestedTemplate",
            "type": "Microsoft.Resources/deployments",
            "resourceGroup": "[parameters('nestedResourceGroup')]",
            "subscriptionId": "[subscription().subscriptionId]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {
                    },
                    "variables": {
                    },
                    "resources": [
                        {
                            "type": "Microsoft.Network/dnszones",
                            "name": "[parameters('newZoneName')]",
                            "apiVersion": "2016-04-01",
                            "location": "global",
                            "properties": {
                            }
                        },
                        {
                            "type": "Microsoft.Network/dnszones/a",
                            "name": "[concat(parameters('newZoneName'), '/', parameters('newRecordName'))]",
                            "apiVersion": "2016-04-01",
                            "location": "global",
                            "dependsOn": [
                                "[resourceId('Microsoft.Network/dnszones', parameters('newZoneName'))]"
                            ],
                            "properties": {
                                "TTL": 3600,
                                "ARecords": [
                                    {
                                        "ipv4Address": "1.2.3.4"
                                    },
                                    {
                                        "ipv4Address": "1.2.3.5"
                                    }
                                ]
                            }
                        }
                    ],
                    "outputs": {
                        "nameServers": {
                            "type": "array",
                            "value": "[reference(resourceId('Microsoft.Network/dnszones', parameters('newZoneName')), '2016-04-01').nameServers]"
                        }
                    }
                }
            }
        }
    ]
}

Have a nice day!