如何使用 Terraform 将标准网络接口分配给 Google Cloud 平台上的计算实例?

huangapple go评论53阅读模式
英文:

How to assign a STANDARD network interface to a compute instance on GCP using terraform?

问题

在使用标准网络的 Terraform 模板运行时:

resource "google_compute_address" "default_standard" {
  name          = "standard-address"
  region        = var.region
  network_tier  = "STANDARD"
}

# 创建虚拟机实例
resource "google_compute_instance" "my_instance" {
  name         = "my-instance"
  machine_type = "e2-micro"  # 免费套餐实例
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "debian-11-bullseye-v20230509"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // 公共 IP
      nat_ip = google_compute_address.default_standard.address
    }
  }
}

出现错误:

错误: 创建实例时出错: googleapi: 错误 400: 无效的资源使用: '外部 IP 地址:35.211.106.69 的网络层级为 STANDARD,与实例访问配置中的 PREMIUM 网络层级不匹配',invalidResourceUsage

我该怎么办?

英文:

When running a terraform template using a STANDARD network like:

resource "google_compute_address" "default_standard" {
  name          = "standard-address"
  region        = var.region
  network_tier  = "STANDARD"
}

# Create a virtual machine instance
resource "google_compute_instance" "my_instance" {
  name         = "my-instance"
  machine_type = "e2-micro"  # Free tier instance
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "debian-11-bullseye-v20230509"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Public IP
      nat_ip = google_compute_address.default_standard.address
    }
  }
}

Gives the error:

Error: Error creating instance: googleapi: Error 400: Invalid resource usage: 'External IP address: 35.211.106.69 has a different network tier STANDARD from the network tier in instance access config PREMIUM.'., invalidResourceUsage

What can I do?

答案1

得分: 1

我找到了问题,实际的实例定义必须包括它将允许的网络类型:

resource "google_compute_address" "default_standard" {
  name        = "standard-address"
  region      = var.region
  network_tier = "STANDARD"
}

# 创建一个虚拟机实例
resource "google_compute_instance" "my_instance" {
  name         = "my-instance"
  machine_type = "e2-micro"  # 免费套餐实例
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "debian-11-bullseye-v20230509"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // 公共IP
      nat_ip      = google_compute_address.default_standard.address
      network_tier = "STANDARD"
    }
  }
}
英文:

Found the problem, the actual instance definition has to include the type of network it will allow:

resource "google_compute_address" "default_standard" {
  name          = "standard-address"
  region        = var.region
  network_tier  = "STANDARD"
}

# Create a virtual machine instance
resource "google_compute_instance" "my_instance" {
  name         = "my-instance"
  machine_type = "e2-micro"  # Free tier instance
  zone         = var.zone
  boot_disk {
    initialize_params {
      image = "debian-11-bullseye-v20230509"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Public IP
      nat_ip = google_compute_address.default_standard.address
      network_tier = "STANDARD"
    }
  }
}

huangapple
  • 本文由 发表于 2023年6月6日 12:02:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定