【问题标题】:How to add the `default-allow-http`如何添加`default-allow-http`
【发布时间】:2020-05-20 08:02:54
【问题描述】:

如何将 terraform 脚本中的 default-allow-http 防火墙规则添加到 Google 云计算实例?

provider "google" {
    credentials = file("CREDENTIAL_FILE")
    project = "gitlab-project"
    region = var.region
}

resource "google_compute_instance" "gitlab" {
  name          = var.machine_specs.name
  machine_type  = var.machine_type.emicro
  zone          = var.zone

  boot_disk {
    initialize_params {
        image = var.machine_specs.os
        size = var.machine_specs.size
    }
  }

  network_interface {
    # A default network is created for all GCP projects
    network     = "default"
    access_config {
      nat_ip = google_compute_address.static.address
    }
  }

    // Add the SSH key
    metadata = {
        ssh-keys = "martin:${file("~/.ssh/id_rsa.pub")}"
    }

}

// A variable for extracting the external ip of the instance
output "ip" {
 value = "${google_compute_instance.gitlab.network_interface.0.access_config.0.nat_ip}"
}

resource "google_compute_address" "static" {
  name = "ipv4-address"
  address_type = "EXTERNAL"
  address = "XXX.XXX.XXX.XXX"
}

resource "google_compute_firewall" "allow-http" {
  name = "default-allow-http"
  network = 

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



【问题讨论】:

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


【解决方案1】:

您需要将标签["http-server", "https-server"] 添加到您的资源组google_compute_instance,如下所示:

[...]

resource "google_compute_instance" "gitlab" {
  name          = var.machine_specs.name
  machine_type  = var.machine_type.emicro
  zone          = var.zone

tags = ["http-server", "https-server"]

[...]

【讨论】:

    【解决方案2】:

    只需将标签http-serverhttps-server 添加到您的google_cloud_instance 资源组。 标签可以在 GCloud-Console 的防火墙设置中找到。

    【讨论】:

      【解决方案3】:

      您可以使用google_compute_instance 资源中提供的tags 参数。

      它看起来像:

      resource "google_compute_instance" "gitlab" {
        name          = var.machine_specs.name
        machine_type  = var.machine_type.emicro
        zone          = var.zone
      
        tags = ["http-server"]
      

      http-server 标签用于default-allow-http 防火墙规则。 如果您需要default-allow-https,只需将https-server 附加到标签列表即可。

      希望这会有所帮助。

      【讨论】:

      • 跑了这个,仍然没有得到 default-allow-http 防火墙规则...我有什么遗漏吗?
      • 你能分享“gcloud compute firewall-rules describe default-allow-http”的输出吗?
      猜你喜欢
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2021-06-07
      • 2016-07-25
      • 1970-01-01
      • 2020-01-01
      • 2020-09-03
      相关资源
      最近更新 更多