2017-07-27 54 views
0

我正在使用谷歌虛擬機提供商terraform。我想將現有的靜態IP分配給虛擬機。如何將靜態IP映射到terraform google計算引擎實例?

代碼

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: invalid or unknown key: address

我該如何解決這個問題?

回答

2

它的工作方式是將address更改爲access_config中的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 
    } 
    } 
} 
5

您還可以讓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}" 
    } 
    } 
} 
相關問題