如何创建通用 cosmos db terraform 模块以添加多个地理位置

Posted

技术标签:

【中文标题】如何创建通用 cosmos db terraform 模块以添加多个地理位置【英文标题】:how to create generic cosmos db terraform module to add multiple geo_locations 【发布时间】:2022-01-22 19:40:34 【问题描述】:

我正在尝试使用 terraform 为 azure cosmos db 创建一个模块。在我的示例中,我希望 geo_location 应该更灵活/自定义。这意味着我的故障转移位置不是我所有应用程序的标准。在其中一个应用程序中,我的主要位置是 WEU,但故障转移是 EUS。在其他应用程序中,主要是 EUS,但故障转移位置是 WEU、WUS2。等等...所以我想使用 1 个 cosmosdb 模块,并且 geo_location 属性应该更加面向自助服务,基础设施开发人员可以指定他们需要的任意数量的区域。

我看到在 terraform 中我们必须为每个区域指定“geo_location”块。这种方法将破坏拥有 1 个模块的目的。无论如何我可以像上面解释的那样让它更通用吗?

任何建议都有帮助。

谢谢, 桑托什

【问题讨论】:

【参考方案1】:

如果我正确理解您的要求,您想为 Cosmos DB 构建一个模块,其中将要求操作员提供任意数量的地理位置值,并且资源块将相应地创建 geo_location 块

在上述情况下,您可以创建一个列表类型的变量,这将要求用户提供值 相同,然后使用 dynamic geo_location block 以便对其进行相应配置。我已经使用以下代码进行了测试:

provider "azurerm" 
  features 

resource "azurerm_resource_group" "rg" 
  name     = "cosmos-dbtest"
  location = "East US"


variable "geo_location" 
    type = list
    description = "value for Geo Locations"


resource "random_integer" "ri" 
  min = 10000
  max = 99999


resource "azurerm_cosmosdb_account" "db" 
  name                = "tfex-cosmos-db-$random_integer.ri.result"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  offer_type          = "Standard"
  kind                = "MongoDB"

  enable_automatic_failover = true

  capabilities 
    name = "EnableAggregationPipeline"
  

  capabilities 
    name = "mongoEnableDocLevelTTL"
  

  capabilities 
    name = "MongoDBv3.4"
  

  capabilities 
    name = "EnableMongo"
  

  consistency_policy 
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  

  dynamic "geo_location" 
      for_each = var.geo_location
    content
    location          = geo_location.value
    failover_priority = geo_location.key
  


输出:

如果您想保持 第一个 geo_location 与 cosmos DB 的位置相同,然后是其他故障转移位置,那么 您可以使用一个静态和一个动态地理位置块,如下所示:

resource "azurerm_cosmosdb_account" "db" 
  name                = "tfex-cosmos-db-$random_integer.ri.result"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  offer_type          = "Standard"
  kind                = "MongoDB"

  enable_automatic_failover = true

  capabilities 
    name = "EnableAggregationPipeline"
  

  capabilities 
    name = "mongoEnableDocLevelTTL"
  

  capabilities 
    name = "MongoDBv3.4"
  

  capabilities 
    name = "EnableMongo"
  

  consistency_policy 
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  
  geo_location 
    location          = azurerm_resource_group.rg.location
    failover_priority = 0
  
  dynamic "geo_location" 
      for_each = var.geo_location
    content
    location          = geo_location.value
    failover_priority = "$geo_location.key + 1"
  


输出:

【讨论】:

感谢 AnsumanBal。我不知道 terraform 中的“动态”。这真的很有用。我会试试这个,让你知道。感谢您提出答案。 np,希望能帮上忙 :)

以上是关于如何创建通用 cosmos db terraform 模块以添加多个地理位置的主要内容,如果未能解决你的问题,请参考以下文章