Having trouble trying to reference one of my outputs in main.tf. I'm trying to use an output of a public IP address of an instance to try and connect to it using remote-exec . Here are the files:

main.tf

module "subnetwork" {source = "../modules/uc1" env = "${var.var_env}"company = "${var.var_company}"depends_on = [module.vpc]}output "server_private_ip" {value = google_compute_instance.default.network_interface[0].network_ip}output "server_public_ip" {value = google_compute_instance.default.network_interface[0].access_config[0].nat_ip}

../modules/uc1:

resource "google_compute_instance" "default" {name = "${format("%s","${var.company}-${var.tester}-${var.env}-${var.var_region_name}-instance1")}"machine_type = "${var.var_machine_type}"zone = "${var.var_zone_name}"tags = ["http", "https", "ssh"]boot_disk {initialize_params {image = "${var.var_instance_image}"}}metadata = {ssh-keys = "root:${file(var.var_ssh)}"}provisioner "remote-exec" {connection {user = "root"host = module.uc1.server_public_ip <--- here is the errortype = "ssh"private_key = "${file(var.var_ssh)}"}script = "./scripts/nginx_instance.sh"}network_interface {subnetwork = "${google_compute_subnetwork.public_subnet.name}"access_config {// Ephemeral IP}}}

The issue im having is trying to reference my output public IP address in remote-exec:

 provisioner "remote-exec" {connection {user = "root"host = module.uc1.server_public_iptype = "ssh"private_key = "${file(var.var_ssh)}"}script = "./scripts/nginx_instance.sh"}

Here is the error:

│ on ..\modules\uc1\instance.tf line 24, in resource "google_compute_instance" "default": │ 24: host = module.uc1.server_public_ip││ No module call named "uc1" is declared in module.subnetwork.
1

Best Answer


The module name is subnetwork not uc1. uc1 is the name of the folder the module's source files are in. When you declared the module: module "subnetwork" { you named it "subnetwork". To reference it would be:

host = module.subnetwork.server_public_ip

Assuming you declared an output named server_public_ip inside the module.