|
| 1 | +--- |
| 2 | +title: Linter rule - use resourceId functions |
| 3 | +description: Linter rule - use resourceId functions |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 09/21/2022 |
| 6 | +--- |
| 7 | + |
| 8 | +# Linter rule - use resourceId function |
| 9 | + |
| 10 | +This rule finds uses of Azure location values that aren't parameterized. |
| 11 | + |
| 12 | +If property "id" represents a resource ID, it must use a symbolic resource reference, be a parameter or start with one of these functions: extensionResourceId, guid, if, reference, resourceId, subscription, subscriptionResourceId, tenantResourceId. Found nonconforming expression at id -> serviceBusConnectionId [https://aka.ms/bicep/linter/use-resource-id-functions] |
| 13 | + |
| 14 | +## Linter rule code |
| 15 | + |
| 16 | +Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings: |
| 17 | + |
| 18 | +`use-resource-id-functions` |
| 19 | + |
| 20 | +## Solution |
| 21 | + |
| 22 | +Template users may have limited access to regions where they can create resources. A hard-coded resource location might block users from creating a resource, thus preventing them from using the template. By providing a location parameter that defaults to the resource group location, users can use the default value when convenient but also specify a different location. |
| 23 | + |
| 24 | +Rather than using a hard-coded string or variable value, use a parameter, the string 'global', or an expression (but not `resourceGroup().location` or `deployment().location`, see [no-loc-expr-outside-params](./linter-rule-no-loc-expr-outside-params.md)). Best practice suggests that to set your resources' locations, your template should have a string parameter named `location`. This parameter may default to the resource group or deployment location (`resourceGroup().location` or `deployment().location`). |
| 25 | + |
| 26 | +The following example fails this test because the resource's `location` property uses a string literal: |
| 27 | + |
| 28 | +```bicep |
| 29 | + resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = { |
| 30 | + location: 'westus' |
| 31 | + } |
| 32 | +``` |
| 33 | +You can fix it by creating a new `location` string parameter (which may optionally have a default value - resourceGroup().location is frequently used as a default): |
| 34 | + |
| 35 | +```bicep |
| 36 | + param location string = resourceGroup().location |
| 37 | + resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = { |
| 38 | + location: location |
| 39 | + } |
| 40 | +``` |
| 41 | + |
| 42 | +The following example fails this test because the resource's `location` property uses a variable with a string literal. |
| 43 | + |
| 44 | +```bicep |
| 45 | + var location = 'westus' |
| 46 | + resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = { |
| 47 | + location: location |
| 48 | + } |
| 49 | +``` |
| 50 | + |
| 51 | +You can fix it by turning the variable into a parameter: |
| 52 | + |
| 53 | +```bicep |
| 54 | + param location string = 'westus' |
| 55 | + resource stg 'Microsoft.Storage/storageAccounts@2021-02-01' = { |
| 56 | + location: location |
| 57 | + } |
| 58 | +``` |
| 59 | + |
| 60 | +The following example fails this test because a string literal is being passed in to a module parameter that is in turn used for a resource's `location` property: |
| 61 | + |
| 62 | +```bicep |
| 63 | +module m1 'module1.bicep' = { |
| 64 | + name: 'module1' |
| 65 | + params: { |
| 66 | + location: 'westus' |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +where module1.bicep is: |
| 71 | +```bicep |
| 72 | +param location string |
| 73 | +
|
| 74 | +resource storageaccount 'Microsoft.Storage/storageAccounts@2021-02-01' = { |
| 75 | + name: 'storageaccount' |
| 76 | + location: location |
| 77 | + kind: 'StorageV2' |
| 78 | + sku: { |
| 79 | + name: 'Premium_LRS' |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +You can fix the failure by creating a new parameter for the value: |
| 85 | +```bicep |
| 86 | +param location string // optionally with a default value |
| 87 | +module m1 'module1.bicep' = { |
| 88 | + name: 'module1' |
| 89 | + params: { |
| 90 | + location: location |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Next steps |
| 96 | + |
| 97 | +For more information about the linter, see [Use Bicep linter](./linter.md). |
0 commit comments