繁体   English   中英

Terraform Azure 模块?

[英]Terraform Azure Module?

我正在尝试模块化 terraform Azure MySQL-server 以使其可重用,但不知何故语法不会引发错误,我正在学习 Z1B1ED905D54C18E3DD8828986C14BE17,如果有人可以帮助我,那会很棒。

子模块:

  resource "azurerm_mysql_server" "mysql-server" {
  for_each = var.mysql
  location            = var.location
  resource_group_name = var.resource_group_name
  
  sku = {
    name   = lookup(each.value, "sku_name")
    capacity = lookup(each.value, "capacity")
    tier = lookup(each.value,"tier")
    family = lookup(each.value, "family")
  }

  storage_profile = {
    storage_mb = lookup(each.value, "storage_mb")
    backup_retention_days = lookup(each.value, "backup_retention_days")
    geo_redundant_backup = lookup(each.value, "geo_redundant_backup")
  }

  name                         = lookup(each.value, "name")
  administrator_login          = lookup(each.value, "administrator_login")
  administrator_login_password = lookup(each.value, "administrator_login_password")

  auto_grow_enabled                 = lookup(each.value, "auto_grow_enabled")
  infrastructure_encryption_enabled = lookup(each.value, "infrastructure_encryption_enabled")
  public_network_access_enabled     = var.public_network_access_enabled
  ssl_enforcement          = lookup(each.value, "ssl_enforcement")
  version = lookup(each.value, "server_version")
}

变量.tf 文件:

variable resource_group_name {
  description = "The name of the resource group in which to create the MySQL Server. Changing this forces a new resource to be created."
  default = ""
}

variable location {
  description = "Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created."
  default = ""
}

variable public_network_access_enabled {
  default = ""
}


variable "mysql" {
  description = "Map of resource references"
  type = map(object({
    name = string
    administrator_login = string
    administrator_login_password = string
    server_version = string

    sku_name   = string
    capacity = string
    tier = string
    family = string

    storage_mb = string
    backup_retention_days = string
    geo_redundant_backup = string

    ssl_enforcement = string
    infrastructure_encryption_enabled = bool
    auto_grow_enabled = bool
  }))
  default     = {}
}

这是我的调用模块:

module mysql-server {
    source = "/Users/sam/workdir/mysql-module"
    resource_group_name    = var.resource_group_name
    location               = var.location
    public_network_access_enabled = var.public_network_access_enabled
    
}

变量.tf 文件:

variable resource_group_name {
  description = "The name of the resource group in which to create the MySQL Server. Changing this forces a new resource to be created."
  default = ""
}

variable location {
  description = "Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created."
  default = ""
}

variable public_network_access_enabled {
  default = ""
}

variable "mysql" {
  description = "Map of resource references"
  type = map(object({
    name = string
    administrator_login = string
    administrator_login_password = string
    server_version = string
    
    sku_name   = string
    capacity = string
    tier = string
    family = string

    storage_mb = string
    backup_retention_days = string
    geo_redundant_backup = string

    ssl_enforcement = string
    infrastructure_encryption_enabled = bool
    auto_grow_enabled = bool
  }))
  default     = {}
}

tfvars 文件:

location = "eastus2"
resource_group_name = "test"
public_network_access_enabled = "Disabled"

mysql = {
    server1 = {
        name = "testingmysql-server01"
        administrator_login = "mysqladmin01"
        administrator_login_password = "H@Sh1CoR3" 
        server_version = "5.7"
        
        sku_name = "B_Gen4_2"
        capacity = "2"
        tier = "Basic"
        family = "Gen4"
        
        storage_mb = "5120"
        backup_retention_days = "7"
        geo_redundant_backup = "Disabled"

        ssl_enforcement = "Enabled"
        public_network_access_enabled = false
        infrastructure_encryption_enabled = false
        auto_grow_enabled = true

    }
}

错误:


Error: Missing required argument

  on .terraform/modules/mysql-server/main.tf line 1, in resource "azurerm_mysql_server" "mysql-server":
   1: resource "azurerm_mysql_server" "mysql-server" {

The argument "sku_name" is required, but no definition was found.


Error: Unsupported argument

  on .terraform/modules/mysql-server/main.tf line 6, in resource "azurerm_mysql_server" "mysql-server":
   6:   sku = {

An argument named "sku" is not expected here.


Error: Unsupported argument

  on .terraform/modules/mysql-server/main.tf line 13, in resource "azurerm_mysql_server" "mysql-server":
  13:   storage_profile = {

An argument named "storage_profile" is not expected here. Did you mean to
define a block of type "storage_profile"?


Error: Unsupported argument

  on .terraform/modules/mysql-server/main.tf line 23, in resource "azurerm_mysql_server" "mysql-server":
  23:   auto_grow_enabled                 = lookup(each.value, "auto_grow_enabled")

我将本文档作为参考, https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_server

错误说这些不是这里所期望的,但文件说这些是必需的值,所以不知道如何正确。

如果有人可以提供帮助,那将很有帮助。

谢谢

从该参数参考terraform azurerm 版本2.44.0 ,您应该使用参数sku_name定义 SKU 的名称,遵循tier + family + cores模式(例如 B_Gen4_1、GP_Gen5_8)。 此外,基本层不支持geo_redundant_backup_enabled

在这种情况下,如果您使用的是最新的 terraform 版本,则可以删除sku块而不是添加sku_name = each.value.sku_name 此外, storage_profile在那里是一个无效参数,需要删除。

这是我这边的一个工作示例:

tfvars 文件:

location = "eastus"
resource_group_name = "xxxx"

mysql = {
    server1 = {
        name = "qqqsql-server01"
        administrator_login = "mysqladmin01"
        administrator_login_password = "H@Sh1CoR3" 
        server_version = "5.7"
        
        sku_name = "B_Gen5_2"
        # capacity = "2"
        # tier = "Basic"
        # family = "Gen4"
        
        storage_mb = "5120"
        version = "8.0"
        backup_retention_days = "7"
        # geo_redundant_backup = "Disabled"  # This is not supported for the Basic tier.

        ssl_enforcement = true
        public_network_access_enabled = false
        infrastructure_encryption_enabled = false
        auto_grow_enabled = true
        public_network_access_enabled = true 

    }
}

变量文件和调用模块:

variable resource_group_name {
  description = "The name of the resource group in which to create the MySQL Server. Changing this forces a new resource to be created."
  default = ""
}

variable location {
  description = "Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created."
  default = ""
}

variable "mysql" {
  description = "Map of resource references"
  type = map(object({
    name = string
    administrator_login = string
    administrator_login_password = string
    version = string

    sku_name   = string

    storage_mb = string
    backup_retention_days = string

    ssl_enforcement = string
    infrastructure_encryption_enabled = bool
    auto_grow_enabled = bool
    public_network_access_enabled = bool
  }))
  default     = {}
}

module mysql-server {
    source = "./modules/sql"
    resource_group_name    = var.resource_group_name
    location               = var.location
    mysql    = var.mysql
    
}

子模块:

 resource "azurerm_mysql_server" "mysql-server" {
  for_each = var.mysql
  name                = each.value.name
  location            = var.location
  resource_group_name = var.resource_group_name
  
  sku_name = each.value.sku_name

  administrator_login          = each.value.administrator_login
  administrator_login_password = each.value.administrator_login_password
 
  storage_mb = each.value.storage_mb
  version    = each.value.version

  auto_grow_enabled                 = each.value.auto_grow_enabled
  backup_retention_days             = each.value.backup_retention_days
  infrastructure_encryption_enabled = each.value.infrastructure_encryption_enabled
  public_network_access_enabled     = each.value.public_network_access_enabled
  ssl_enforcement_enabled           = each.value.ssl_enforcement

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM