【问题标题】:Terraform create an app service plan after every x app serviceTerraform 在每个 x 应用服务之后创建一个应用服务计划
【发布时间】:2020-07-10 13:20:56
【问题描述】:

我利用 Terraform Azure 提供程序将大量 webapp 部署到 appservice 计划中。

webapps 列表是这样设置的:

applist = [
"app1",
"app2",
"app3",
"app4",
"app5",
"app6",
"app7",
"app8",
"app9",
"app10",
"app11",
"app12",
"app13",
"app14"
]

是否可以使用 terraform 在每 5 个应用后创建一个新的应用服务计划?

我应该得到这样的结果:

appserviceplan01(
"app1",
"app2",
"app3",
"app4",
"app5"
)
appserviceplan02(
"app6",
"app7",
"app8",
"app9",
"app10"
)
appserviceplan03(
"app11",
"app12",
"app13",
"app14
)

【问题讨论】:

  • 更新了我的问题以更好地解释它

标签: terraform terraform-provider-azure


【解决方案1】:

您可以将列表声明为地图,然后使用 for-Each 循环通过在键上除以 5 来创建计划资源

这样的……

variable "app_service_object" {  
  default = {
1 ="app1"
2 = "app2"
3 = "app3"
4 = "app4"
5 = "app5"
6 = "app6"
7 = "app7"
 } 
  
}

resource "azurerm_resource_group" "game" {
  name     = var.resource_group_name
  location = var.resource_group_location
}



resource "azurerm_app_service_plan" "game" {
  for_each            = tomap(var.app_service_object)  
  name                = "${var.app_service_plan_name}-${floor(tonumber(each.key) / 5)}"
  location            = azurerm_resource_group.game.location
  resource_group_name = azurerm_resource_group.game.name
  kind                = "Linux"
  reserved            = true

  sku {
    tier = "Basic"
    size = "B1"
  }
}

resource "azurerm_app_service" "game" {
   for_each            = tomap(var.app_service_object)   

   name                = "${var.app_service_name_prefix}-${each.value}"
   location            = azurerm_resource_group.game.location
   resource_group_name = azurerm_resource_group.game.name
   app_service_plan_id = azurerm_app_service_plan.game[tostring(each.key)].id

  site_config {
   // your site config
  }

}

【讨论】:

  • 感谢您的努力。它开始 ceating 很多应用服务计划,然后停止重复?
  • 必须在应用服务之前创建计划。查看 terraform plan 命令结果,您会发现每 5 个应用计划的名称都相同
  • 您的代码很可靠,只有在我尝试将其调整到我的特定场景时才会中断
  • 找到了,如果列表更长,部署中断
【解决方案2】:

在此页面上找到了解决方案: https://azurecitadel.com/automation/terraform/lab4/

包含块列表的本地人创建最多包含 20 个应用程序的组, 输出用于创建带有 count 的 appservice 计划,并使用 element() 将应用分布在 appservice 计划中

variable "app_service_object" {
  default = [
    "app1",
    "app2",
    "app3",
    "app4",
    "app5",
    "app6",
    "app7",
    "app8",
    "app9",
    "app10",
    "app11",
    "app12",
    "app13",
    "app14",
     ...
    "app100"
  ]
}

resource "azurerm_resource_group" "game" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

locals {
  webappsperloc = [
    for name in chunklist(var.app_service_object, 20) : name
  ]
}

resource "azurerm_app_service_plan" "game" {
  count               = length(local.webappsperloc)
  name                = "${var.app_service_plan_name}${count.index}"
  location            = azurerm_resource_group.game.location
  resource_group_name = azurerm_resource_group.game.name

  sku {
    tier = "Basic"
    size = "B1"
  }
}

resource "azurerm_app_service" "game" {
  count               = length(var.app_service_object)
  name                = "${var.app_service_object[count.index]}-azure-web"
  location            = azurerm_resource_group.game.location
  resource_group_name = azurerm_resource_group.game.name
  app_service_plan_id = element(azurerm_app_service_plan.game.*.id, count.index)

  site_config {
    // your site config
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2020-05-25
    • 2016-04-15
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多