在 Terraform 中动态使用 Azure SQL 池 SKU 系列

Dynamic use of Azure SQL Pool SKU family in Terraform

我正在描述通过 Terraform 创建一个 Azure SQL 池。在一个环境中,我在另一个 vcore 上有一个标准池。

本节的主要区别

sku {
    capacity = var.capacity_sql_pool_dtu
    name = var.capacity_sql_pool_sku_name
    tier = var.capacity_sql_pool_tier
    family  = var.capacity_sql_pool_family
  }

family 值没有用于标准池,我如何根据环境使用或不使用它,以便我的脚本不会因错误而崩溃。谢谢

根据您的环境创建动态 sku 块,即 DTUvcore。您可以使用下面的 代码 ,其中您将在 变量中声明 sku 层级和容量 在本地块中我们将验证您的要求 然后 在弹性池资源块中使用.

代码:

Main.tf

provider "azurerm" {
  features{}
}
locals {
  vcore_tiers                 = ["GeneralPurpose", "BusinessCritical"]
  elastic_pool_vcore_family   = "Gen5"
  elastic_pool_vcore_sku_name = var.sku != null ? format("%s_%s", var.sku.tier == "GeneralPurpose" ? "GP" : "BC", local.elastic_pool_vcore_family) : null
  elastic_pool_dtu_sku_name   = var.sku != null ? format("%sPool", var.sku.tier) : null
  elastic_pool_sku = var.sku != null ? {
    name     = contains(local.vcore_tiers, var.sku.tier) ? local.elastic_pool_vcore_sku_name : local.elastic_pool_dtu_sku_name
    capacity = var.sku.capacity
    tier     = var.sku.tier
    family   = contains(local.vcore_tiers, var.sku.tier) ? local.elastic_pool_vcore_family : null
  } : null

}

data "azurerm_resource_group" "test" {
  name = "ansumantest"
}
resource "azurerm_sql_server" "server" {
  name = "ansumansqlserver"

  location            = data.azurerm_resource_group.test.location
  resource_group_name = data.azurerm_resource_group.test.name

  version                      = var.server_version
  administrator_login          = var.administrator_login
  administrator_login_password = var.administrator_password
}
resource "azurerm_mssql_elasticpool" "elastic_pool" {
  name  = "ansumansqlelastic-pool"

  location            = data.azurerm_resource_group.test.location
  resource_group_name = data.azurerm_resource_group.test.name

  server_name = azurerm_sql_server.server.name

  per_database_settings {
    max_capacity = coalesce(var.database_max_capacity, var.sku.capacity)
    min_capacity = var.database_min_capacity
  }

  max_size_gb    = var.elastic_pool_max_size
  zone_redundant = var.zone_redundant

  sku {
    capacity = local.elastic_pool_sku.capacity
    name     = local.elastic_pool_sku.name
    tier     = local.elastic_pool_sku.tier
    family   = local.elastic_pool_sku.family
  }
}

variable.tf

variable "server_version" {
  description = "Version of the SQL Server. Valid values are: 2.0 (for v11 server) and 12.0 (for v12 server). See https://www.terraform.io/docs/providers/azurerm/r/sql_server.html#version"
  type        = string
  default     = "12.0"
}

variable "administrator_login" {
  description = "Administrator login for SQL Server"
  type        = string
  default = "ansumanadmin"
}

variable "administrator_password" {
  description = "Administrator password for SQL Server"
  type        = string
  default = "Password@1234"
}

variable "elastic_pool_max_size" {
  description = "Maximum size of the Elastic Pool in gigabytes"
  type        = string
  default     = 756
}

variable "sku" {
  description = <<DESC
    SKU for the Elastic Pool with tier and eDTUs capacity. Premium tier with zone redundancy is mandatory for high availability.
    Possible values for tier are "GP_Gen5", "BC_Gen5" for vCore models and "Basic", "Standard", or "Premium" for DTU based models. Example {tier="Standard", capacity="50"}.
    See https://docs.microsoft.com/en-us/azure/sql-database/sql-database-dtu-resource-limits-elastic-pools"
DESC

  type = object({
    tier     = string,
    capacity = number,
  })
}

variable "zone_redundant" {
  description = "Whether or not the Elastic Pool is zone redundant, SKU tier must be Premium to use it. This is mandatory for high availability."
  type        = bool
  default     = false
}

variable "database_min_capacity" {
  description = "The minimum capacity (DTU or vCore) all databases are guaranteed in the Elastic Pool. Defaults to 0."
  type        = string
  default     = "0"
}

variable "database_max_capacity" {
  description = "The maximum capacity (DTU or vCore) any one database can consume in the Elastic Pool. Default to the max Elastic Pool capacity."
  type        = string
  default     = ""
}

输出:

如果我们为变量值提供 {tier="Standard",capacity=50} 那么我们会得到以下输出:

如果我们为变量值提供 {tier="GeneralPurpose",capacity=40} 那么我们会得到以下输出: