【问题标题】:How to map static IP to terraform google compute engine instance?如何将静态 IP 映射到 terraform google 计算引擎实例?
【发布时间】:2017-07-27 19:11:57
【问题描述】:

我将 terraform 与 google 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:无效或未知密钥:地址

我该如何解决这个问题?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    您还可以允许 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}"
        }
      }
    }
    

    【讨论】:

    • 你知道我为什么会收到这个错误吗?错误:错误创建地址:googleapi:错误 409:资源 'projects/XXX/regions/europe-west1/addresses/my-test-static-ip-address' 已经存在,已经存在
    • 我找到了解决方案:terraform import google_compute_address.static my-test-static-ip-address
    【解决方案2】:

    它通过在access_config 中将address 更改为nat_ip 来工作。

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

    【讨论】:

      猜你喜欢
      • 2014-04-08
      • 2019-02-28
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多