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

Project is getting tainted in TF state file, even though it got created; when billing id is incorrect #5222

Closed
pb0101 opened this issue Dec 18, 2019 · 4 comments · Fixed by GoogleCloudPlatform/magic-modules#3145, #5719 or hashicorp/terraform-provider-google-beta#1777
Assignees
Labels

Comments

@pb0101
Copy link

pb0101 commented Dec 18, 2019

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

terraform -v
Terraform v0.12.17

  • provider.google v2.20.0
  • provider.random v2.2.1

Affected Resource(s)

  • google_project

Terraform Configuration Files

resource "google_project" "my_project" {
 name       = var.gcp_project
 project_id = "xxxx-project-name-xxxxx"
 billing_account = var.billing_id
 folder_id = "77777777777777777"
}

Debug Output

Panic Output

Expected Behavior

Project should update the correct billing id, instead of deleting and creating the project again.

Actual Behavior

When running terraform for creating project (with incorrect billing id), project gets created. But when applying an update to the project with correct billing id; terraform is destroying the project and creating it again. This causes name conflict, because the project is pending deletion.

Steps to Reproduce

  1. Create project with incorrect billing id

  2. Fix the billing id and apply changes.

  3. terraform apply

Important Factoids

terraform apply --auto-approve (with incorrect billing id)
google_project.my_project: Creating...

Error: Error setting billing account "yyyyyyyyyyyyy" for project "projects/xxxxxxxxxx": googleapi: Error 400: Request contains an invalid argument., badRequest

on main.tf line 50, in resource "google_project" "my_project":
50: resource "google_project" "my_project" {

Apply after fixing the billing id

google_project.my_project: Destroying... [id=xxxxxxxxxxxxx]
google_project.my_project: Destruction complete after 5s
google_project.my_project: Creating...

Error: error creating project xxxxxxxxxxxx (xxxxxxxxxxxxxxx): googleapi: Error 409: Requested entity already exists, alreadyExists. If you received a 403 error, make sure you have the roles/resourcemanager.projectCreator permission

on main.tf line 50, in resource "google_project" "my_project":
50: resource "google_project" "my_project" {

References

  • #0000
@ghost ghost added the bug label Dec 18, 2019
@edwardmedia edwardmedia assigned edwardmedia and chrisst and unassigned edwardmedia Jan 6, 2020
@edwardmedia
Copy link
Contributor

edwardmedia commented Jan 7, 2020

@chrisst here is the two plans

Terraform will perform the following actions:

  # google_project.my_project will be created
  + resource "google_project" "my_project" {
      + auto_create_network = true
      + billing_account     = "wrong_billing_account"
      + folder_id           = (known after apply)
      + id                  = (known after apply)
      + name                = "sunedward-test-1"
      + number              = (known after apply)
      + org_id              = "my_org_id"
      + project_id          = "xxxxxx-test-1"
      + skip_delete         = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

  # google_project.my_project is tainted, so must be replaced
-/+ resource "google_project" "my_project" {
        auto_create_network = true
      + billing_account     = "corrected_billing_account"
      + folder_id           = (known after apply)
      ~ id                  = "projects/xxxxxx-test-1" -> (known after apply)
      - labels              = {} -> null
        name                = "xxxxxx-test-1"
      ~ number              = "784120170585" -> (known after apply)
        org_id              = "my_org_id"
        project_id          = "xxxxxx-test-1"
      + skip_delete         = (known after apply)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

@chrisst
Copy link
Contributor

chrisst commented Jan 7, 2020

@pb0101 There was a change in 0.12 that will taint a resource when the create call throws an error for any reason unlike 0.11 which did not automatically taint the resource. Unfortunately there's no elegant solution in the provider to this problem with the existing version of Terraform core. The only way to avoid tainting the resource is to suppress the error, in which case the resource will appear to have succeeded and no error will be reported but subsequent plan's will show a diff on the billing account that won't make sense without an error.

For now you can manually untaint the resource with terraform untaint google_project.my_project and rerunning apply will fix the resource.

If this solution doesn't work for your use case I suggest filing an issue against Terraform core with your specific use case and see if they can add more granular control over the taint process.

Let me know if the workaround solves your use case!

@chrisst
Copy link
Contributor

chrisst commented Feb 12, 2020

assigning to @hashibot and myself as @umairidris is working on a fix for this.

@ghost
Copy link

ghost commented Mar 21, 2020

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 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 21, 2020
@ghost ghost removed the waiting-response label Mar 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.