如何使用 terraform 在 Google Compute Engine 上启用允许 HTTP 流量/允许 HTTP 流量

Pra*_*any 11 google-compute-engine google-cloud-platform terraform terraform-provider-gcp

有没有其他方法可以直接使用 terraform 启用这些规则,而无需在 GCP 中创建单独的防火墙规则,然后将标签附加到计算引擎

在此输入图像描述

目前我正在这样做

resource "google_compute_firewall" "allow_http" {
  name    = "allow-http-rule"
  network = "default"
  allow {
    ports    = ["80"]
    protocol = "tcp"
  }
  target_tags = ["allow-http"]
  priority    = 1000

}
Run Code Online (Sandbox Code Playgroud)

然后使用这个标签

resource "google_compute_instance" "app" {
  ...
  tags = ["allow-http"]
}
Run Code Online (Sandbox Code Playgroud)

小智 4

参数参考提到了选项标签。您可以按如下方式使用它:

...
  tags = ["http-server","https-server"]
...
Run Code Online (Sandbox Code Playgroud)

以同样的方式,您可以使用gcloud 计算实例添加标签来编辑这些值,如下所示:

要将标签添加到现有 VM 实例,请使用以下gcloud命令:

gcloud compute instances add-tags [YOUR_INSTANCE_NAME] --tags http-server,https-server
Run Code Online (Sandbox Code Playgroud)

要在创建实例时添加标签,请在语句中包含该标志:

gcloud compute instances create [YOUR_INSTANCE_NAME] --tags http-server,https-server
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复,我实际上看到了提到的这个链接,但它只是向实例添加标签,它既不勾选这些框也不创建防火墙规则 (2认同)