For a new deployment I want to create databases, users and grants on a previous created MySQL database on azure.
The following code used inside a module creates the environment as expected and I still struggle how to create the outputs of the module and so I get something like the following, to give it to the operations team:
module.test_cluster.mysql_users[cat].password = randompassword1
module.test_cluster.mysql_users[dog].password = randompassword2
Code example:
mysql_databases = [
"foo",
"bar"
]
mysql_users = [
"stage-cat",
"stage-dog",
"stage-snake"
]
mysql_grants = {
"cat-0" = {
name = "stage-cat"
database = "foo"
grant = ["ALL"]
},
"cat-1" = {
name = "stage-cat"
database = "bar"
grant = ["SELECT", "EXECUTE", "SHOW VIEW"]
},
"dog-0" = {
name = "stage-dog"
database = "bar"
grant = ["ALL"]
}
}
resource "mysql_database" "test" {
for_each = var.mysql_databases
name = each.key
}
resource "mysql_user" "test" {
for_each = var.mysql_users
user = each.value
host = "%"
tls_option = "SSL"
plaintext_password = random_password.test[each.value].result
}
resource "mysql_grant" "test" {
for_each = var.mysql_grants
user = mysql_user.test[each.value["name"]].user
host = "%"
database = each.value["database"]
privileges = each.value["grant"]
}
resource "random_password" "test" {
for_each = var.mysql_users
length = 32
special = false
}