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
azurerm_mysql_server
. – Marcin