google_compute_backend_service

A Backend Service defines a group of virtual machines that will serve traffic for load balancing. This resource is a global backend service, appropriate for external load balancing or self-managed internal load balancing. For managed internal load balancing, use a regional backend service instead.

Currently self-managed internal load balancing is only available in beta.

To get more information about BackendService, see:

Open in Cloud Shell

Example Usage - Backend Service Basic

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_http_health_check" "default" {
  name               = "health-check"
  request_path       = "/"
  check_interval_sec = 1
  timeout_sec        = 1
}
Open in Cloud Shell

Example Usage - Backend Service External Iap

resource "google_compute_backend_service" "default" {
  name                  = "tf-test-backend-service-external"
  protocol              = "HTTP"
  load_balancing_scheme = "EXTERNAL"
  iap {
    oauth2_client_id     = "abc"
    oauth2_client_secret = "xyz"
  }
}
Open in Cloud Shell

Example Usage - Backend Service Cache Simple

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  health_checks = [google_compute_http_health_check.default.id]
  enable_cdn  = true
  cdn_policy {
    signed_url_cache_max_age_sec = 7200
  }
}

resource "google_compute_http_health_check" "default" {
  name               = "health-check"
  request_path       = "/"
  check_interval_sec = 1
  timeout_sec        = 1
}
Open in Cloud Shell

Example Usage - Backend Service Cache Include Http Headers

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  enable_cdn  = true
  cdn_policy {
    cache_mode = "USE_ORIGIN_HEADERS"
    cache_key_policy {
      include_host = true
      include_protocol = true
      include_query_string = true
      include_http_headers = ["X-My-Header-Field"]
    }
  }
}
Open in Cloud Shell

Example Usage - Backend Service Cache Include Named Cookies

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  enable_cdn  = true
  cdn_policy {
    cache_mode = "CACHE_ALL_STATIC"
    default_ttl = 3600
    client_ttl  = 7200
    max_ttl     = 10800
    cache_key_policy {
      include_host = true
      include_protocol = true
      include_query_string = true
      include_named_cookies = ["__next_preview_data", "__prerender_bypass"]
    }
  }
}
Open in Cloud Shell

Example Usage - Backend Service Cache

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  health_checks = [google_compute_http_health_check.default.id]
  enable_cdn  = true
  cdn_policy {
    cache_mode = "CACHE_ALL_STATIC"
    default_ttl = 3600
    client_ttl  = 7200
    max_ttl     = 10800
    negative_caching = true
    signed_url_cache_max_age_sec = 7200
  }
}

resource "google_compute_http_health_check" "default" {
  name               = "health-check"
  request_path       = "/"
  check_interval_sec = 1
  timeout_sec        = 1
}
Open in Cloud Shell

Example Usage - Backend Service Cache Bypass Cache On Request Headers

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  health_checks = [google_compute_http_health_check.default.id]
  enable_cdn  = true
  cdn_policy {
    cache_mode = "CACHE_ALL_STATIC"
    default_ttl = 3600
    client_ttl  = 7200
    max_ttl     = 10800
    negative_caching = true
    signed_url_cache_max_age_sec = 7200

    bypass_cache_on_request_headers {
      header_name = "Authorization"
    }

    bypass_cache_on_request_headers {
      header_name = "Proxy-Authorization"
    }
  }
}

resource "google_compute_http_health_check" "default" {
  name               = "health-check"
  request_path       = "/"
  check_interval_sec = 1
  timeout_sec        = 1
}
Open in Cloud Shell

Example Usage - Backend Service Traffic Director Round Robin

resource "google_compute_backend_service" "default" {
  provider = google-beta

  name                  = "backend-service"
  health_checks         = [google_compute_health_check.health_check.id]
  load_balancing_scheme = "INTERNAL_SELF_MANAGED"
  locality_lb_policy    = "ROUND_ROBIN"
}

resource "google_compute_health_check" "health_check" {
  provider = google-beta

  name = "health-check"
  http_health_check {
    port = 80
  }
}
Open in Cloud Shell

Example Usage - Backend Service Traffic Director Ring Hash

resource "google_compute_backend_service" "default" {
  provider = google-beta

  name                  = "backend-service"
  health_checks         = [google_compute_health_check.health_check.id]
  load_balancing_scheme = "INTERNAL_SELF_MANAGED"
  locality_lb_policy    = "RING_HASH"
  session_affinity      = "HTTP_COOKIE"
  circuit_breakers {
    max_connections = 10
  }
  consistent_hash {
    http_cookie {
      ttl {
        seconds = 11
        nanos   = 1111
      }
      name = "mycookie"
    }
  }
  outlier_detection {
    consecutive_errors = 2
  }
}

resource "google_compute_health_check" "health_check" {
  provider = google-beta

  name = "health-check"
  http_health_check {
    port = 80
  }
}
Open in Cloud Shell

Example Usage - Backend Service Network Endpoint

resource "google_compute_global_network_endpoint_group" "external_proxy" {
  provider = google-beta
  name                  = "network-endpoint"
  network_endpoint_type = "INTERNET_FQDN_PORT"
  default_port          = "443"
}

resource "google_compute_global_network_endpoint" "proxy" {
  provider = google-beta
  global_network_endpoint_group = google_compute_global_network_endpoint_group.external_proxy.id
  fqdn                          = "test.example.com"
  port                          = google_compute_global_network_endpoint_group.external_proxy.default_port
}

resource "google_compute_backend_service" "default" {
  provider = google-beta
  name                            = "backend-service"
  enable_cdn                      = true
  timeout_sec                     = 10
  connection_draining_timeout_sec = 10

  custom_request_headers          = ["host: ${google_compute_global_network_endpoint.proxy.fqdn}"]
  custom_response_headers         = ["X-Cache-Hit: {cdn_cache_status}"]

  backend {
    group = google_compute_global_network_endpoint_group.external_proxy.id
  }
}
Open in Cloud Shell

Example Usage - Backend Service External Managed

resource "google_compute_backend_service" "default" {
  name          = "backend-service"
  health_checks = [google_compute_health_check.default.id]
  load_balancing_scheme = "EXTERNAL_MANAGED"
}

resource "google_compute_health_check" "default" {
  name = "health-check"
  http_health_check {
    port = 80
  }
}

Argument Reference

The following arguments are supported:


The backend block supports:

The circuit_breakers block supports:

The connect_timeout block supports:

The consistent_hash block supports:

The http_cookie block supports:

The ttl block supports:

The cdn_policy block supports:

The cache_key_policy block supports:

The negative_caching_policy block supports:

The bypass_cache_on_request_headers block supports:

The iap block supports:

The locality_lb_policies block supports:

The policy block supports:

The custom_policy block supports:

The outlier_detection block supports:

The base_ejection_time block supports:

The interval block supports:

The security_settings block supports:

The log_config block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

This resource provides the following Timeouts configuration options:

Import

BackendService can be imported using any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import BackendService using one of the formats above. For example:

import {
  id = "projects/{{project}}/global/backendServices/{{name}}"
  to = google_compute_backend_service.default
}

When using the terraform import command, BackendService can be imported using one of the formats above. For example:

$ terraform import google_compute_backend_service.default projects/{{project}}/global/backendServices/{{name}}
$ terraform import google_compute_backend_service.default {{project}}/{{name}}
$ terraform import google_compute_backend_service.default {{name}}

User Project Overrides

This resource supports User Project Overrides.