简体   繁体   English

如何获取 IP 和 Terraform 谷歌云资源的名称 google_compute_instance_template

[英]How to get IP and name of Terraform Google Cloud ressource google_compute_instance_template

I'm using a google_compute_instance_template ressource in Terraform and am wondering, how I can access the public IP address and name of the VM that will be created by the template in Terraform.我在 Terraform 中使用google_compute_instance_template资源,我想知道如何访问公共 IP 地址和将由 Z303E96F80577360D0BZ7B28 中的模板创建的 VM 名称。

My template looks like the following:我的模板如下所示:

resource "google_compute_instance_template" "tpl" {
  name_prefix             = "${var.name_prefix}-"
  project                 = var.project_id
  machine_type            = var.machine_type
  labels                  = var.labels
  metadata                = var.metadata
  tags                    = var.tags
  can_ip_forward          = var.can_ip_forward
  metadata_startup_script = var.startup_script
  region                  = var.region
  dynamic "disk" {
    for_each = local.all_disks
    content {
      auto_delete  = lookup(disk.value, "auto_delete", null)
      boot         = lookup(disk.value, "boot", null)
      device_name  = lookup(disk.value, "device_name", null)
      disk_name    = lookup(disk.value, "disk_name", null)
      disk_size_gb = lookup(disk.value, "disk_size_gb", null)
      disk_type    = lookup(disk.value, "disk_type", null)
      interface    = lookup(disk.value, "interface", null)
      mode         = lookup(disk.value, "mode", null)
      source       = lookup(disk.value, "source", null)
      source_image = lookup(disk.value, "source_image", null)
      type         = lookup(disk.value, "type", null)

      dynamic "disk_encryption_key" {
        for_each = lookup(disk.value, "disk_encryption_key", [])
        content {
          kms_key_self_link = lookup(disk_encryption_key.value, "kms_key_self_link", null)
        }
      }
    }
  }

  dynamic "service_account" {
    for_each = [var.service_account]
    content {
      email  = lookup(service_account.value, "email", null)
      scopes = lookup(service_account.value, "scopes", null)
    }
  }

  network_interface {
    network            = var.network
    subnetwork         = var.subnetwork
    subnetwork_project = var.subnetwork_project
    dynamic "access_config" {
      for_each = var.access_config
      content {
        nat_ip       = access_config.value.nat_ip
        network_tier = access_config.value.network_tier
      }
    }
  }

  lifecycle {
    create_before_destroy = "true"
  }

  # scheduling must have automatic_restart be false when preemptible is true.
  scheduling {
    preemptible       = var.preemptible
    automatic_restart = ! var.preemptible
  }

  dynamic "shielded_instance_config" {
    for_each = local.shielded_vm_configs
    content {
      enable_secure_boot          = lookup(var.shielded_instance_config, "enable_secure_boot", shielded_instance_config.value)
      enable_vtpm                 = lookup(var.shielded_instance_config, "enable_vtpm", shielded_instance_config.value)
      enable_integrity_monitoring = lookup(var.shielded_instance_config, "enable_integrity_monitoring", shielded_instance_config.value)
    }
  }
}

I don't have a google_compute_instance ressource in my plan, only a google_compute_instance_template .我的计划中没有google_compute_instance资源,只有google_compute_instance_template

So my question is, how can I access the VM's public IP adresses and names in Terraform?所以我的问题是,如何在 Terraform 中访问 VM 的公共 IP 地址和名称?

I have tried google_compute_instance_template.tpl.network_interface.0.access_config.0.nat_ip but it's empty.我试过google_compute_instance_template.tpl.network_interface.0.access_config.0.nat_ip但它是空的。

Best regards, rforberger最好的问候, rforberger

Based on the comments.根据评论。

The google_compute_instance_template is only template, based on which you can create instances using compute_instance_group_manager or google_compute_instance_from_template . google_compute_instance_template只是模板,您可以在此基础上使用compute_instance_group_managergoogle_compute_instance_from_template创建实例。

Thus, once you create these instance you will be able to access their IP addresses using the instance or MIG resources.因此,一旦您创建了这些实例,您将能够使用实例或 MIG 资源访问它们的 IP 地址。 But template in itself, does not provide such information.但模板本身并不提供此类信息。 Once you create your MIG, you can use instances attribute from MIG data source to get the instance.创建 MIG 后,您可以使用 MIG 数据源中的实例属性来获取实例。 But the number and id of the instances changes with time, as they are fully managed by MIG, not by terraform.但是实例的数量和 ID 会随着时间而变化,因为它们完全由 MIG 管理,而不是由 terraform 管理。

Use local-exec to execute the CLI.使用local-exec执行 CLI。

resource "null_resource" "instances" {
  provisioner "local-exec" {
    command = "gcloud compute instance-groups managed list-instances <NAME>"
  }
}

Replace <Name> with a var or string representing the MIG.<Name>替换为代表 MIG 的var或字符串。

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

相关问题 GCP 获取从 google_compute_instance_template 创建的计算实例的公共 IP 地址 - GCP Fetch Public IP address for compute instance created from google_compute_instance_template 如何从实例模板创建 Google Cloud Compute Instance - How to create a Google Cloud Compute Instance from the an instance template 带有 Google Cloud 的 Terraform:google_cloud_instance 找不到 google_compute_disk - Terraform with Google Cloud: google_cloud_instance can’t find google_compute_disk Terraform Google Cloud:公开 VM 实例 IP - Terraform Google Cloud: Making VM instance IP public Google Cloud Endpoints定位到VM实例(计算引擎)的内部IP? - Google Cloud Endpoints target to Internal IP of VM Instance (Compute Engine)? 无法通过Google Cloud Compute引擎实例中的外部IP连接 - Cannot connect through external IP in Google cloud Compute engine instance 获取当前Google Compute实例的名称或ID - Get the name or ID of the current Google Compute Instance 如何在 Google Compute VM 上使用 terraform 进行 ip 转发 - How to do ip forwarding with terraform on a Google Compute VM 如何使用 CLI 将 static 外部 IP 地址附加到 Google 云计算实例 - How to attach a static external IP address to a google cloud compute instance using the CLI 如何在 Google Cloud Compute Engine 实例上轮换临时 IP? - How do I rotate ephemeral IP on Google Cloud Compute Engine instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM