如何使用 bicep 将父资源名称引用到模块内的资源

2024-03-10

如何使用 Microsoft bicep 代码将父资源名称引用到模块内的资源。

下面的 main.bicep 文件代码正在运行。

# main.bicep

param apimName string = 'devApim'
param apimLocation string = 'eastus'
param publisherName string = 'danny'
param publisherEmail string = '[email protected] /cdn-cgi/l/email-protection'

param api_display_name string = 'Test Consumer API'
param api_description         = 'Test API description'
param api_versioningScheme    = 'Segment'

resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' = {
  name: apimName
  location: apimLocation
  sku: {
    name: 'Developer'
    capacity: 1
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
}


resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
// Below reference to first/parent resource is working fine as it's in the same bicep file.
  parent: devApim_resource
  name: 'test_api_vs_name'
  properties: {
    displayName: api_display_name
    description: api_description
    versioningScheme: api_versioningScheme
  }
}

我想将此 main.bicep 第二个资源(VersionSet 资源)修改为如下文件所示的模块。

# main.bicep

param apimName string = 'devApim'
param apimLocation string = 'eastus'
param publisherName string = 'danny'
param publisherEmail string = '[email protected] /cdn-cgi/l/email-protection'

param api_display_name string = 'Test Consumer API'
param api_description         = 'Test API description'
param api_versioningScheme    = 'Segment'


resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' = {
  name: apimName
  location: apimLocation
  sku: {
    name: 'Developer'
    capacity: 1
  }
  properties: {
    publisherEmail: publisherEmail
    publisherName: publisherName
  }
}

module test_api_module 'test-api.bicep' = {
  name: 'test_api'
  params: {
    api_display_name: api_display_name
    api_description: api_description
    api_versioningScheme: api_versioningScheme
     
  }
  
}

# test-api.bicep file

param api_display_name string 
param api_description  string
param api_versioningScheme string 

resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
  // Below reference to first/parent resource is not working.
  //parent: devApim_resource 

  name: 'test_api_vs_name'
  properties: {
    displayName: api_display_name
    description: api_description
    versioningScheme: api_versioningScheme
  }
}

现在,如何将父资源“devApim_resource”(第一个资源)引用/传递到模块资源 test_api_vs_v1(第二个资源)中,因为使用父资源:devApim_resource 在 test-api.bicep 模块文件中不起作用

我对二头肌编码很陌生。


找到此文档以获取更多详细信息:

  • 参考现有资源 https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/resource-declaration?tabs=azure-powershell#reference-existing-resources

您需要将父资源名称添加为子模块中的参数:

param apimName string

然后您可以像这样引用现有资源:

// Reference to the parent resource
resource devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' existing = {
  name: apimName
}

resource test_api_vs_v1 'Microsoft.ApiManagement/service/apiVersionSets@2021-01-01-preview' = {
  parent: devApim_resource 
  name: 'test_api_vs_name'
  ...
}

然后在你的main.bicep,你可以这样称呼你的子模块:

module test_api_module 'test-api.bicep' = {
  name: 'test_api'
  params: {
    apimName: devApim_resource.name
    api_display_name: api_display_name
    api_description: api_description
    api_versioningScheme: api_versioningScheme     
  }  
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 bicep 将父资源名称引用到模块内的资源 的相关文章

随机推荐