简体   繁体   English

如何引用terraform中的GCP资源?

[英]how to refer GCP resources in terraform?

I would like to understand about the resources reference in terraform.我想了解 terraform 中的资源参考。

Example: In my project, pubsub topic has been referred with.name as well as.id示例:在我的项目中,pubsub 主题已使用 .name 和 .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.我无法从许多在线论坛中获得有关通过.name/.id 引用的区别的信息。

For which resources using terraform we need to refer by.name and.id?哪些使用terraform的资源需要通过.name和.id来引用?

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.google_cloudiot_registry ,我看到id由包含资源名称的资源返回,并且同样被传递给event_notification_configs块的pubsub_topic_name部分。

If you wish to change pubsub_topic_name to pubsub_topic_id , you could create PR on the provider code base.如果您希望将pubsub_topic_name更改为pubsub_topic_id ,您可以在提供者代码库上创建 PR。

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.总而言之,如果您想引用某些资源/数据源的 output,您需要获取响应中返回的属性并将其分配给下一个资源中的适当字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM