Sekhar
Sekhar

Reputation: 699

how to refer GCP resources in terraform?

I would like to understand about the resources reference in terraform.

Example: In my project, pubsub topic has been referred with .name as well as .id

  resource "google_pubsub_topic" "topic" {
      name = "my_topic"
  }

resource "google_pubsub_subscription" "subscription" {
  name  = "my_subscription"
  topic = google_pubsub_topic.topic.name
  }

 resource "google_cloudiot_registry" "cloudiot" {
  name      = "my_iot_registry"
  region    = "us-central1"
  log_level = "ERROR"

  event_notification_configs {
    pubsub_topic_name = google_pubsub_topic.topic.id
  }

  mqtt_config = {
    mqtt_enabled_state = "MQTT_ENABLED"
  }
}

I could not get the information the difference in referring by .name/.id from many online forums.

For which resources using terraform we need to refer by .name and .id?

Upvotes: 0

Views: 716

Answers (1)

harshavmb
harshavmb

Reputation: 3872

There is no such hard referring notion, it appears to be an anomaly with usage specific to this resource.

I guess it needs to be

event_notification_configs {
    pubsub_topic_id = google_pubsub_topic.topic.id
}

From google_cloudiot_registry, I see id being returned by the resource which contains the resource name & the same is being passed to pubsub_topic_name part of event_notification_configs block.

If you wish to change pubsub_topic_name to pubsub_topic_id, you could create PR on the provider code base.

To conclude, if you would like to refer output of some resource/data source, you would need to fetch the attributes returned in the response & assign it to appropriate field in the next resource.

Upvotes: 1

Related Questions