I am trying to create users with names same as that of the DB in postgres flexible server using terraform. The user creation code is as below.
resource "azurerm_postgresql_flexible_server_configuration" "create_users" {
name = "create_users_script"
server_id = azurerm_postgresql_flexible_server.example.id
count = length(var.db_names)
value = <<-EOT
CREATE USER ${var.db_names[count.index]} WITH PASSWORD 'password1';
EOT
}
Everything seems good with terraform plan, with it showing the names of users to be created like:
# module.eng_dev_pg.azurerm_postgresql_flexible_server_configuration.create_users[0] will be created
+ resource "azurerm_postgresql_flexible_server_configuration" "create_users" {
+ id = (known after apply)
+ name = "create_users_script"
+ server_id = "/subscriptions/XXXXX/resourceGroups/rsg-01/providers/Microsoft.DBforPostgreSQL/flexibleServers/pg-02"
+ value = <<-EOT
CREATE USER keycloak WITH PASSWORD 'password1';
EOT
}
But while terraform apply, it errors out saying:
Error: updating Configuration (Subscription: "XXXXX" Resource Group Name: "rsg-01" Flexible Server Name: "pg-02" Configuration Name: "create_users_script"): performing Update: unexpected status 400 with error: InvalidParameterValue: Invalid value given for parameter serverParameterValue. Specify a valid parameter value. │ with module.eng_dev_pg.azurerm_postgresql_flexible_server_configuration.create_users[0], on ../../modules/postgres/public-postgres-flexible.tf line 43, in resource "azurerm_postgresql_flexible_server_configuration" "create_users": 43: resource "azurerm_postgresql_flexible_server_configuration" "create_users" {
I tried a bit of like using for_each, but that's of no use as the current code is polling the required usernames properly as expected.
Expectation is to get the users created; about 12 users to be created. Error is the same for all user creations.
As the error says Invalid value given for parameter serverParameterValue I am looking for a way to specify the serverParameterValue.
NOTE: The db_names (user names) are provided from a module as:
db_names = ["keycloak", "provisioner", "account-development", "customer1-account-development", "customer1-file-development", "customer1-keycloak-development", "customer1-metadata-development", "entity-development", "file-development", "keycloak-development", "provisioner-development", "metadata-development"]
}
As the
postgresql flexible server configurationmodule is failing during the execution ofterraform applyfor creating users inPostgreSQLflexible database.Alternatively, you can use
Azure CLIcommands within a null resource in Terraform.Here is the updated code to create multiple users in a
PostgreSQL flexibleserver database.Terraform Apply:
To check all users in a
PostgreSQL database, Hers is the query.After running the above
Terraformcode, all the users specified in the script have been created in the databaseReference: az postgres flexible-server | Microsoft Learn