Skip to content

Latest commit

 

History

History
 
 

net-lb-app-ext

External Application Load Balancer Module

This module allows managing Global HTTP/HTTPS Classic Load Balancers (GLBs). It's designed to expose the full configuration of the underlying resources, and to facilitate common usage patterns by providing sensible defaults, and optionally managing prerequisite resources like health checks, instance groups, etc.

Due to the complexity of the underlying resources, changes to the configuration that involve recreation of resources are best applied in stages, starting by disabling the configuration in the urlmap that references the resources that need recreation, then doing the same for the backend service, etc.

Examples

Minimal HTTP Example

An HTTP load balancer with a backend service pointing to a GCE instance group:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
        { backend = "projects/myprj/zones/europe-west8-c/instanceGroups/myig-c" },
      ]
    }
  }
}
# tftest modules=1 resources=5

Minimal HTTPS examples

HTTP backends

An HTTPS load balancer needs a certificate and backends can be HTTP or HTTPS. THis is an example With HTTP backends and a managed certificate:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
        { backend = "projects/myprj/zones/europe-west8-c/instanceGroups/myig-c" },
      ]
      protocol = "HTTP"
    }
  }
  protocol = "HTTPS"
  ssl_certificates = {
    managed_configs = {
      default = {
        domains = ["glb-test-0.example.org"]
      }
    }
  }
}
# tftest modules=1 resources=6

HTTPS backends

For HTTPS backends the backend service protocol needs to be set to HTTPS. The port name if omitted is inferred from the protocol, in this case it is set internally to https. The health check also needs to be set to https. This is a complete example:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
        { backend = "projects/myprj/zones/europe-west8-c/instanceGroups/myig-c" },
      ]
      protocol = "HTTPS"
    }
  }
  health_check_configs = {
    default = {
      https = {
        port_specification = "USE_SERVING_PORT"
      }
    }
  }
  protocol = "HTTPS"
  ssl_certificates = {
    managed_configs = {
      default = {
        domains = ["glb-test-0.example.org"]
      }
    }
  }
}
# tftest modules=1 resources=6

HTTP to HTTPS redirect

Redirect is implemented via an additional HTTP load balancer with a custom URL map, similarly to how it's done via the GCP Console. The address shared by the two load balancers needs to be reserved.

module "addresses" {
  source     = "./fabric/modules/net-address"
  project_id = "myprj"
  global_addresses = {
    "glb-test-0" = {}
  }
}

module "glb-test-0-redirect" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0-redirect"
  address = (
    module.addresses.global_addresses["glb-test-0"].address
  )
  health_check_configs = {}
  urlmap_config = {
    description = "URL redirect for glb-test-0."
    default_url_redirect = {
      https         = true
      response_code = "MOVED_PERMANENTLY_DEFAULT"
    }
  }
}

module "glb-test-0" {
  source              = "./fabric/modules/net-lb-app-ext"
  project_id          = "myprj"
  name                = "glb-test-0"
  use_classic_version = false
  address = (
    module.addresses.global_addresses["glb-test-0"].address
  )
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
      ]
      protocol = "HTTP"
    }
  }
  protocol = "HTTPS"
  ssl_certificates = {
    managed_configs = {
      default = {
        domains = ["glb-test.example.com"]
      }
    }
  }
}

# tftest modules=3 resources=10

Classic vs Non-classic

The module uses a classic Global Load Balancer by default. To use the non-classic version set the use_classic_version variable to false as in the following example, note that the module is not enforcing feature sets between the two versions:

module "glb-0" {
  source              = "./fabric/modules/net-lb-app-ext"
  project_id          = "myprj"
  name                = "glb-test-0"
  use_classic_version = false
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
        { backend = "projects/myprj/zones/europe-west8-c/instanceGroups/myig-c" },
      ]
    }
  }
}
# tftest modules=1 resources=5

Health Checks

You can leverage externally defined health checks for backend services, or have the module create them for you.

By default a simple HTTP health check named default is created and used in backend services. If you need to override the default, simply define your own health check using the same key (default). For more complex configurations you can define your own health checks and reference them via keys in the backend service configurations.

Health checks created by this module are controlled via the health_check_configs variable, which behaves in a similar way to other LB modules in this repository. This is an example that overrides the default health check configuration using a TCP health check:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = var.project_id
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [{
        backend = "projects/myprj/zones/europe-west1-a/instanceGroups/my-ig"
      }]
      # no need to reference the hc explicitly when using the `default` key
      # health_checks = ["default"]
    }
  }
  health_check_configs = {
    default = {
      tcp = { port = 80 }
    }
  }
}
# tftest modules=1 resources=5

To leverage existing health checks without having the module create them, simply pass their self links to backend services and set the health_check_configs variable to an empty map:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = var.project_id
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [{
        backend = "projects/myprj/zones/europe-west1-a/instanceGroups/my-ig"
      }]
      health_checks = ["projects/myprj/global/healthChecks/custom"]
    }
  }
  health_check_configs = {}
}
# tftest modules=1 resources=4

Backend Types and Management

Instance Groups

The module can optionally create unmanaged instance groups, which can then be referred to in backends via their key. THis is the simple HTTP example above but with instance group creation managed by the module:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "default-b" }
      ]
    }
  }
  group_configs = {
    default-b = {
      zone = "europe-west8-b"
      instances = [
        "projects/myprj/zones/europe-west8-b/instances/vm-a"
      ]
      named_ports = { http = 80 }
    }
  }
}
# tftest modules=1 resources=6

Managed Instance Groups

This example shows how to use the module with a manage instance group as backend:

module "win-template" {
  source          = "./fabric/modules/compute-vm"
  project_id      = "myprj"
  zone            = "europe-west8-a"
  name            = "win-template"
  instance_type   = "n2d-standard-2"
  create_template = true
  boot_disk = {
    initialize_params = {
      image = "projects/windows-cloud/global/images/windows-server-2019-dc-v20221214"
      size  = 70
    }
  }
  network_interfaces = [{
    network    = var.vpc.self_link
    subnetwork = var.subnet.self_link
    nat        = false
    addresses  = null
  }]
}

module "win-mig" {
  source            = "./fabric/modules/compute-mig"
  project_id        = "myprj"
  location          = "europe-west8-a"
  name              = "win-mig"
  instance_template = module.win-template.template.self_link
  autoscaler_config = {
    max_replicas    = 3
    min_replicas    = 1
    cooldown_period = 30
    scaling_signals = {
      cpu_utilization = {
        target = 0.80
      }
    }
  }
  named_ports = {
    http = 80
  }
}

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = module.win-mig.group_manager.instance_group }
      ]
    }
  }
}
# tftest modules=3 resources=8

Storage Buckets

GCS bucket backends can also be managed and used in this module in a similar way to regular backend services.Multiple GCS bucket backends can be defined and referenced in URL maps by their keys (or self links if defined externally) together with regular backend services, an example is provided later in this document. This is a simple example that defines a GCS backend as the default for the URL map:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_buckets_config = {
    default = {
      bucket_name = "tf-playground-svpc-gce-public"
    }
  }
  # with a single GCS backend the implied default health check is not needed
  health_check_configs = {}
}
# tftest modules=1 resources=4

Network Endpoint Groups (NEGs)

Supported Network Endpoint Groups (NEGs) can also be used as backends. Similarly to groups, you can pass a self link for existing NEGs or have the module manage them for you. A simple example using an existing zonal NEG:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        {
          backend        = "projects/myprj/zones/europe-west8-b/networkEndpointGroups/myneg-b"
          balancing_mode = "RATE"
          max_rate       = { per_endpoint = 10 }
        }
      ]
    }
  }
}
# tftest modules=1 resources=5

Zonal NEG creation

This example shows how to create and manage zonal NEGs using GCE VMs as endpoints:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        {
          backend        = "neg-0"
          balancing_mode = "RATE"
          max_rate       = { per_endpoint = 10 }
        }
      ]
    }
  }
  neg_configs = {
    neg-0 = {
      gce = {
        network    = "projects/myprj-host/global/networks/svpc"
        subnetwork = "projects/myprj-host/regions/europe-west8/subnetworks/gce"
        zone       = "europe-west8-b"
        endpoints = {
          e-0 = {
            instance   = "myinstance-b-0"
            ip_address = "10.24.32.25"
            port       = 80
          }
        }
      }
    }
  }
}
# tftest modules=1 resources=7

Hybrid NEG creation

This example shows how to create and manage hybrid NEGs:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        {
          backend        = "neg-0"
          balancing_mode = "RATE"
          max_rate       = { per_endpoint = 10 }
        }
      ]
    }
  }
  neg_configs = {
    neg-0 = {
      hybrid = {
        network = "projects/myprj-host/global/networks/svpc"
        zone    = "europe-west8-b"
        endpoints = {
          e-0 = {
            ip_address = "10.0.0.10"
            port       = 80
          }
        }
      }
    }
  }
}
# tftest modules=1 resources=7

Internet NEG creation

This example shows how to create and manage internet NEGs:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "neg-0" }
      ]
      health_checks = []
    }
  }
  # with a single internet NEG the implied default health check is not needed
  health_check_configs = {}
  neg_configs = {
    neg-0 = {
      internet = {
        use_fqdn = true
        endpoints = {
          e-0 = {
            destination = "www.example.org"
            port        = 80
          }
        }
      }
    }
  }
}
# tftest modules=1 resources=6

Private Service Connect NEG creation

The module supports managing PSC NEGs if the non-classic version of the load balancer is used:

module "glb-0" {
  source              = "./fabric/modules/net-lb-app-ext"
  project_id          = "myprj"
  name                = "glb-test-0"
  use_classic_version = false
  backend_service_configs = {
    default = {
      backends = [
        { backend = "neg-0" }
      ]
      health_checks = []
    }
  }
  # with a single PSC NEG the implied default health check is not needed
  health_check_configs = {}
  neg_configs = {
    neg-0 = {
      psc = {
        region         = "europe-west8"
        target_service = "europe-west8-cloudkms.googleapis.com"
      }
    }
  }
}
# tftest modules=1 resources=5

Serverless NEG creation

The module supports managing Serverless NEGs for Cloud Run and Cloud Function. This is an example of a Cloud Run NEG:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "neg-0" }
      ]
      health_checks = []
    }
  }
  # with a single serverless NEG the implied default health check is not needed
  health_check_configs = {}
  neg_configs = {
    neg-0 = {
      cloudrun = {
        region = "europe-west8"
        target_service = {
          name = "hello"
        }
      }
    }
  }
}
# tftest modules=1 resources=5

Serverless NEGs don't use the port name but it should be set to http. An HTTPS frontend requires the protocol to be set to HTTPS, and the port name field will infer this value if omitted so you need to set it explicitly:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "neg-0" }
      ]
      health_checks = []
      port_name     = "http"
    }
  }
  # with a single serverless NEG the implied default health check is not needed
  health_check_configs = {}
  neg_configs = {
    neg-0 = {
      cloudrun = {
        region = "europe-west8"
        target_service = {
          name = "hello"
        }
      }
    }
  }
  protocol = "HTTPS"
  ssl_certificates = {
    managed_configs = {
      default = {
        domains = ["glb-test-0.example.org"]
      }
    }
  }
}
# tftest modules=1 resources=6 inventory=https-sneg.yaml

URL Map

The module exposes the full URL map resource configuration, with some minor changes to the interface to decrease verbosity, and support for aliasing backend services via keys.

The default URL map configuration sets the default backend service as the default service for the load balancer as a convenience. Just override the urlmap_config variable to change the default behaviour:

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [{
        backend = "projects/myprj/zones/europe-west8-b/instanceGroups/ig-0"
      }]
    }
    other = {
      backends = [{
        backend = "projects/myprj/zones/europe-west8-c/instanceGroups/ig-1"
      }]
    }
  }
  urlmap_config = {
    default_service = "default"
    host_rules = [{
      hosts        = ["*"]
      path_matcher = "pathmap"
    }]
    path_matchers = {
      pathmap = {
        default_service = "default"
        path_rules = [{
          paths   = ["/other", "/other/*"]
          service = "other"
        }]
      }
    }
  }
}

# tftest modules=1 resources=6

SSL Certificates

The module also allows managing managed and self-managed SSL certificates via the ssl_certificates variable. Any certificate defined there will be added to the HTTPS proxy resource.

THe HTTPS example above shows how to configure manage certificated, the following example shows how to use an unmanaged (or self managed) certificate. The example uses Terraform resource for the key and certificate so that the we don't depend on external files when running tests, in real use the key and certificate are generally provided via external files read by the Terraform file() function.

resource "tls_private_key" "default" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "tls_self_signed_cert" "default" {
  private_key_pem = tls_private_key.default.private_key_pem
  subject {
    common_name  = "example.com"
    organization = "ACME Examples, Inc"
  }
  validity_period_hours = 720
  allowed_uses = [
    "key_encipherment",
    "digital_signature",
    "server_auth",
  ]
}

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_service_configs = {
    default = {
      backends = [
        { backend = "projects/myprj/zones/europe-west8-b/instanceGroups/myig-b" },
        { backend = "projects/myprj/zones/europe-west8-c/instanceGroups/myig-c" },
      ]
      protocol = "HTTP"
    }
  }
  protocol = "HTTPS"
  ssl_certificates = {
    create_configs = {
      default = {
        # certificate and key could also be read via file() from external files
        certificate = tls_self_signed_cert.default.cert_pem
        private_key = tls_private_key.default.private_key_pem
      }
    }
  }
}
# tftest modules=1 resources=8

Complex example

This example mixes group and NEG backends, and shows how to set HTTPS for specific backends.

module "glb-0" {
  source     = "./fabric/modules/net-lb-app-ext"
  project_id = "myprj"
  name       = "glb-test-0"
  backend_buckets_config = {
    gcs-0 = {
      bucket_name = "my-bucket"
    }
  }
  backend_service_configs = {
    default = {
      backends = [
        { backend = "ew8-b" },
        { backend = "ew8-c" },
      ]
    }
    neg-gce-0 = {
      backends = [{
        balancing_mode = "RATE"
        backend        = "neg-ew8-c"
        max_rate       = { per_endpoint = 10 }
      }]
    }
    neg-hybrid-0 = {
      backends = [{
        backend = "neg-hello"
      }]
      health_checks = ["neg"]
      protocol      = "HTTPS"
    }
  }
  group_configs = {
    ew8-b = {
      zone = "europe-west8-b"
      instances = [
        "projects/prj-gce/zones/europe-west8-b/instances/nginx-ew8-b"
      ]
      named_ports = { http = 80 }
    }
    ew8-c = {
      zone = "europe-west8-c"
      instances = [
        "projects/prj-gce/zones/europe-west8-c/instances/nginx-ew8-c"
      ]
      named_ports = { http = 80 }
    }
  }
  health_check_configs = {
    default = {
      http = {
        port = 80
      }
    }
    neg = {
      https = {
        host = "hello.example.com"
        port = 443
      }
    }
  }
  neg_configs = {
    neg-ew8-c = {
      gce = {
        network    = "projects/myprj-host/global/networks/svpc"
        subnetwork = "projects/myprj-host/regions/europe-west8/subnetworks/gce"
        zone       = "europe-west8-c"
        endpoints = {
          e-0 = {
            instance   = "nginx-ew8-c"
            ip_address = "10.24.32.26"
            port       = 80
          }
        }
      }
    }
    neg-hello = {
      hybrid = {
        network = "projects/myprj-host/global/networks/svpc"
        zone    = "europe-west8-b"
        endpoints = {
          e-0 = {
            ip_address = "192.168.0.3"
            port       = 443
          }
        }
      }
    }
  }
  urlmap_config = {
    default_service = "default"
    host_rules = [
      {
        hosts        = ["*"]
        path_matcher = "gce"
      },
      {
        hosts        = ["hello.example.com"]
        path_matcher = "hello"
      },
      {
        hosts        = ["static.example.com"]
        path_matcher = "static"
      }
    ]
    path_matchers = {
      gce = {
        default_service = "default"
        path_rules = [
          {
            paths   = ["/gce-neg", "/gce-neg/*"]
            service = "neg-gce-0"
          }
        ]
      }
      hello = {
        default_service = "neg-hybrid-0"
      }
      static = {
        default_service = "gcs-0"
      }
    }
  }
}
# tftest modules=1 resources=15

Files

name description resources
backend-service.tf Backend service resources. google_compute_backend_service
backends.tf Backend groups and backend buckets resources. google_compute_backend_bucket
groups.tf None google_compute_instance_group
health-check.tf Health check resource. google_compute_health_check
main.tf Module-level locals and resources. google_compute_global_forwarding_rule · google_compute_managed_ssl_certificate · google_compute_ssl_certificate · google_compute_target_http_proxy · google_compute_target_https_proxy
negs.tf NEG resources. google_compute_global_network_endpoint · google_compute_global_network_endpoint_group · google_compute_network_endpoint · google_compute_network_endpoint_group · google_compute_region_network_endpoint_group
outputs.tf Module outputs.
urlmap.tf URL map resources. google_compute_url_map
variables-backend-service.tf Backend services variables.
variables-health-check.tf Health check variable.
variables-urlmap.tf URLmap variable.
variables.tf Module variables.
versions.tf Version pins.

Variables

name description type required default
name Load balancer name. string
project_id Project id. string
address Optional IP address used for the forwarding rule. string null
backend_buckets_config Backend buckets configuration. map(object({…})) {}
backend_service_configs Backend service level configuration. map(object({…})) {}
description Optional description used for resources. string "Terraform managed."
group_configs Optional unmanaged groups to create. Can be referenced in backends via key or outputs. map(object({…})) {}
health_check_configs Optional auto-created health check configurations, use the output self-link to set it in the auto healing policy. Refer to examples for usage. map(object({…})) {…}
https_proxy_config HTTPS proxy connfiguration. object({…}) {}
labels Labels set on resources. map(string) {}
neg_configs Optional network endpoint groups to create. Can be referenced in backends via key or outputs. map(object({…})) {}
ports Optional ports for HTTP load balancer, valid ports are 80 and 8080. list(string) null
protocol Protocol supported by this load balancer. string "HTTP"
ssl_certificates SSL target proxy certificates (only if protocol is HTTPS) for existing, custom, and managed certificates. object({…}) {}
urlmap_config The URL map configuration. object({…}) {…}
use_classic_version Use classic Global Load Balancer. bool true

Outputs

name description sensitive
address Forwarding rule address.
backend_service_ids Backend service resources.
backend_service_names Backend service resource names.
forwarding_rule Forwarding rule resource.
global_neg_ids Autogenerated global network endpoint group ids.
group_ids Autogenerated instance group ids.
health_check_ids Autogenerated health check ids.
id Fully qualified forwarding rule id.
neg_ids Autogenerated network endpoint group ids.
psc_neg_ids Autogenerated PSC network endpoint group ids.
serverless_neg_ids Autogenerated serverless network endpoint group ids.