Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform tries (and fails) to recreate database on database_instance while it is stopped. #8356

Closed
MattGson opened this issue Feb 1, 2021 · 2 comments · Fixed by GoogleCloudPlatform/magic-modules#4489, #8462 or hashicorp/terraform-provider-google-beta#2957

Comments

@MattGson
Copy link

MattGson commented Feb 1, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

0.14.5

Affected Resource(s)

  • google_sql_database_instance
  • google_sql_database

Terraform Configuration Files

resource "google_sql_database_instance" "some_db_instance" {
  database_version = "MYSQL_5_7"
  name             = "name"
  region           = var.region

  settings {
    activation_policy = "NEVER"
...

resource "google_sql_database" "some_db" {
  charset   = "utf8"
  collation = "utf8_general_ci"
  instance  = google_sql_database_instance.some_db_instance.name
  name      = "name"

  lifecycle {
    prevent_destroy = true
  }
}

Expected Behavior

Terraform should identify that the instance is stopped and not try to create sub-resources.

Actual Behavior

Error creating Database: googleapi: Error 400: Invalid request: Invalid request since instance is not running.

The database actually already exists. Terraform assumes it doesn't and tries to create it on the stopped instance.

Steps to Reproduce

  1. Create instance with activation_policy = "NEVER"
  2. Create database for the instance.
  3. terraform apply
@ghost ghost added the bug label Feb 1, 2021
@edwardmedia edwardmedia self-assigned this Feb 1, 2021
@edwardmedia
Copy link
Contributor

@MattGson I can repro the error. But if you change the data to uppercase for charset (below), you should not see this error.

Here are things around here

  1. because terraform saves charset to uppercase, it detects the difference and plans to update the database when you do subsequent apply. (This is a provider bug) Once you provide the uppercase data, the database recreation will not be in the plan and then you will not see above error.
  2. If you purposely want to update the database along with the instance update, you will see above error. Because these (instance and db) are different resources, there is no way at the provider level to avoid this error. This is the same behavior as Enabling private nodes in GKE does not recreate the node pools #8243.

Does this make sense? I could put a fix for above uppercase issue later on but it think it might not be the one you originally reported. Do you agree?

resource "google_sql_database" "some_db" {
  charset   = "utf8" ---> "UTF8"

@ghost
Copy link

ghost commented Mar 15, 2021

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Mar 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.