Reputation: 421
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.
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
.
So my question is, how can I access the VM's public IP adresses and names in Terraform?
I have tried google_compute_instance_template.tpl.network_interface.0.access_config.0.nat_ip
but it's empty.
Best regards, rforberger
Upvotes: 0
Views: 1699
Reputation: 81356
Use local-exec
to execute the 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.
Upvotes: 2
Reputation: 238189
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.
Thus, once you create these instance you will be able to access their IP addresses using the instance or MIG resources. 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. But the number and id of the instances changes with time, as they are fully managed by MIG, not by terraform.
Upvotes: 4