Pinser
Pinser

Reputation: 1896

Terraform Topic resource already exists in across projects? How to handle this?

How to deal with resources existing in multiple projects. In this case I have 2 TF projects and have a shared topic in google pubsub. Following the google provider docs I created this configuration in the subscriber project. But it gives me a resource already exists error.

resource "google_pubsub_topic" "item_edited" {
  project = "listing-dev"
  name    = "item_edited"
}

So this resource already exits in some other project but following the guides I created it our own project as well.

resource "google_pubsub_subscription" "item_edited" {
  name    = "item_edited_subscription"
  topic   = "${google_pubsub_topic.item_edited.id}"
  project = "${module.offer-dev.gcp_project_id}"
}

Error:

Error: Error applying plan:

1 error(s) occurred:

* google_pubsub_topic.item_edited: 1 error(s) occurred:

* google_pubsub_topic.item_edited: googleapi: Error 409: Resource already exists in the project (resource=item_edited)., alreadyExists

Upvotes: 1

Views: 4667

Answers (2)

Pinser
Pinser

Reputation: 1896

Actually, the correct answer is we need to use variables to explicitly define dependency in terraform config.

https://learn.hashicorp.com/terraform/getting-started/dependencies.html

By studying the resource attributes used in interpolation expressions, Terraform can automatically infer when one resource depends on another

Upvotes: 0

F10
F10

Reputation: 2893

Topic's name should have this format:

projects/<your_project>/topics/<your_topic_name>

For more information, you could check the following link and/or try the API explorer.

Hope it helps.

Upvotes: 1

Related Questions