terraform-provider-postgresql icon indicating copy to clipboard operation
terraform-provider-postgresql copied to clipboard

How to fix chicken-egg problem when re-creating Postgresql server

Open michaellihs opened this issue 4 years ago • 2 comments

Terraform Version

all versions, we are running on 0.13 currently

Affected Resource(s)

all resources

Terraform Configuration Files

provider "postgresql" {
  alias            = "pkg"
  host             = local.db_url
  port             = 5432
  username         = "${local.db_admin_username}@${local.db_server_name}"
  password         = azurerm_key_vault_secret.pkg_db_admin_password.value
  sslmode          = "require"
  superuser        = false
  expected_version = "11"
  connect_timeout  = 15
}

resource "postgresql_role" "service_role" {
  count = 0
  provider = postgresql.pkg
  name     = local.db_service_username
  login    = true
  password = azurerm_key_vault_secret.pkg_db_service_user_password.value

  depends_on = [azurerm_postgresql_server.pkg_db]
}

resource "postgresql_grant" "service_grant_pkg" {
  count = 0
  provider    = postgresql.pkg
  database    = local.db_name
  role        = postgresql_role.service_role[0].name
  schema      = "public"
  object_type = "table"
  privileges  = ["SELECT", "INSERT", "UPDATE"]

  depends_on = [azurerm_postgresql_server.pkg_db, azurerm_postgresql_database.pkg]
}

Debug Output

Error: retrieving PostgreSQL Database "DB NAME" (Server "SERVER NAME" / Resource Group "RESOURCE GROUP"): postgresql.DatabasesClient#Get: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="ServiceBusy" Message="Service is temporarily busy and the operation cannot be performed. Please try again later."

Error: dial tcp: lookup DB-NAME.postgres.database.azure.com on XXX.XXX.XXX.XXX:53: no such host

Expected Behavior

I would expect the postgresql not to connect to the DB if no resources should be changed / created. That way we could prevent the creation of resources when the DB server has not yet been provisioned. The only workaround for us at the moment is to comment out the code sections.

Actual Behavior

The provider seems to try to connect to the DB although there are no resources to be created.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform plan with the above code snippet and a DB server not yet existing

michaellihs avatar Jul 14 '21 08:07 michaellihs

@michaellihs Which version of the provider do you use?

This should work since v1.10, before this version the provider was indeed trying to connect to the database before it exists. Now, if there's no Postrgres to "refresh", i.e. at the first creation when state is empty, it will not try to connect.

cyrilgdn avatar Jul 15 '21 12:07 cyrilgdn

Thanks for the response - I forwarded it to the team, unfortunately I can't try it out anymore, since I rolled off the project...

michaellihs avatar Jul 26 '21 09:07 michaellihs

I allow myself to close this issue. Feel free to reopen if needed.

cyrilgdn avatar Jul 16 '23 08:07 cyrilgdn