Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

Terraform Azure Module?

I'm trying to modularized the terraform Azure MySQL-server to make it reusable but somehow the syntax is not throwing errors, i'm learning the terraform so if someone can help me, that would be great.

child module:

  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")
}

variabels.tf file:

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     = {}
}

And this is my calling module:

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
    
}

variables.tf file:

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 file:

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:


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")

I'm following this document as a reference, https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mysql_server

Error says those are not expected here, but document says those are required values so not sure how to get it right.

if anyone can help, that would be helpful.

Thanks

question from:https://stackoverflow.com/questions/65877476/terraform-azure-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From that argument Reference with terraform azurerm version 2.44.0, you should define the name of the SKU with argument sku_name, follows the tier + family + cores pattern (e.g. B_Gen4_1, GP_Gen5_8). Also, The geo_redundant_backup_enabled is not supported for the Basic tier.

In this case, if you are using the latest terraform version, you could remove the sku block instead add sku_name = each.value.sku_name. also, the storage_profile is an invalid argument there and needs to be removed.

Here is a working sample on my side:

tfvars file:

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 file and calling module:

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
    
}

Child module:

 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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...