<commit_before># External TCP proxy load balancer with managed instance group backend

# [START cloudloadbalancing_ext_tcp_proxy_lb]
# VPC
resource "google_compute_network" "default" {
  name                    = "<%= ctx[:vars]['tcp_proxy_xlb_network'] %>"
  provider                = google-beta
  auto_create_subnetworks = false
}

# backend subnet
resource "google_compute_subnetwork" "default" {
  name          = "<%= ctx[:vars]['tcp_proxy_xlb_subnet'] %>"
  provider      = google-beta
  ip_cidr_range = "10.0.1.0/24"
  region        = "us-central1"
  network       = google_compute_network.default.id
}

# reserved IP address
resource "google_compute_global_address" "default" {
  provider = google-beta
  name = "<%= ctx[:vars]['tcp_proxy_xlb_ip'] %>"
}

# forwarding rule
resource "google_compute_global_forwarding_rule" "<%= ctx[:primary_resource_id] %>" {
  name                  = "<%= ctx[:vars]['tcp_proxy_xlb_forwarding_rule'] %>"
  provider              = google-beta
  ip_protocol           = "TCP"
  load_balancing_scheme = "EXTERNAL"
  port_range            = "110"
  target                = google_compute_target_tcp_proxy.default.id
  ip_address            = google_compute_global_address.default.id
}

resource "google_compute_target_tcp_proxy" "default" {
  provider = google-beta
  name            = "<%= ctx[:vars]['test_proxy_health_check'] %>"
  backend_service = google_compute_backend_service.default.id
}

# backend service
resource "google_compute_backend_service" "default" {
  provider = google-beta
  name                  = "<%= ctx[:vars]['tcp_proxy_xlb_backend_service'] %>"
  protocol              = "TCP"
  port_name             = "tcp"
  load_balancing_scheme = "EXTERNAL"
  timeout_sec           = 10
  health_checks         = [google_compute_health_check.default.id]
  backend {
    group           = google_compute_instance_group_manager.default.instance_group
    balancing_mode  = "UTILIZATION"
    max_utilization = 1.0
    capacity_scaler = 1.0
  }
}

resource "google_compute_health_check" "default" {
  provider = google-beta
  name               = "<%= ctx[:vars]['tcp_proxy_health_check'] %>"
  timeout_sec        = 1
  check_interval_sec = 1

  tcp_health_check {
    port = "80"
  }
}

# instance template
resource "google_compute_instance_template" "default" {
  name         = "<%= ctx[:vars]['tcp_proxy_xlb_mig_template'] %>"
  provider     = google-beta
  machine_type = "e2-small"
  tags         = ["allow-health-check"]

  network_interface {
    network    = google_compute_network.default.id
    subnetwork = google_compute_subnetwork.default.id
    access_config {
      # add external ip to fetch packages
    }
  }
  disk {
    source_image = "debian-cloud/debian-10"
    auto_delete  = true
    boot         = true
  }

  # install nginx and serve a simple web page
  metadata = {
    startup-script = <<-EOF1
      #! /bin/bash
      set -euo pipefail
      export DEBIAN_FRONTEND=noninteractive
      apt-get update
      apt-get install -y nginx-light jq
      NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname")
      IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip")
      METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])')
      cat <<EOF > /var/www/html/index.html
      <pre>
      Name: $NAME
      IP: $IP
      Metadata: $METADATA
      </pre>
      EOF
    EOF1
  }
  lifecycle {
    create_before_destroy = true
  }
}

# MIG
resource "google_compute_instance_group_manager" "default" {
  name     = "<%= ctx[:vars]['tcp_proxy_xlb_mig1'] %>"
  provider = google-beta
  zone     = "us-central1-c"
  named_port {
    name = "tcp"
    port = 110
  }
  version {
    instance_template = google_compute_instance_template.default.id
    name              = "primary"
  }
  base_instance_name = "vm"
  target_size        = 2
}

# allow access from health check ranges
resource "google_compute_firewall" "default" {
  name          = "<%= ctx[:vars]['tcp_proxy_xlb_fw_allow_hc'] %>"
  provider      = google-beta
  direction     = "INGRESS"
  network       = google_compute_network.default.id
  source_ranges = ["192.168.127.12/22", "192.168.127.12/16"]
  allow {
    protocol = "tcp"
  }
  target_tags = ["allow-health-check"]
}
# [END cloudloadbalancing_ext_tcp_proxy_lb]<commit_msg>fix: tcp proxy backend to right named port (#6149)<commit_after># External TCP proxy load balancer with managed instance group backend

# [START cloudloadbalancing_ext_tcp_proxy_lb]
# VPC
resource "google_compute_network" "default" {
  name                    = "<%= ctx[:vars]['tcp_proxy_xlb_network'] %>"
  provider                = google-beta
  auto_create_subnetworks = false
}

# backend subnet
resource "google_compute_subnetwork" "default" {
  name          = "<%= ctx[:vars]['tcp_proxy_xlb_subnet'] %>"
  provider      = google-beta
  ip_cidr_range = "10.0.1.0/24"
  region        = "us-central1"
  network       = google_compute_network.default.id
}

# reserved IP address
resource "google_compute_global_address" "default" {
  provider = google-beta
  name = "<%= ctx[:vars]['tcp_proxy_xlb_ip'] %>"
}

# forwarding rule
resource "google_compute_global_forwarding_rule" "<%= ctx[:primary_resource_id] %>" {
  name                  = "<%= ctx[:vars]['tcp_proxy_xlb_forwarding_rule'] %>"
  provider              = google-beta
  ip_protocol           = "TCP"
  load_balancing_scheme = "EXTERNAL"
  port_range            = "110"
  target                = google_compute_target_tcp_proxy.default.id
  ip_address            = google_compute_global_address.default.id
}

resource "google_compute_target_tcp_proxy" "default" {
  provider = google-beta
  name            = "<%= ctx[:vars]['test_proxy_health_check'] %>"
  backend_service = google_compute_backend_service.default.id
}

# backend service
resource "google_compute_backend_service" "default" {
  provider = google-beta
  name                  = "<%= ctx[:vars]['tcp_proxy_xlb_backend_service'] %>"
  protocol              = "TCP"
  port_name             = "tcp"
  load_balancing_scheme = "EXTERNAL"
  timeout_sec           = 10
  health_checks         = [google_compute_health_check.default.id]
  backend {
    group           = google_compute_instance_group_manager.default.instance_group
    balancing_mode  = "UTILIZATION"
    max_utilization = 1.0
    capacity_scaler = 1.0
  }
}

resource "google_compute_health_check" "default" {
  provider = google-beta
  name               = "<%= ctx[:vars]['tcp_proxy_health_check'] %>"
  timeout_sec        = 1
  check_interval_sec = 1

  tcp_health_check {
    port = "80"
  }
}

# instance template
resource "google_compute_instance_template" "default" {
  name         = "<%= ctx[:vars]['tcp_proxy_xlb_mig_template'] %>"
  provider     = google-beta
  machine_type = "e2-small"
  tags         = ["allow-health-check"]

  network_interface {
    network    = google_compute_network.default.id
    subnetwork = google_compute_subnetwork.default.id
    access_config {
      # add external ip to fetch packages
    }
  }
  disk {
    source_image = "debian-cloud/debian-10"
    auto_delete  = true
    boot         = true
  }

  # install nginx and serve a simple web page
  metadata = {
    startup-script = <<-EOF1
      #! /bin/bash
      set -euo pipefail
      export DEBIAN_FRONTEND=noninteractive
      apt-get update
      apt-get install -y nginx-light jq
      NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname")
      IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip")
      METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])')
      cat <<EOF > /var/www/html/index.html
      <pre>
      Name: $NAME
      IP: $IP
      Metadata: $METADATA
      </pre>
      EOF
    EOF1
  }
  lifecycle {
    create_before_destroy = true
  }
}

# MIG
resource "google_compute_instance_group_manager" "default" {
  name     = "<%= ctx[:vars]['tcp_proxy_xlb_mig1'] %>"
  provider = google-beta
  zone     = "us-central1-c"
  named_port {
    name = "tcp"
    port = 80
  }
  version {
    instance_template = google_compute_instance_template.default.id
    name              = "primary"
  }
  base_instance_name = "vm"
  target_size        = 2
}

# allow access from health check ranges
resource "google_compute_firewall" "default" {
  name          = "<%= ctx[:vars]['tcp_proxy_xlb_fw_allow_hc'] %>"
  provider      = google-beta
  direction     = "INGRESS"
  network       = google_compute_network.default.id
  source_ranges = ["192.168.127.12/22", "192.168.127.12/16"]
  allow {
    protocol = "tcp"
  }
  target_tags = ["allow-health-check"]
}
# [END cloudloadbalancing_ext_tcp_proxy_lb]
{'fraction_non_alphanumeric': 0.1176860234047251, 'fraction_numerical': 0.014020755133583572, 'mean_word_length': 2.9233434387180597, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4156137', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2922, 'n_words': 525}