From 3d953200e2954f5771ab5ff55c459e1a0e0ad4a3 Mon Sep 17 00:00:00 2001 From: Nick Stroud Date: Tue, 6 Jun 2023 01:01:29 +0000 Subject: [PATCH] Update vm-instance instructions to handle the case that there are zero instances --- modules/compute/vm-instance/outputs.tf | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/compute/vm-instance/outputs.tf b/modules/compute/vm-instance/outputs.tf index e45d3e70ee..87db84dee5 100644 --- a/modules/compute/vm-instance/outputs.tf +++ b/modules/compute/vm-instance/outputs.tf @@ -29,12 +29,18 @@ output "internal_ip" { value = google_compute_instance.compute_vm[*].network_interface[0].network_ip } +locals { + no_instance_instructions = "No instances were created." + first_instance_name = try(google_compute_instance.compute_vm[0].name, "no-instance") + ssh_instructions = <<-EOT + Use the following commands to SSH into the first VM created: + gcloud compute ssh --zone ${var.zone} ${local.first_instance_name} --project ${var.project_id} + If not accessible from the public internet, use an SSH tunnel through IAP: + gcloud compute ssh --zone ${var.zone} ${local.first_instance_name} --project ${var.project_id} --tunnel-through-iap + EOT +} + output "instructions" { description = "Instructions on how to SSH into the created VM. Commands may fail depending on VM configuration and IAM permissions." - value = <<-EOT - Use the following commands to SSH into the first VM created: - gcloud compute ssh --zone ${var.zone} ${google_compute_instance.compute_vm[0].name} --project ${var.project_id} - If not accessible from the public internet, use an SSH tunnel through IAP: - gcloud compute ssh --zone ${var.zone} ${google_compute_instance.compute_vm[0].name} --project ${var.project_id} --tunnel-through-iap - EOT + value = var.instance_count > 0 ? local.ssh_instructions : local.no_instance_instructions }