-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.tf
187 lines (159 loc) · 5.69 KB
/
compute.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance
resource "google_compute_instance" "vm1" {
name = "vm1-${var.instance_id}"
machine_type = var.gcp_vms_size
zone = data.google_compute_zones.available.names[0]
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = var.gcp_os_r
}
}
metadata = {
serial-port-logging-enable = "TRUE"
serial-port-enable = true
startup-script-url = "https://raw.githubusercontent.com/ibenrodriguez/iac-demo-gcp/main/init_os_redhat_deps.sh"
}
network_interface {
subnetwork = google_compute_subnetwork.subnet1.name
/* access_config {
// Include this section to give the VM an external ip address
} */
}
}
# Backend service
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_backend_service
resource "google_compute_backend_service" "website" {
name = "backend-service-${var.instance_id}"
description = "backend-service-${var.instance_id}"
protocol = "HTTP"
port_name = "http"
# port_name - Name of backend port. The same name should appear in the instance groups referenced by this service.
load_balancing_scheme = "EXTERNAL"
enable_cdn = false
timeout_sec = 10
security_policy = google_compute_security_policy.security-policy-1.id
# custom_request_headers = ["X-Client-Geo-Location: {client_region_subdivision}, {client_city}"]
# custom_response_headers = ["X-Cache-Hit: {cdn_cache_status}"]
health_checks = [google_compute_health_check.health.id]
backend {
group = google_compute_instance_group_manager.default.instance_group
balancing_mode = "UTILIZATION"
capacity_scaler = 1.0
}
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall
resource "google_compute_firewall" "allow-tcp" {
name = "allow-tcp-armor-firewall-${var.instance_id}"
network = google_compute_network.gcp-network.id
allow {
protocol = "tcp"
ports = ["80", "443"]
}
source_ranges = ["0.0.0.0/0"]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall
resource "google_compute_firewall" "allow-ssh" {
name = "allow-ssh-firewall-${var.instance_id}"
network = google_compute_network.gcp-network.id
direction = "INGRESS"
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
resource "google_compute_firewall" "default-allow-health-check" {
name = "default-allow-health-check"
description = "default-allow-health-check"
direction = "INGRESS"
network = google_compute_network.gcp-network.id
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"]
allow {
protocol = "tcp"
# ports = ["22","80","443"]
}
target_tags = ["allow-health-check","http-server"]
log_config {
metadata = "INCLUDE_ALL_METADATA"
}
}
/* resource "google_compute_firewall" "allow-ssh-eg" {
name = "allow-ssh-firewall-eg-${var.instance_id}"
description = "allow-ssh-firewall-eg-${var.instance_id}"
network = google_compute_network.gcp-network.id
direction = "INGRESS"
allow {
protocol = "tcp"
ports = ["22"]
}
} */
resource "google_compute_firewall" "default-eg" {
name = "allow-tcp-armor-eg-firewall"
description = "allow-tcp-armor-eg-firewall"
direction = "EGRESS"
network = google_compute_network.gcp-network.id
allow {
protocol = "tcp"
ports = ["80", "443"]
}
target_tags = ["allow-health-check","http-server"]
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_http_health_check
resource "google_compute_health_check" "health" {
check_interval_sec = "5"
healthy_threshold = "2"
name = "tcp-http-health-check-${var.instance_id}"
description = "tcp-http-health-check-${var.instance_id}"
tcp_health_check {
port = "80"
proxy_header = "NONE"
}
#http_health_check {
# request_path = "/"
# port = "80"
#}
# https://cloud.google.com/load-balancing/docs/https/ext-http-lb-tf-module-examples
# log_config = {
# enable = true
# sample_rate = 1.0
# }
timeout_sec = "5"
unhealthy_threshold = "2"
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_forwarding_rule
resource "google_compute_global_forwarding_rule" "default" {
name = "armor-rule-${var.instance_id}"
target = google_compute_target_http_proxy.default.id
ip_protocol = "TCP"
load_balancing_scheme = "EXTERNAL"
port_range = "80"
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_target_http_proxy
resource "google_compute_target_http_proxy" "default" {
name = "armor-proxy-${var.instance_id}"
url_map = google_compute_url_map.default.id
}
# https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_url_map
resource "google_compute_url_map" "default" {
name = "armor-url-map-${var.instance_id}"
default_service = google_compute_backend_service.website.id
# host_rule {
# hosts = ["*"]
# path_matcher = "allpaths"
# }
# path_matcher {
# name = "allpaths"
# default_service = google_compute_backend_service.website.self_link
# path_rule {
# paths = ["/*"]
# service = google_compute_backend_service.website.self_link
# }
# }
}