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

Deleting activated services on a project fails #1292

Closed
morgante opened this issue Apr 4, 2018 · 12 comments · Fixed by #2090
Closed

Deleting activated services on a project fails #1292

morgante opened this issue Apr 4, 2018 · 12 comments · Fixed by #2090
Labels
bug forward/review In review; remove label to forward service/serviceusage

Comments

@morgante
Copy link

morgante commented Apr 4, 2018

I'm trying to delete a module which both created a project and activated some services on that project, but it fails because the project gets created first.

Terraform Version

Terraform v0.11.5
+ provider.external v1.0.0
+ provider.google v1.8.0
+ provider.gsuite (unversioned)
+ provider.null v1.0.0
+ provider.random v1.1.0

Affected Resource(s)

Please list the resources as a list, for example:

  • google_project_service

Terraform Configuration Files

resource "google_project" "folder" {
  count           = "${var.folder_id != false ? 1 : 0}"
  name            = "${var.name}"
  project_id      = "${local.final_project_id}"
  folder_id       = "${var.folder_id}"
  billing_account = "${var.billing_account}"
}

locals {
  project_id       = "${element(coalescelist(google_project.folder.*.project_id, google_project.org.*.project_id), 0)}"
}

resource "google_project_service" "project_services" {
  count = "${length(var.activate_apis)}"

  project = "${local.project_id}"
  service = "${element(var.activate_apis,count.index)}"
}

Debug Output

Ping me privately if this is needed.

Expected Behavior

The services resource is "deleted" before the project (not that it actually needs to do something).

Actual Behavior

The service enters a zombie state where it constantly tries to get removed even though the project isn't even active any more.

Whenever I try to apply afterwards it's unable to fetch the state:

* module.application-xxx-xxx-xxx.module.project-xxx-xxx.google_project_service.project_services: google_project_service.project_services: googleapi: Error 404: Project 'marketo-xxx-xxx-xxx' not found or permission denied., notFound
@morgante
Copy link
Author

morgante commented Apr 4, 2018

Also running into this with usage bucket export:

* module.application-marketo-bigdata-solr.module.project-application-int.google_project_usage_export_bucket.usage_report_export: google_project_usage_export_bucket.usage_report_export: Error reading Project data for project marketo-bigdata-solr-int: googleapi: Error 403: Project 593012260542 has been scheduled for deletion and cannot be used for API calls. Visit https://console.developers.google.com/iam-admin/projects?pendingDeletion=true to undelete the project., accessNotConfigured

@rosbo
Copy link
Contributor

rosbo commented Apr 4, 2018

We should add a field disable_on_destroy like we did for google_project_service.

Excerpt from the google_project_service documentation:

  • disable_on_destroy - (Optional) If true, disable the service when the terraform resource is destroyed. Defaults to true. May be useful in the event that a project is long-lived but the infrastructure running in that project changes frequently.

@morgante
Copy link
Author

morgante commented Apr 4, 2018

Thanks @rosbo but I'm not sure that actually solves it since I'm already using google_project_service with the default parameter (disable_on_destory defaults to true).

@rosbo
Copy link
Contributor

rosbo commented Apr 4, 2018

Ooops. I read too quickly and mistakenly read "google_project_services" for the resource you are using because the name of your resource is "project_services"...

I believe the problem you are experiencing is caused by the fact that google_project_service.project_services doesn't have a depends_on relationship with the google_project.folder so Terraform tries to delete both at the same time.

Usually, that relationship is created automatically when interpolating the project field in the google_project_service resource.

In your case, you will need to add an explicit dependency:

resource "google_project_service" "project_services" {
...
   depends_on = ["google_project.folder"]
}

Let me know if that fixes your problem.

@morgante
Copy link
Author

morgante commented Apr 4, 2018

I'm curious why the dependency isn't being created automatically? Do you know why the transitivity isn't working when it passes through the locals variable first?

The dependency did work when the project was created since I didn't get any errors about trying to activate services on a non-existent project.

@catsby
Copy link
Contributor

catsby commented Apr 5, 2018

I'm guessing it's because google_project_service depends on locals, which depends on google_project, so the dependencies are interpolated correctly for create.

I imagine the graph looks different for a complete destroy, IIRC that would read from state and plan to destroy everything, meaning it wouldn't consider the dependency on local et. al. I could be wrong though...

If I'm write, that would mean that project and service don't get a dependency and are deleted in parallel (or possibly deleted in parallel, depending on the number of resources). Which leads to this issue.

In that case, the depends_on forces the dependency in destroy, but is redundant in create.

Does changing _service to get it's project directly work?

resource "google_project" "folder" {
  count           = "${var.folder_id != false ? 1 : 0}"
  name            = "${var.name}"
  project_id      = "${local.final_project_id}"
  folder_id       = "${var.folder_id}"
  billing_account = "${var.billing_account}"
}

resource "google_project_service" "project_services" {
  count = "${length(var.activate_apis)}"

  project = "${element(coalescelist(google_project.folder.*.project_id, google_project.org.*.project_id), 0)}"
  service = "${element(var.activate_apis,count.index)}"
}

@u2mejc
Copy link

u2mejc commented May 29, 2018

@danawillow Is this related to #1538 ?

@danawillow
Copy link
Contributor

Nope- #1538 is specific to the release that occurred last week.

@paddycarver
Copy link
Contributor

@morgante is this still an issue? Can I close this out? If this is still an issue, I think it's at least an upstream issue with Terraform--I'm not sure there's much the provider can do, unfortunately, except maybe consider the service deleted if the project can't be found.

@morgante
Copy link
Author

@paddycarver Yes I still encounter this regularly.

except maybe consider the service deleted if the project can't be found.

That would help a lot. I regularly have to run terraform state rm to solve this today.

paddycarver added a commit that referenced this issue Sep 20, 2018
When reading or deleting the google_project_service resource, first
retrieve the project. If it's not found, or if it's pending deletion,
remove the service, as it's functionally disabled.

Also, add a test to confirm the behaviour.

This should resolve #1292.
@paddycarver
Copy link
Contributor

@morgante I was unable to successfully reproduce the 404 error, but I managed to get the 403. #2090 should handle both, even though I can't verify the 404.

@ghost
Copy link

ghost commented Nov 16, 2018

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 Nov 16, 2018
@ghost ghost removed the waiting-response label Nov 16, 2018
@github-actions github-actions bot added forward/review In review; remove label to forward service/serviceusage labels Jan 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug forward/review In review; remove label to forward service/serviceusage
Projects
None yet
7 participants