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

How to reference a parent resource name to a resource inside a module using bicep

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

以下 main.bicep 文件代码有效。

# main.bicep

param apimName string = 'devApim'
param apimLocation string = 'eastus'
param publisherName string = 'danny'
param publisherEmail string = 'danny@gmail.com'

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 = 'danny@gmail.com'

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
  }
}

现在我如何 reference/pass 父资源 'devApim_resource'(第一个资源)进入模块资源 test_api_vs_v1(第二个资源),因为使用父资源:devApim_resource 在测试-api.bicep 模块文件

我对二头肌编码还很陌生。

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

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

param apimName string

更简单的解决方案是使用父资源名称构建子资源名称:

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

或者您可以像这样引用现有资源:

// 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 中,你可以这样称呼你 chil 模块:

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     
  }  
}