Google compute engine 如何使用Terraform在我的Google Compute实例上公开额外的端口?

Google compute engine 如何使用Terraform在我的Google Compute实例上公开额外的端口?,google-compute-engine,terraform,Google Compute Engine,Terraform,我有一个GoogleCompute实例,由一些Terraform代码定义 provider "google" { credentials = "${file("auth.json")}" project = "aqueous-depth-189023" region = "europe-west2" } resource "google_project" "website" { name = "Website" project_id = "aqueous-

我有一个GoogleCompute实例,由一些Terraform代码定义

provider "google" {
  credentials = "${file("auth.json")}"
  project     = "aqueous-depth-189023"
  region      = "europe-west2"
}

resource "google_project" "website" {
  name = "Website"
  project_id = "aqueous-depth-189023"
}

resource "google_compute_instance" "default" {
  name         = "website"
  machine_type = "n1-standard-1"
  zone         = "europe-west1-b"

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }

  metadata {
    sshKeys = "james:${file("website.pem.pub")}"
  }

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-8"
    }
  }
}
默认情况下,Google只为Google Compute实例公开端口22和一些其他端口。我可以更新我的Terraform代码以实现暴露端口80和其他一些端口,而不必使用web控制台吗?我需要添加或编辑哪些地形资源?

使用。您需要使用实例资源创建实例,并在防火墙资源上进行设置。您可以参考这些标记的工作方式

例子 向实例添加标记 添加防火墙资源
您还需要定义或,上面的示例使用的源范围为
0.0.0.0/0
,即“任意”。这可能不适用于所有规则。

如果有一个带有代码示例的摘要答案,并通过链接到文档以获取更多信息进行补充,则此答案比仅链接到文档更有用。@ydaetskcoR用示例进行了更新
resource "google_compute_instance" "default" {
  name         = "website"
  machine_type = "n1-standard-1"
  zone         = "europe-west1-b"

  tags = ["web"]

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }

  metadata {
    sshKeys = "james:${file("website.pem.pub")}"
  }

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-8"
    }
  }
}
resource "google_compute_firewall" "default" {
 name    = "web-firewall"
 network = "default"

 allow {
   protocol = "icmp"
 }

 allow {
   protocol = "tcp"
   ports    = ["80"]
 }

 source_ranges = ["0.0.0.0/0"]
 target_tags = ["web"]
}