How to map static IP to terraform google compute engine instance?

六月ゝ 毕业季﹏ 提交于 2020-08-24 21:50:21

问题


I'm using terraform with google vm provider. I want to assign existing Static IP to a VM.

Code:

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"
    }
  }
}

But its failing with the error:

google_compute_instance.test2: network_interface.0.access_config.0: invalid or unknown key: address

How can I fix this?


回答1:


You could also allow terraform to create the static IP address for you and then assign it to the instance by object name.

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}"
    }
  }
}



回答2:


It worked by changing address to nat_ip in 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
    }
  }
}


来源:https://stackoverflow.com/questions/45359189/how-to-map-static-ip-to-terraform-google-compute-engine-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!