2015-08-20 19:53:23 +00:00
---
layout: "google"
page_title: "Google: google_compute_backend_service"
2015-10-23 14:10:41 +00:00
sidebar_current: "docs-google-compute-backend-service"
2015-08-20 19:53:23 +00:00
description: |-
Creates a Backend Service resource for Google Compute Engine.
---
# google\_compute\_backend\_service
2017-06-14 21:13:52 +00:00
A Backend Service defines a group of virtual machines that will serve traffic for load balancing. For more information
2017-06-07 22:17:49 +00:00
see [the official documentation ](https://cloud.google.com/compute/docs/load-balancing/http/backend-service )
and the [API ](https://cloud.google.com/compute/docs/reference/latest/backendServices ).
2015-08-20 19:53:23 +00:00
2017-05-09 10:10:14 +00:00
For internal load balancing, use a [google_compute_region_backend_service ](/docs/providers/google/r/compute_region_backend_service.html ).
2015-08-20 19:53:23 +00:00
## Example Usage
2017-04-17 10:17:54 +00:00
```hcl
2017-06-14 21:13:52 +00:00
resource "google_compute_backend_service" "website" {
2017-06-14 21:17:34 +00:00
name = "my-backend"
2017-06-14 21:13:52 +00:00
description = "Our company website"
2016-04-10 21:34:15 +00:00
port_name = "http"
protocol = "HTTP"
timeout_sec = 10
2016-07-28 20:38:09 +00:00
enable_cdn = false
2016-04-10 21:34:15 +00:00
backend {
2017-06-14 21:13:52 +00:00
group = "${google_compute_instance_group_manager.webservers.instance_group}"
2016-04-10 21:34:15 +00:00
}
health_checks = ["${google_compute_http_health_check.default.self_link}"]
2015-08-20 19:53:23 +00:00
}
2017-06-14 21:13:52 +00:00
resource "google_compute_instance_group_manager" "webservers" {
2017-06-14 21:17:34 +00:00
name = "my-webservers"
2017-06-14 21:13:52 +00:00
instance_template = "${google_compute_instance_template.webserver.self_link}"
base_instance_name = "webserver"
2016-04-10 21:34:15 +00:00
zone = "us-central1-f"
target_size = 1
2015-09-08 10:57:51 +00:00
}
2017-06-14 21:13:52 +00:00
resource "google_compute_instance_template" "webserver" {
2017-06-14 21:17:34 +00:00
name = "standard-webserver"
2016-04-10 21:34:15 +00:00
machine_type = "n1-standard-1"
network_interface {
network = "default"
}
disk {
2018-08-28 23:58:27 +00:00
source_image = "debian-cloud/debian-9"
2016-04-10 21:34:15 +00:00
auto_delete = true
boot = true
}
2015-09-08 10:57:51 +00:00
}
2015-08-20 19:53:23 +00:00
resource "google_compute_http_health_check" "default" {
2016-04-10 21:34:15 +00:00
name = "test"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
2015-08-20 19:53:23 +00:00
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the backend service.
2016-04-10 21:34:15 +00:00
2017-06-20 17:45:58 +00:00
* `health_checks` - (Required) Specifies a list of HTTP/HTTPS health checks
2017-06-20 17:26:09 +00:00
for checking the health of the backend service. Currently at most one health
check can be specified, and a health check is required.
2016-04-10 21:34:15 +00:00
- - -
2017-06-07 22:17:49 +00:00
* `backend` - (Optional) The list of backends that serve this BackendService. Structure is documented below.
2016-04-10 21:34:15 +00:00
2017-12-11 17:43:27 +00:00
* `iap` - (Optional) Specification for the Identity-Aware proxy. Disabled if not specified. Structure is documented below.
2018-03-16 17:50:32 +00:00
* `cdn_policy` - (Optional) Cloud CDN configuration for this BackendService. Structure is documented below.
2018-03-22 16:53:24 +00:00
* `connection_draining_timeout_sec` - (Optional) Time for which instance will be drained (not accept new connections,
but still work to finish started ones). Defaults to `300` .
2018-05-29 20:29:40 +00:00
* `custom_request_headers` - (Optional, [Beta ](/docs/providers/google/index.html#beta-features )) Headers that the
HTTP/S load balancer should add to proxied requests. See [guide ](https://cloud.google.com/compute/docs/load-balancing/http/backend-service#user-defined-request-headers ) for details.
2018-10-04 20:20:35 +00:00
This property is in beta, and should be used with the terraform-provider-google-beta provider.
See [Provider Versions ](https://terraform.io/docs/provider/google/provider_versions.html ) for more details on beta fields.
2018-05-29 20:29:40 +00:00
2015-08-20 19:53:23 +00:00
* `description` - (Optional) The textual description for the backend service.
2016-07-28 20:38:09 +00:00
* `enable_cdn` - (Optional) Whether or not to enable the Cloud CDN on the backend service.
2016-04-10 21:34:15 +00:00
* `port_name` - (Optional) The name of a service that has been added to an
instance group in this backend. See [related docs ](https://cloud.google.com/compute/docs/instance-groups/#specifying_service_endpoints ) for details. Defaults to http.
2018-03-12 20:59:47 +00:00
* `project` - (Optional) The ID of the project in which the resource belongs. If it
2016-04-10 21:34:15 +00:00
is not provided, the provider project is used.
* `protocol` - (Optional) The protocol for incoming requests. Defaults to
`HTTP` .
2018-03-22 16:53:24 +00:00
* `security_policy` - (Optional, [Beta ](/docs/providers/google/index.html#beta-features )) Name or URI of a
[security policy ](https://cloud.google.com/armor/docs/security-policy-concepts ) to add to the backend service.
2017-01-16 21:42:55 +00:00
* `session_affinity` - (Optional) How to distribute load. Options are `NONE` (no
affinity), `CLIENT_IP` (hash of the source/dest addresses / ports), and
`GENERATED_COOKIE` (distribute load using a generated session cookie).
2016-11-27 19:12:56 +00:00
2015-08-20 19:53:23 +00:00
* `timeout_sec` - (Optional) The number of secs to wait for a backend to respond
2016-04-10 21:34:15 +00:00
to a request before considering the request failed. Defaults to `30` .
2017-06-14 21:13:52 +00:00
2017-06-07 22:17:49 +00:00
The `backend` block supports:
2015-08-20 19:53:23 +00:00
2016-04-10 21:34:15 +00:00
* `group` - (Required) The name or URI of a Compute Engine instance group
(`google_compute_instance_group_manager.xyz.instance_group`) that can
receive traffic.
* `balancing_mode` - (Optional) Defines the strategy for balancing load.
Defaults to `UTILIZATION`
* `capacity_scaler` - (Optional) A float in the range [0, 1.0] that scales the
maximum parameters for the group (e.g., max rate). A value of 0.0 will cause
no requests to be sent to the group (i.e., it adds the group in a drained
state). The default is 1.0.
2015-08-20 19:53:23 +00:00
* `description` - (Optional) Textual description for the backend.
2016-04-10 21:34:15 +00:00
* `max_rate` - (Optional) Maximum requests per second (RPS) that the group can
handle.
* `max_rate_per_instance` - (Optional) The maximum per-instance requests per
second (RPS).
2018-04-19 21:00:34 +00:00
* `max_connections` - (Optional) The max number of simultaneous connections for the
group. Can be used with either CONNECTION or UTILIZATION balancing
modes. For CONNECTION mode, either maxConnections or
maxConnectionsPerInstance must be set.
* `max_connections_per_instance` - (Optional) The max number of simultaneous connections
that a single backend instance can handle. This is used to calculate
the capacity of the group. Can be used in either CONNECTION or
UTILIZATION balancing modes. For CONNECTION mode, either
maxConnections or maxConnectionsPerInstance must be set.
2016-04-10 21:34:15 +00:00
* `max_utilization` - (Optional) The target CPU utilization for the group as a
float in the range [0.0, 1.0]. This flag can only be provided when the
balancing mode is `UTILIZATION` . Defaults to `0.8` .
2015-08-20 19:53:23 +00:00
2018-03-16 17:50:32 +00:00
The `cdn_policy` block supports:
* `cache_key_policy` - (Optional) The CacheKeyPolicy for this CdnPolicy.
Structure is documented below.
The `cache_key_policy` block supports:
* `include_host` - (Optional) If true, requests to different hosts will be cached separately.
* `include_protocol` - (Optional) If true, http and https requests will be cached separately.
* `include_query_string` - (Optional) If true, include query string parameters in the cache key
according to `query_string_whitelist` and `query_string_blacklist` . If neither is set, the entire
query string will be included. If false, the query string will be excluded from the cache key entirely.
* `query_string_blacklist` - (Optional) Names of query string parameters to exclude in cache keys.
All other parameters will be included. Either specify `query_string_whitelist` or
`query_string_blacklist` , not both. '& ' and '=' will be percent encoded and not treated as delimiters.
* `query_string_whitelist` - (Optional) Names of query string parameters to include in cache keys.
All other parameters will be excluded. Either specify `query_string_whitelist` or
`query_string_blacklist` , not both. '& ' and '=' will be percent encoded and not treated as delimiters.
2017-12-11 17:43:27 +00:00
The `iap` block supports:
* `oauth2_client_id` - (Required) The client ID for use with OAuth 2.0.
* `oauth2_client_secret` - (Required) The client secret for use with OAuth 2.0.
2015-08-20 19:53:23 +00:00
## Attributes Reference
2016-04-10 21:34:15 +00:00
In addition to the arguments listed above, the following computed attributes are
exported:
* `fingerprint` - The fingerprint of the backend service.
2015-08-20 19:53:23 +00:00
* `self_link` - The URI of the created resource.
2017-06-14 21:13:52 +00:00
## Import
Backend services can be imported using the `name` , e.g.
```
2017-06-14 21:17:34 +00:00
$ terraform import google_compute_backend_service.website my-backend
2017-06-14 21:13:52 +00:00
```