Martin Müsli
Martin Müsli

Reputation: 1159

How to add the `default-allow-http`

How to add the default-allow-http firewall rule in a terraform script to a Google Cloud Compute Instance?

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



Upvotes: 4

Views: 2157

Answers (3)

Pradeep Bhadani
Pradeep Bhadani

Reputation: 4721

You can use tags argument available in google_compute_instance resource.

it would look something like:

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

  tags = ["http-server"]

http-server tag is for default-allow-http firewall rule. If you need default-allow-https then simply append https-server to the tag list.

Hope this helps.

Upvotes: 10

Martin Zwölfer
Martin Zwölfer

Reputation: 21

Simply add the tags http-server and https-server to your google_cloud_instance resource gruop. The tags can be found in the Firewall-settings in your GCloud-Console.

Upvotes: 2

Martin Müsli
Martin Müsli

Reputation: 1159

You need to add the tags ["http-server", "https-server"] to your resource group google_compute_instance like so:

[...]

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

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

[...]

Upvotes: 2

Related Questions