Terraform: HTTP 409 while provisioning Google Cloud SQL instance

Created on 7 Jan 2016  ยท  10Comments  ยท  Source: hashicorp/terraform

I'm attempting to provision the following CLoud SQL instance (derived from the example in the docs):

resource "google_sql_database_instance" "master" {
  name = "node-control"
  region = "us-central"

  settings {
    tier = "D0"
  }
}

Here's what I'm getting:

google_sql_database_instance.master: Creating...
  database_version:   "" => "MYSQL_5_5"
  name:               "" => "nodecontrol"
  region:             "" => "us-central"
  self_link:          "" => "<computed>"
  settings.#:         "" => "1"
  settings.0.tier:    "" => "D0"
  settings.0.version: "" => "<computed>"
Error applying plan:

1 error(s) occurred:

* google_sql_database_instance.master: Error, failed to create instance nodecontrol: googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

The Google Cloud project is empty, and I have no other resources defined. Currently running Terraform v0.6.8.

$ gcloud sql instances list
NAME  REGION  TIER  ADDRESS  STATUS
Listed 0 items.
bug providegoogle-cloud

Most helpful comment

OK, this ended up being a sneaky one. I guess they use some kind of background task to finalize Cloud SQL instance deletions. Here's what I got when I tried to use the instance name in the web UI:

image

I don't have anything to suggest as far as a workaround, but it's clear that we can't just delete then immediately re-create the instance with the same name.

All 10 comments

OK, this ended up being a sneaky one. I guess they use some kind of background task to finalize Cloud SQL instance deletions. Here's what I got when I tried to use the instance name in the web UI:

image

I don't have anything to suggest as far as a workaround, but it's clear that we can't just delete then immediately re-create the instance with the same name.

@lwander any ideas here? If we know how long that "recently" check is we could workaround by dropping a sleep in the delete step - unless it's several minutes or something.

The docs say that a name can be unavailable for up to two months. This is unfortunate, so I guess the second best thing to do is write a nice error message explaining that you need a new instance name?

Haha I suppose we can't exactly time.Sleep(2 * duration.Month). :stuck_out_tongue:

Yeah a nicer error message for one would be good.

We can also make the name field optional and populate it with resource.UniqueId() to avoid collisions. We do this for a bunch of resources that need unique names and it works pretty nicely.

Ha, that might be too discouraging :sleeping:

Optional field name sounds perfect, I'll submit a PR.

I just found this old issue because I'm getting the following error on Terraform 0.11.13:

* google_sql_database_instance.default: Error, failed to create instance master-instance: googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState

It seems the nicer message is not getting displayed. Yes, I tried to create an instance the had just been deleted.

I'm encountering same as @gtirloni, on matching Terraform version.

Hilariously, I can't even terraform destroy now.

For those who encounter similar. I was trying to recreate a db user. Running into rejections doing this, I thought I would just delete my database instance instead (developing). This failed as well through terraform so I decided to delete it through console. Now I'm unable to plan, apply, or destroy in this module.

  • google_sql_user.X: google_sql_user.X: Error reading SQL User "X" in instance "terraform-20190403210345996400000001": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState

@nickmarx12345678 I just arrived here having hit the same issue. terraform state rm <resource> should sort you out.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings