|
| 1 | +--- |
| 2 | +title: Linter rule - use resource ID functions |
| 3 | +description: Linter rule - use resource ID functions |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 09/23/2022 |
| 6 | +--- |
| 7 | + |
| 8 | +# Linter rule - use resource ID functions |
| 9 | + |
| 10 | +Ensures that the ID of a symbolic resource name or a suitable function is used rather than a manually created ID, such as a concatenating string, for all properties representing a resource ID. Use resource symbolic names whenever it's possible. |
| 11 | + |
| 12 | +The allowed functions include: |
| 13 | + |
| 14 | +- [`extensionResourceId`](./bicep-functions-resource.md#extensionresourceid) |
| 15 | +- [`resourceId`](./bicep-functions-resource.md#resourceid) |
| 16 | +- [`subscriptionResourceId`](./bicep-functions-resource.md#subscriptionresourceid) |
| 17 | +- [`tenantResourceId`](./bicep-functions-resource.md#tenantresourceid) |
| 18 | +- [`reference`](./bicep-functions-resource.md#reference) |
| 19 | +- [`subscription`](./bicep-functions-scope.md#subscription) |
| 20 | +- [`guid`](./bicep-functions-string.md#guid) |
| 21 | + |
| 22 | +## Linter rule code |
| 23 | + |
| 24 | +Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings: |
| 25 | + |
| 26 | +`use-resource-id-functions` |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +The following example fails this test because the resource's `api/id` property uses a manually created string: |
| 31 | + |
| 32 | +```bicep |
| 33 | +@description('description') |
| 34 | +param connections_azuremonitorlogs_name string |
| 35 | +
|
| 36 | +@description('description') |
| 37 | +param location string |
| 38 | +
|
| 39 | +@description('description') |
| 40 | +param resourceTags object |
| 41 | +param tenantId string |
| 42 | +
|
| 43 | +resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = { |
| 44 | + name: connections_azuremonitorlogs_name |
| 45 | + location: location |
| 46 | + tags: resourceTags |
| 47 | + properties: { |
| 48 | + displayName: 'azuremonitorlogs' |
| 49 | + statuses: [ |
| 50 | + { |
| 51 | + status: 'Connected' |
| 52 | + } |
| 53 | + ] |
| 54 | + nonSecretParameterValues: { |
| 55 | + 'token:TenantId': tenantId |
| 56 | + 'token:grantType': 'code' |
| 57 | + } |
| 58 | + api: { |
| 59 | + name: connections_azuremonitorlogs_name |
| 60 | + displayName: 'Azure Monitor Logs' |
| 61 | + description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.' |
| 62 | + iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png' |
| 63 | + brandColor: '#0072C6' |
| 64 | + id: '/subscriptions/<subscription_id_here>/providers/Microsoft.Web/locations/<region_here>/managedApis/${connections_azuremonitorlogs_name}' |
| 65 | + type: 'Microsoft.Web/locations/managedApis' |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +You can fix it by using the `subscriptionResourceId()` function: |
| 72 | + |
| 73 | +```bicep |
| 74 | +@description('description') |
| 75 | +param connections_azuremonitorlogs_name string |
| 76 | +
|
| 77 | +@description('description') |
| 78 | +param location string |
| 79 | +
|
| 80 | +@description('description') |
| 81 | +param resourceTags object |
| 82 | +param tenantId string |
| 83 | +
|
| 84 | +resource connections_azuremonitorlogs_name_resource 'Microsoft.Web/connections@2016-06-01' = { |
| 85 | + name: connections_azuremonitorlogs_name |
| 86 | + location: location |
| 87 | + tags: resourceTags |
| 88 | + properties: { |
| 89 | + displayName: 'azuremonitorlogs' |
| 90 | + statuses: [ |
| 91 | + { |
| 92 | + status: 'Connected' |
| 93 | + } |
| 94 | + ] |
| 95 | + nonSecretParameterValues: { |
| 96 | + 'token:TenantId': tenantId |
| 97 | + 'token:grantType': 'code' |
| 98 | + } |
| 99 | + api: { |
| 100 | + name: connections_azuremonitorlogs_name |
| 101 | + displayName: 'Azure Monitor Logs' |
| 102 | + description: 'Use this connector to query your Azure Monitor Logs across Log Analytics workspace and Application Insights component, to list or visualize results.' |
| 103 | + iconUri: 'https://connectoricons-prod.azureedge.net/releases/v1.0.1501/1.0.1501.2507/${connections_azuremonitorlogs_name}/icon.png' |
| 104 | + brandColor: '#0072C6' |
| 105 | + id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, connections_azuremonitorlogs_name) |
| 106 | + type: 'Microsoft.Web/locations/managedApis' |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Next steps |
| 113 | + |
| 114 | +For more information about the linter, see [Use Bicep linter](./linter.md). |
0 commit comments