Leer en inglés

Compartir a través de

在 Bicep 中引用现有资源

若要引用当前 Bicep 文件中未部署的现有资源,请使用 existing 关键字声明资源。 请在部署需要从现有资源获取值的资源时,使用 existing 关键字。 可以通过其符号名称访问现有资源的属性。

资源在 existing 关键字引用它时不会再次部署。

同一范围

以下示例获取与当前部署相同的资源组中的现有存储帐户。 请注意,仅提供现有资源的名称。 这些属性通过符号名称提供。

resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' existing = {
  name: 'examplestorage'
}

output blobEndpoint string = stg.properties.primaryEndpoints.blob

不同的范围

设置属性 scope 以访问不同范围内的资源。 以下示例引用其他资源组中的现有存储帐户:

resource stg 'Microsoft.Storage/storageAccounts@2023-04-01' existing = {
  name: 'examplestorage'
  scope: resourceGroup(exampleRG)
}

output blobEndpoint string = stg.properties.primaryEndpoints.blob

有关设置范围的详细信息,请参阅 Bicep 的作用域函数

故障排除

如果尝试引用不存在的资源,则将出现 NotFound 错误,并且部署将失败。 检查要引用的资源的名称和范围。

后续步骤

有关部署资源的语法,请参阅 Bicep 中的资源声明