Edit

Share via


Linter rule - no ___location expressions outside of parameter default values

This rule finds resourceGroup().___location or deployment().___location used outside of a parameter default value.

Note

This rule is off by default. Change the level in bicepconfig.json to enable it.

Linter rule code

Use the following value in the Bicep configuration file to customize rule settings:

no-loc-expr-outside-params

Solution

resourceGroup().___location and deployment().___location should only be used as the default value of a parameter.

Template users may have limited access to regions where they can create resources. The expressions resourceGroup().___location or deployment().___location could block users if the resource group or deployment was created in a region the user can't access, thus preventing them from using the template.

Best practice suggests that to set your resources' locations, your template should have a string parameter named ___location. If you default the ___location parameter to resourceGroup().___location or deployment().___location instead of using these functions elsewhere in the template, users of the template can use the default value when convenient but also specify a different ___location when needed.

resource storageaccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
  ___location: resourceGroup().___location
}

You can fix the failure by creating a ___location property that defaults to resourceGroup().___location and use this new parameter instead:

param ___location string = resourceGroup().___location

resource storageaccount 'Microsoft.Storage/storageAccounts@2023-04-01' = {
  ___location: ___location
}

The following example fails this test because ___location is using resourceGroup().___location but isn't a parameter:

  var ___location = resourceGroup().___location

You can fix the failure by turning the variable into a parameter:

  param ___location string  = resourceGroup().___location

If you're using Azure PowerShell to deploy to a subscription, management group, or tenant, you should use a parameter name other than ___location. The New-AzDeployment, New-AzManagementGroupDeployment, and New-AzTenantDeployment commands have a parameter named ___location. This command parameter conflicts with the parameter in your Bicep file. You can avoid this conflict by using a name such as rgLocation.

You can use ___location for a parameter name when deploying to a resource group, because New-AzResourceGroupDeployment doesn't have a parameter named ___location.

Next steps

For more information about the linter, see Use Bicep linter.