【问题标题】:How do I reference a resource from a module in Azure Bicep?如何从 Azure Bicep 中的模块引用资源?
【发布时间】:2022-08-14 04:17:54
【问题描述】:

我有一个相当简单的 Bicep 脚本来创建 Cosmos 数据库以及其中的容器:

resource cosmos_db_live \'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2022-05-15\' = {
    parent: cosmos_account
    name: \'live\'
    properties: {
        resource: {
            id: \'live\'
        }
        options: {
            throughput: 600
        }
    }
}

resource cosmos_container \'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2022-05-15\' = {
    parent: cosmos_db_live
    name: \'container_name\'
    properties: {
        resource: {
            id: \'container_name\'
            partitionKey: {
                paths: [\'/partition\']
            }
            conflictResolutionPolicy: {
                mode: \'LastWriterWins\'
                conflictResolutionPath: \'/_ts\'
            }
            indexingPolicy: {
                indexingMode: \'consistent\'
                automatic: true
                includedPaths: [{path: \'/*\'}]
                excludedPaths: [{path: \'/\"_etag\"/?\'}]
            }
        }
    }
}

这很好用。但是,我现在想创建多个具有相同结构的容器,因此我尝试将容器定义模板化到一个模块中:

param name string
param partition string

resource cosmos_container \'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2022-05-15\' = {
    name: name
    properties: {
        resource: {
            id: name
            partitionKey: {
                paths: [\'/${partition}\']
            }
            conflictResolutionPolicy: {
                mode: \'LastWriterWins\'
                conflictResolutionPath: \'/_ts\'
            }
            indexingPolicy: {
                indexingMode: \'consistent\'
                automatic: true
                includedPaths: [{path: \'/*\'}]
                excludedPaths: [{path: \'/\"_etag\"/?\'}]
            }
        }
    }
}

我现在不知道如何将它链接回父级。我不能在模块中使用parent: ,因为我找不到通过顶层文件将数据库资源传递到模块的方法。我不能在模块调用中使用parent: ,因为它不是有效的操作。我不能从父资源中调用模块,因为它不是有效的语法。

如何从我的父文件中调用上述模块并自动解析依赖项,就好像它们都在一个文件中一样?这不支持吗?应该有一个非常基本的方法来做到这一点(除非我遗漏了什么)。

    标签: azure azure-cosmosdb infrastructure-as-code azure-bicep


    【解决方案1】:

    这在二头肌中得到支持。这是一个示例,它在每个容器上执行名称和分区键以及不同的吞吐量值。

    [Bicep Quickstart Loops] (https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/quickstart-loops?tabs=CLI) 中的更多示例。

    param accountName string = 'my-cosmos-account'
    param location string = 'West US'
    param primaryRegion string = 'West US'
    param databaseName string = 'myDatabase'
    
    param containersConfig object = {
      container1: {
        name: 'myContainer1'
        partitionKey: '/myPartitionKey1'
        throughput: 400
      }
      container2: {
        name: 'myContainer2'
        partitionKey: '/myPartitionKey2'
        throughput: 500
      }
      container3: {
        name: 'myContainer3'
        partitionKey: '/myPartitionKey3'
        throughput: 600
      }
    }
    
    var locations = [
      {
        locationName: primaryRegion
        failoverPriority: 0
        isZoneRedundant: false
      }
    ]
    
    resource account 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
      name: toLower(accountName)
      location: location
      kind: 'GlobalDocumentDB'
      properties: {
        consistencyPolicy: { defaultConsistencyLevel: 'Session'}
        locations: locations
        databaseAccountOfferType: 'Standard'
      }
    }
    
    resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2022-05-15' = {
      parent: account
      name: databaseName
      properties: {
        resource: {
          id: databaseName
        }
      }
    }
    
    resource containers 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2022-05-15' = [for containerConfig in items(containersConfig): {
      parent: database
      name: '${containerConfig.value.name}'
      properties: {
        resource: {
          id: '${containerConfig.value.name}'
          partitionKey: {
            paths: [
              '${containerConfig.value.partitionKey}'
            ]
            kind: 'Hash'
          }
          indexingPolicy: {
            indexingMode: 'consistent'
            includedPaths: [
              {
                path: '/*'
              }
            ]
            excludedPaths: [
              {
                path: '/_etag/?'
              }
            ]
          }
        }
        options: {
          throughput: containerConfig.value.throughput
        }
      }
    }]
    

    PS:我注意到您正在使用数据库吞吐量。如果容器具有高度不对称的请求和存储需求,我建议不要与容器共享数据库吞吐量。当容器都大致相等时很好。对于那些不是,给他们自己的吞吐量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 2020-07-09
      相关资源
      最近更新 更多