Azure-docs: How to access Basic parameter values from within a template

Created on 22 Aug 2020  Â·  10Comments  Â·  Source: MicrosoftDocs/azure-docs

Thank you for the "Create resource groups" template example, which shows how to create an empty resource group.

When deployed via the Azure portal via "Create a resource", "Template deployment (deploy using custom templates)", "Create", "Build your own template in the editor", when we Save this template, the portal prompts us for 5 inputs:

  • Basics
    -- Subscription
    -- Resource Group
    -- Location
  • Settings
    -- Rg Name
    -- Rg Location

All are required. The last 2 are defined in the template, but the first 3 seem to be prompts from the portal.

  1. I understand resource groups may not nest, so why are we prompted for a Resource Group when trying to deploy a template that is trying to create a resource group?
  2. I understand that the values of the user-defined parameters may be accessed from within the template using the deployment function for example as shown in the example:
    parameters('rgName')
    From within the template how do I access the supplied values for the "Basic" parameters?
    For example, how might I use the "Basic" Location value as the value of the rgLocation parameter?

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 assigned-to-author azure-resource-managesvc product-question templatesubsvc triaged

All 10 comments

Hi @SadiqhAhmed-MSFT
Thank you for reaching out. At this time we are reviewing the ask and will provide an update as appropriate

@HughDevlin - if you are using https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json# for the template schema, the portal should recognize that the template is a subscription level deployment. It will not prompt you for the resource group, but it will still ask for subscription and location in the Basics section. However, there was an issue recently with the portal not working with subscription level deployments so you may have run into that issue.

In this case, location is the location for the deployment being processed, not the location for resources. For any deployments other than resource group level, location is a required parameter of the operation.

Thank you for your reply.

I understand the distinction between the location of a resource group and the location of the resources within a resource group, and that the locations may differ, thanks.

Yes, this issue may be reproduced using the "Create resource groups" template example as is, which invokes the 2018-05-01 subscription Deployment schema.

Here is the create resource example template for reference:

{ "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "rgName": { "type": "string" }, "rgLocation": { "type": "string" } }, "variables": {}, "resources": [ { "type": "Microsoft.Resources/resourceGroups", "apiVersion": "2020-06-01", "name": "[parameters('rgName')]", "location": "[parameters('rgLocation')]", "properties": {} } ], "outputs": {} }
Here is a screen shot of the create resource example after saving in the portal.

Screenshot from 2020-08-24 10-18-21

Today I'm not seeing the Basic section, so perhaps as you say I ran into a bug, since resolved.

It seems like the portal prompts for two required parameters, Region and RgName, the latter defined in the template, the former apparently from the portal. It feels like we are prompted for the region twice, and are forced to repeat ourselves. Can we reference the value of the Region parameter from within the template, so as to provide it as the value of the required location parameter of the resource group?

Sorry for the confusion about the regions. Every time you deploy a template there is metadata that is retained. For templates deployed to a resource group, the metadata is automatically stored in the region of the resource group. Since subscriptions don't have a region, Azure Resource Manager doesn't know where to store the metadata for templates deployed to a subscription. The Region field is the value that tells Azure Resource Manager where to store the metadata. This parameter comes from the operation, not the template.

Technically, you could remove the rgLocation parameter and instead set the resource group location to deployment().location. But, you would lose a lot of flexibility in the template and are probably better off keeping the rgLocation parameter.

Thank you for your reply. I understand resource groups have a location where the resource group metadata is stored.

Within a template, deployment().location reflects the setting of the Region portal parameter?
Under what conditions might one want to set the Region portal parameter different from the RgLocation template parameter? How is flexibility lost to set rgLocation from deployment().location?

deployment().location reflects the location of the subscription level deployment, whether it comes from the portal, script, REST or SDK.

You may have some reason why you'd want the deployment data to be stored in one region but the new resource group to be created in a different region. If you provide the rgLocation, that is possible, but without it you have always tied those locations together. Once you create a deployment with a name and a location, you can reuse that name but you can't change its location. This is not be a problem if you are creating unique names for each deployment, but it creates a problem if you reuse a common name like "mainTemplate". The next time you use mainTemplate for the deployment name but try to provide a different location because you want the resource group in a different location, you would get an error saying the location of mainTemplate can't change.

Thank you for your patience. Please can you clarify the semantics of the "operation", portal parameter Region in contrast with the "template" location parameter required by the resource group creation schema? If I understand,

  1. the Region parameter is where the meta data of the resource group is stored
  2. the location parameter is where the resource group is created

and these can be different, and different still from where the resources in the resource group if any are created?

The region parameter is where metadata about the deployment is stored. Other subscription level templates do not create a resource group but they still need the region parameter because the deployment data needs to be retained. It is part of the command you use to deploy the template. For example, az deployment sub create has a required location parameter.

Within the template, you can create parameters for locations you need. Your example template has one location that is used to create the new resource group. You could have more than one if you need to create resource groups in more than one region.

@HughDevlin - hopefully that answers your questions. We'll close this issue for now.

please-close

thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrdfuse picture mrdfuse  Â·  3Comments

varma31 picture varma31  Â·  3Comments

paulmarshall picture paulmarshall  Â·  3Comments

jebeld17 picture jebeld17  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments