如何添加`default-allow-http`

Mar*_*sli 3 google-compute-engine google-cloud-platform terraform

如何将default-allow-httpterraform 脚本中的防火墙规则添加到 Google Cloud Compute 实例?

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"]
  }
}



Run Code Online (Sandbox Code Playgroud)

pra*_*eep 8

您可以使用资源中可用的标签参数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"]
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助。