二头肌名称属性和模块

jok*_*arl 3 azure-resource-manager azure-rm-template azure-bicep

我很难理解name二头肌文件中丰富的属性。main.bicep我已经设置了自己的一些模块,在调用其中之一时看起来像这样:

module vas_resource_group '../common/resource_group.bicep' = {
  name: 'vas_resource_group'
  params: {
    runningNumber: runningNumber
    descriptionShort: descriptionShort
    function: function
    regionShort: regionShort
    environment: environment
    location: defaultLocation
  }
}
Run Code Online (Sandbox Code Playgroud)

有问题的模块如下所示:

resource resource_group 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: 'rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}'
  location: location
}

output name string = resource_group.name
Run Code Online (Sandbox Code Playgroud)

现在,main.bicep我想知道模块中声明的名称,例如我想知道模块决定将我的输入参数串在一起rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}

在 中main.bicep,如果我写下这篇文章,我确信我会得到我想要的东西:

vas_resource_group.outputs.name
Run Code Online (Sandbox Code Playgroud)

但这有一个缺点,那就是它不能在任何地方使用,例如这是不允许的:

scope: resourceGroup(vas_resource_group.outputs.name)
Run Code Online (Sandbox Code Playgroud)

并显示错误:

该表达式用于对“模块”类型的“范围”属性的赋值,这需要一个可以在部署开始时计算的值。可以在开始时计算的 vas_resource_group 的属性包括“name”.bicep(BCP120)

因此,由于组成名称的所有构建块从一开始就已知,因此我不需要先创建资源,以便可以在部署之前计算名称。

然后,我选择引用资源组的名称,如下所示:

scope: resourceGroup(vas_resource_group.name)
Run Code Online (Sandbox Code Playgroud)

错误消失了,但我不知道这是否会导致声明的名称main.psvas_resource_group(无效),或者是否导致模块声明的名称为rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}.

它是哪一个,我应该如何考虑 name 属性?它有时似乎代表正在创建的资源的实际名称,而在其他时候(例如在调用模块中)根本没有多大用处。

如果是当前文件中的名称,如何以最佳方式获取模块中声明的名称?

Str*_*low 5

naming.bicep如果您不想在 PowerShell 中定义命名模式,也许可以使用模板。我当前的方法是使用共享 PowerShell 脚本来定义所有订阅、资源组和资源名称。过去,我使用过模板,效果很好,但部署确实需要更多时间。

以下与我使用命名模板的方式类似。这还意味着您必须将资源组和资源(例如存储帐户)模块部署埋入嵌套更深的一层。

主二头肌

targetScope = 'subscription'

var location = 'East US 2'

module naming 'naming.bicep' = {
  name: 'naming'
  params: {
    descriptionShort: 'app'
    environment: 'n'
    function: 't'
    regionShort: 'use2' 
    runningNumber: 'aa'
  }
}

module resource_group 'resource_group.bicep' = {
  name: 'resource_group'
  params: {
    name: naming.outputs.resourceGroupName
    location: location
    resourceSuffix: naming.outputs.resourceNameSuffix
  }
}
Run Code Online (Sandbox Code Playgroud)

命名.bicep

targetScope = 'subscription'

param runningNumber string
param descriptionShort string
param function string
param regionShort string
param environment string

output resourceGroupName string = 'rg-${runningNumber}-${descriptionShort}-${function}-${regionShort}-${environment}'
output resourceNameSuffix string = '${runningNumber}${descriptionShort}${function}${regionShort}${environment}'
Run Code Online (Sandbox Code Playgroud)

资源组.bicep

targetScope = 'subscription'

param name string
param location string
param resourceSuffix string

resource resource_group 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: name
  location: location
}

module storage_account 'storage_account.bicep' = {
  scope: resourceGroup(name)
  name: 'storage_account'
  params: {
    location: location
    resourceSuffix: resourceSuffix
  }
  dependsOn: [
    resource_group
  ]
}
Run Code Online (Sandbox Code Playgroud)

storage_account.bicep

param resourceSuffix string
param location string

resource storage_account 'Microsoft.Storage/storageAccounts@2021-09-01' = {
  name: 'sa${resourceSuffix}'
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
}
Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述