如何使用 Terraform 在 GCP 中为实例添加私有 IP 输出?

时间:2021-03-24 16:22:12

标签: terraform terraform-provider-gcp

我在 outputs.tf 的 TF 代码中定义了此输出:

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

这是我用来在 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
}

但是当我运行 terraform plan 时,我收到此错误:

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.

在创建实例之前不会知道私有 IP。如何获得显示私有 IP 的输出?我不想为实例分配 IP,我只想知道它在创建后拥有什么 IP。

2 个答案:

答案 0 :(得分:1)

要获取通过 google_compute_instance 资源创建的 Google Compute 实例的默认动态分配 IP 地址,您可以访问 the resource documentation 中所述的 network_interface.0.network_ip 属性:

<块引用>
  • network_interface.0.network_ip - 实例的内部 IP 地址,手动或动态分配。

所以你的输出应该是:

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

答案 1 :(得分:0)

下面的输出块将为您提供所需的输出值

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