如何将静态IP映射到terraform google计算引擎实例?

时间:2017-07-27 19:11:58

标签: terraform

我正在使用谷歌与谷歌vm提供商。我想将现有的静态IP分配给VM。

代码

resource "google_compute_instance" "test2" {
  name         = "dns-proxy-nfs"
  machine_type = "n1-standard-1"
  zone         = "${var.region}"

  disk {
    image = "centos-7-v20170719"
  }

  metadata {
    ssh-keys = "myuser:${file("~/.ssh/id_rsa.pub")}"
  }

  network_interface {
    network = "default"
    access_config {
      address = "130.251.4.123"
    }
  }
}

但它失败了,错误:

  

google_compute_instance.test2:network_interface.0.access_config.0:无效或未知密钥:地址

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:9)

您还可以允许terraform为您创建静态IP地址,然后按对象名称将其分配给实例。

resource "google_compute_address" "test-static-ip-address" {
  name = "my-test-static-ip-address"
}

resource "google_compute_instance" "test2" {
  name         = "dns-proxy-nfs"
  machine_type = "n1-standard-1"
  zone         = "${var.region}"

  disk {
    image = "centos-7-v20170719"
  }

  metadata {
    ssh-keys = "myuser:${file("~/.ssh/id_rsa.pub")}"
  }

  network_interface {
    network = "default"
    access_config {
      nat_ip = "${google_compute_address.test-static-ip-address.address}"
    }
  }
}

答案 1 :(得分:2)

通过在address中将nat_ip更改为access_config来实现效果。

resource "google_compute_instance" "test2" {
  name         = "dns-proxy-nfs"
  machine_type = "n1-standard-1"
  zone         = "${var.region}"

  disk {
    image = "centos-7-v20170719"
  }

  metadata {
    ssh-keys = "myuser:${file("~/.ssh/id_rsa.pub")}"
  }

  network_interface {
    network = "default"
    access_config {
      nat_ip = "130.251.4.123" // this adds regional static ip to VM
    }
  }
}