【问题标题】:How to reference a parent resource name to a resource inside a module using bicep如何使用二头肌将父资源名称引用到模块内的资源
【发布时间】:2021-09-19 17:25:48
【问题描述】:

如何使用 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
  }
}

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

我对二头肌编码非常陌生。

【问题讨论】:

    标签: azure-rm azure-bicep


    【解决方案1】:

    找到此文档以了解更多详细信息:

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

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

    【讨论】:

    • 资源 devApim_resource 'Microsoft.ApiManagement/service@2021-01-01-preview' 现有 = { name: apimName }
    • 上面的注释语法是否也有助于在二头肌中获取资源定义,就像 terraform 导入一样,或者仅用于在所需资源之间进行引用?
    • 使用existing 关键字还可以让您检索资源属性
    • 好的,谢谢@Thomas。假设我有一个名为“examplestorage456”的 Azure 存储帐户。如何使用以下二头肌代码检索此资源属性?就像通过运行此命令或其他方式一样。 " az 部署组创建 -g test12 -f storage-account.bicep -c "
    • resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' 现有 = { name: 'examplestorage456' }
    猜你喜欢
    • 1970-01-01
    • 2021-10-11
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    相关资源
    最近更新 更多