digitalocean / terraform-provider-digitalocean

Terraform DigitalOcean provider
https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs
Mozilla Public License 2.0
503 stars 270 forks source link

Creating PostgreSQL DBs and users still fails randomly #493

Open gfmio opened 4 years ago

gfmio commented 4 years ago

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.

Terraform v0.13.3

terraform {
  required_providers {
    digitalocean = {
      source  = "digitalocean/digitalocean"
      version = "~> 1.22.2"
    }
  }
}

Affected Resource(s)

Please list the resources as a list, for example:

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.
# App DB

locals {
  db_name = "app"
  db_user = "app"
}

# DB cluster

resource "digitalocean_database_cluster" "app_db_cluster" {
  name                 = "${var.name}-app-db-cluster"
  engine               = "pg"
  version              = "12"
  size                 = "db-s-1vcpu-1gb"
  region               = var.region
  node_count           = 1
  private_network_uuid = digitalocean_vpc.vpc.id
}

# DB

resource "digitalocean_database_db" "app_db" {
  cluster_id = digitalocean_database_cluster.app_db_cluster.id
  name       = local.db_name
}

# DB user

resource "digitalocean_database_user" "db_user" {
  cluster_id = digitalocean_database_cluster.app_db_cluster.id
  name       = local.db_user
}

Debug Output

Please provider a link to a GitHub Gist containing the complete debug output: https://www.terraform.io/docs/internals/debugging.html. Please do NOT paste the debug output in the issue; just paste a link to the Gist.

When the resources get created, I sometimes get the following error:

Error: Provider produced inconsistent result after apply

When applying changes to digitalocean_database_user.db_user, provider
"registry.terraform.io/-/digitalocean" produced an unexpected new value for
was present, but now absent.

This is a bug in the provider, which should be reported in the provider's own
issue tracker.

Panic Output

If Terraform produced a panic, please provide a link to a GitHub Gist containing the output of the crash.log.

Expected Behavior

What should have happened?

The resources get created without fail.

Actual Behavior

What actually happened?

The resource creation sometimes throws the above error. I think this might be some kind of race condition? Because when I check manually in the UI, everything has been created and looks fine.

Steps to Reproduce

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

  1. terraform apply

Important Factoids

Are there anything atypical about your accounts that we should know? For example: Running in EC2 Classic? Custom version of OpenStack? Tight ACLs?

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

adelmoradian commented 3 years ago

same issue when creating a mysql cluster and db users. I'm using provider version 2.3.0 and terraform 0.13.5

indrif commented 2 years ago

Having similar errors when creating/ deleting databases and users within an existing cluster. Any update on this?

cikupin commented 2 years ago

It is still happening to me when I create MongoDB database users using list and for_each.

I use provider 2.19.0, terraform 1.1.4, and terragrunt 0.36.0.

c-w commented 2 years ago

Happening here too for Postgres DBs on provider v2.21.0 terraform v1.1.9.

Nuxij commented 2 years ago

I just started getting this issue randomly..

resource "digitalocean_database_cluster" "saas" {
    name                 = var.cluster_name
    engine               = "mysql"
    version              = "8"
    size                 = "db-s-1vcpu-1gb"
    region               = var.region
    node_count           = 1
    private_network_uuid = digitalocean_vpc.saas.id
}

resource "digitalocean_database_user" "saas" {
  cluster_id = digitalocean_database_cluster.saas.id
  name       = "saas-user"
  mysql_auth_plugin = "mysql_native_password"
}

resource "digitalocean_database_db" "saas" {
  cluster_id = digitalocean_database_cluster.saas.id
  name       = "saas"
}

And I get this error:

digitalocean_database_db.saas: Creating...
╷
│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to digitalocean_database_db.saas, provider
│ "provider[\"registry.terraform.io/digitalocean/digitalocean\"]" produced an
│ unexpected new value: Root resource was present, but now absent.
│ 
│ This is a bug in the provider, which should be reported in the provider's
│ ow

No other clues as to what could be causing this - one minute it worked. Next minute it doesn't :(

terraform {
    required_providers {
        digitalocean = {
            source = "digitalocean/digitalocean"
            version = "~> 2.0"
        }
    }
}
Mayeu commented 1 year ago

We are also experiencing this on Postgres and the last version of the provider (2.25.2).

SkeLLLa commented 1 year ago

I've also managed to reproduce this. So in order to have higher chances you may need to create multiple users at the same terraform apply stage. It looks like that it's caused by some race condition or API loadbalancing, e.g. when resource created on one instance of DO API, on the next request it's still not properly propagated to other.

So as a workaround I suggest to implement few retries with some delays:

POST -> create
GET -> get resource by id
[retry if GET failed]
GET -> get resource by id
[retry if GET failed]
GET -> get resource by id