bluethundr
bluethundr

Reputation: 1345

How do I add a private IP output for an instance in GCP using Terraform?

I have this output defined in my TF code in outputs.tf:

output "ip" {
  value = google_compute_address.vm_private_ip.address
}

This is the code I use to create the instance in main.tf:

// Launch an MS SQL Server into GCP
// Configure the Google Cloud provider
provider "google" {
  credentials = file("mycreds.json")
  project     = var.project
  region      = var.region
}

// Terraform plugin for creating random ids
resource "random_id" "instance_id" {
 byte_length = 8
}

// A single Compute Engine instance
resource "google_compute_instance" "default" {
  name         = var.instance_name
  machine_type = var.machine_type
  zone         = var.zone
  tags         = [var.instance_name, var.env_name]
  boot_disk {
    initialize_params {
    size =  var.boot_disk_size
    image = data.google_compute_image.sqlserverimage.self_link
    }

    }

  network_interface {
  subnetwork = var.subnetwork
  subnetwork_project = var.subnetwork_project
 }
}

data "google_compute_image" "sqlserverimage" {
  family  = var.image_family
  project = var.project
}

// Create additional disks
resource "google_compute_disk" "datadisk" {
  name  = var.data_disk_name
  type  = var.data_disk_type
  size  = var.data_disk_size
  zone  = var.zone
  image = data.google_compute_image.sqlserverimage.self_link
  labels = {
    environment = "dev"
    asv = "mycompanytools"
    ownercontact = "myuser"
  }
  physical_block_size_bytes = 4096
}

resource "google_compute_disk" "backupdisk" {
  name  = var.backup_disk_name
  type  = var.backup_disk_type
  size  = var.backup_disk_size
  zone  = var.zone
  image = data.google_compute_image.sqlserverimage.self_link
  labels = {
    environment = "dev"
    asv = "mycompanytools"
    ownercontact = "myuser"
  }
  physical_block_size_bytes = 4096
}

resource "google_compute_disk" "logdisk" {
  name  = var.log_disk_name
  type  = var.log_disk_type
  size  = var.log_disk_size
  zone  = var.zone
  image = data.google_compute_image.sqlserverimage.self_link
  labels = {
    environment = "dev"
    asv = "mycompanytools"
    ownercontact = "myuser"
  }
  physical_block_size_bytes = 4096
}

// Attach additional disks
resource "google_compute_attached_disk" "datadiskattach" {
  disk = google_compute_disk.datadisk.id
  instance = google_compute_instance.default.id
}

resource "google_compute_attached_disk" "backupdiskattach" {
  disk = google_compute_disk.backupdisk.id
  instance = google_compute_instance.default.id
}

resource "google_compute_attached_disk" "logdiskattach" {
  disk = google_compute_disk.logdisk.id
  instance = google_compute_instance.default.id
}

But when I run a terraform plan I get this error:

terraform plan

Error: Reference to undeclared resource

  on outputs.tf line 2, in output "ip":
   2:   value = google_compute_address.vm_private_ip.address

A managed resource "google_compute_address" "vm_private_ip" has not been
declared in the root module.

The private IP will not be known until the instance is created. How do I get an output to show the private IP? I do not want to assign an IP to the instance, I just want to know what IP it has once it's created.

Upvotes: 1

Views: 4064

Answers (2)

ydaetskcoR
ydaetskcoR

Reputation: 56997

To get the default, dynamically assigned IP address of a Google Compute instance created via the google_compute_instance resource you can access the network_interface.0.network_ip attribute as described in the resource documentation:

  • network_interface.0.network_ip - The internal ip address of the instance, either manually or dynamically assigned.

So your output should be:

output "ip" {
  value = google_compute_instance.default.network_interface.0.network_ip
}

Upvotes: 1

Ravichandran
Ravichandran

Reputation: 481

The below output block will give you the desired output value

output "ip" {
  value = google_compute_instance.default.network_interface.0.network_ip
}

Upvotes: 2

Related Questions