【问题标题】:How to add a static IP to a GCE VM instance using Terraform如何使用 Terraform 将静态 IP 添加到 GCE VM 实例
【发布时间】:2020-01-14 13:41:46
【问题描述】:

我想使用 Terraform 0.12 部署一个 Google Cloud Compute Engine VM 实例。我的问题是创建了 2 个 IP 地址。我有一个静态 IP 地址和一个临时 IP 地址。 VM 实例正在使用临时 IP。 区域是正确的。

这是我正在使用的代码:

resource "google_compute_address" "static-ip" {
  name = "static-ip"
  address_type = "EXTERNAL"
  region = var.location
}

在 Compute Engine 虚拟机实例中,google_compute_instance_template, 网络是这样配置的:

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

之后,我使用资源 google_compute_instance_from_template 实例化 VM 实例。

我想知道,如何将外部 IP 附加到我的 VM 实例并且只有一个 IP 地址?

【问题讨论】:

    标签: google-cloud-platform virtual-machine google-compute-engine terraform


    【解决方案1】:

    GCP IP Addresses 文章中所述,您将拥有 2 个 IP、一个内部 IP 和一个可选的外部 IP(临时或静态)

    要使用 Terraform 创建具有静态 IP 的实例,请查看他们的 google_compute_address 示例

    resource "google_compute_address" "static" {
      name = "ipv4-address"
    }
    
    data "google_compute_image" "debian_image" {
      family  = "debian-9"
      project = "debian-cloud"
    }
    
    resource "google_compute_instance" "instance_with_ip" {
      name         = "vm-instance"
      machine_type = "f1-micro"
      zone         = "us-central1-a"
    
      boot_disk {
        initialize_params {
          image = data.google_compute_image.debian_image.self_link
        }
      }
    
      network_interface {
        network = "default"
        access_config {
          nat_ip = google_compute_address.static.address
        }
      }
    }
    

    阅读Argument Reference 部分以了解每个变量的预期内容

    【讨论】:

      【解决方案2】:

      感谢您的回复。我自己解决了这个问题。我认为问题在于我首先创建了一个具有内部 IP 的 VM。比我将 terraform 脚本更改为外部临时 IP。之后,我将所有内容都更改为静态外部。

      Terraform 没有删除临时 IP。所以我手动删除了ip。

      【讨论】:

        猜你喜欢
        • 2019-08-12
        • 2021-07-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 2019-02-28
        • 2021-05-02
        • 2021-06-27
        相关资源
        最近更新 更多