你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

引用 Bicep 中的现有资源

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

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

同一范围

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

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

output blobEndpoint string = stg.properties.primaryEndpoints.blob

不同的范围

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

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

output blobEndpoint string = stg.properties.primaryEndpoints.blob

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

Troubleshooting

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

后续步骤

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