-
Notifications
You must be signed in to change notification settings - Fork 205
/
main.tf
157 lines (146 loc) · 5.79 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# External HTTP load balancer with an CDN-enabled managed instance group backend
# [START cloudloadbalancing_external_http_lb_mig_backend_parent_tag]
# [START cloudloadbalancing_ext_http_gce_instance_template]
resource "google_compute_instance_template" "default" {
name = "lb-backend-template"
disk {
auto_delete = true
boot = true
device_name = "persistent-disk-0"
mode = "READ_WRITE"
source_image = "projects/debian-cloud/global/images/family/debian-11"
type = "PERSISTENT"
}
labels = {
managed-by-cnrm = "true"
}
machine_type = "n1-standard-1"
metadata = {
startup-script = "#! /bin/bash\n sudo apt-get update\n sudo apt-get install apache2 -y\n sudo a2ensite default-ssl\n sudo a2enmod ssl\n vm_hostname=\"$(curl -H \"Metadata-Flavor:Google\" \\\n http://169.254.169.254/computeMetadata/v1/instance/name)\"\n sudo echo \"Page served from: $vm_hostname\" | \\\n tee /var/www/html/index.html\n sudo systemctl restart apache2"
}
network_interface {
access_config {
network_tier = "PREMIUM"
}
network = "global/networks/default"
subnetwork = "regions/us-east1/subnetworks/default"
}
region = "us-east1"
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
provisioning_model = "STANDARD"
}
service_account {
email = "default"
scopes = ["https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring.write", "https://www.googleapis.com/auth/pubsub", "https://www.googleapis.com/auth/service.management.readonly", "https://www.googleapis.com/auth/servicecontrol", "https://www.googleapis.com/auth/trace.append"]
}
tags = ["allow-health-check"]
}
# [END cloudloadbalancing_ext_http_gce_instance_template]
# [START cloudloadbalancing_ext_http_gce_instance_mig]
resource "google_compute_instance_group_manager" "default" {
name = "lb-backend-example"
zone = "us-east1-b"
named_port {
name = "http"
port = 80
}
version {
instance_template = google_compute_instance_template.default.id
name = "primary"
}
base_instance_name = "vm"
target_size = 2
}
# [END cloudloadbalancing_ext_http_gce_instance_mig]
# [START cloudloadbalancing_ext_http_gce_instance_firewall_rule]
resource "google_compute_firewall" "default" {
name = "fw-allow-health-check"
direction = "INGRESS"
network = "global/networks/default"
priority = 1000
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
target_tags = ["allow-health-check"]
allow {
ports = ["80"]
protocol = "tcp"
}
}
# [END cloudloadbalancing_ext_http_gce_instance_firewall_rule]
# [START cloudloadbalancing_ext_http_gce_instance_ip_address]
resource "google_compute_global_address" "default" {
name = "lb-ipv4-1"
ip_version = "IPV4"
}
# [END cloudloadbalancing_ext_http_gce_instance_ip_address]
# [START cloudloadbalancing_ext_http_gce_instance_health_check]
resource "google_compute_health_check" "default" {
name = "http-basic-check"
check_interval_sec = 5
healthy_threshold = 2
http_health_check {
port = 80
port_specification = "USE_FIXED_PORT"
proxy_header = "NONE"
request_path = "/"
}
timeout_sec = 5
unhealthy_threshold = 2
}
# [END cloudloadbalancing_ext_http_gce_instance_health_check]
# [START cloudloadbalancing_ext_http_gce_instance_backend_service]
resource "google_compute_backend_service" "default" {
name = "web-backend-service"
connection_draining_timeout_sec = 0
health_checks = [google_compute_health_check.default.id]
load_balancing_scheme = "EXTERNAL_MANAGED"
port_name = "http"
protocol = "HTTP"
session_affinity = "NONE"
timeout_sec = 30
backend {
group = google_compute_instance_group_manager.default.instance_group
balancing_mode = "UTILIZATION"
capacity_scaler = 1.0
}
}
# [END cloudloadbalancing_ext_http_gce_instance_backend_service]
# [START cloudloadbalancing_ext_http_gce_instance_url_map]
resource "google_compute_url_map" "default" {
name = "web-map-http"
default_service = google_compute_backend_service.default.id
}
# [END cloudloadbalancing_ext_http_gce_instance_url_map]
# [START cloudloadbalancing_ext_http_gce_instance_target_http_proxy]
resource "google_compute_target_http_proxy" "default" {
name = "http-lb-proxy"
url_map = google_compute_url_map.default.id
}
# [END cloudloadbalancing_ext_http_gce_instance_target_http_proxy]
# [START cloudloadbalancing_ext_http_gce_instance_forwarding_rule]
resource "google_compute_global_forwarding_rule" "default" {
name = "http-content-rule"
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL_MANAGED"
port_range = "80-80"
target = google_compute_target_http_proxy.default.id
ip_address = google_compute_global_address.default.id
}
# [END cloudloadbalancing_ext_http_gce_instance_forwarding_rule]
# [END cloudloadbalancing_external_http_lb_mig_backend_parent_tag]