[英]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_manager或google_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.