Attempts to map the result from the SQL query. This seems correct to me, but terraform returns an error.
It does not return any blank result for IP and server name.
Is there a bug somewhere? It seems correct to me.
I have main.tf
/*
* admin database
*/
module "sql-servers-admindb" {
source = "../terraform-modules/terraform-data-db"
dbtype = "mysql"
connection = {
mysql = {
host = local.mysql-admin_credentials.dbhost
user = local.mysql-admin_credentials.dbuser
pass = local.mysql-admin_credentials.dbpass
dbase = local.mysql-admin_credentials.dbname
}
sqlite = null
}
debugfile = "debug.json"
query = {
sqlquery = <<EOT
SELECT sd.serwer_id, sd.ip_local, sd.host_mysql
FROM serwer_panel sd
JOIN serwer_serwer_grupa ssg ON ssg.serwer_id = sd.serwer_id
JOIN serwer_grupa sg ON ssg.serwer_grupa_id = sg.id
EOT
}
}
module "terraform-sql" {
source = "../terraform-modules/terraform-sql"
depends_on = [module.sql-servers-admindb]
for_each = { for sql in module.sql-servers-admindb.result.sqlquery : sql.serwer_id => sql }
general = {
zone = "sql.${var.dns_local_name[local.environment]}."
ptr_networks = "${var.dns_local_reverse_allowed[local.environment]}"
}
}
terraform-modules/terraform-sql/main.tf
// IP address and A record
module "sql-nsrecord_a" {
source = "../terraform-modules/terraform-generic-nsrecord
zone = "{var.general.zone}."
name = "${var.sql.serwer_id}."
type = "A"
records = [var.sql.local_ip]
ptr_networks = var.general.ptr_networks
}
/*
* General
*/
variable "general" {
type = object({
zone = string
ptr_networks = list(any)
})
default = {
zone = ""
ptr_networks = []
}
}
/*
* SQL records
*/
variable "sql" {
type = map(any)
default = {}
}
Example debug.json
Array
(
[sqlquery] => Array
(
[0] => Array
(
[serwer_id] => s12
[ip_local] => 127.0.0.1
[host_mysql] =>
)
Return error:
│ Error: Missing map element
│
│ on ../terraform-modules/terraform-sql/main.tf line 9, in module "sql-nsrecord_a":
│ 9: name = "${var.sql.serwer_id}."
│ ├────────────────
│ │ var.sql is empty map of dynamic
│
│ This map does not have an element with the key "serwer_id".
╵
╷
│ Error: Missing map element
│
│ on ../terraform-modules/terraform-sql/main.tf line 11, in module "sql-nsrecord_a":
│ 11: records = [var.sql.local_ip]
│ ├────────────────
│ │ var.sql is empty map of dynamic
│
I am asking for advice on how to improve it.
>Solution :
You don’t seem to be passing the variable sql to the terraform-sql module and thus its default value is used:
default = {}
Hence an empty map. No wonder it doesn’t contain some entry.
You need to amend your
module "terraform-sql" {
...
}
to pass the sql argument too.