Skip to content

Commit 9c4413e

Browse files
Adding end-to-end example for L7 ILB (#5064) (#3535)
Co-authored-by: Cameron Thornton <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Cameron Thornton <[email protected]>
1 parent 2245550 commit 9c4413e

File tree

3 files changed

+414
-0
lines changed

3 files changed

+414
-0
lines changed

.changelog/5064.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```release-note:none
2+
```

google-beta/resource_compute_forwarding_rule_generated_test.go

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,219 @@ import (
2323
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2424
)
2525

26+
func TestAccComputeForwardingRule_internalHttpLbWithMigBackendExample(t *testing.T) {
27+
t.Parallel()
28+
29+
context := map[string]interface{}{
30+
"random_suffix": randString(t, 10),
31+
}
32+
33+
vcrTest(t, resource.TestCase{
34+
PreCheck: func() { testAccPreCheck(t) },
35+
Providers: testAccProvidersOiCS,
36+
CheckDestroy: testAccCheckComputeForwardingRuleDestroyProducer(t),
37+
Steps: []resource.TestStep{
38+
{
39+
Config: testAccComputeForwardingRule_internalHttpLbWithMigBackendExample(context),
40+
},
41+
},
42+
})
43+
}
44+
45+
func testAccComputeForwardingRule_internalHttpLbWithMigBackendExample(context map[string]interface{}) string {
46+
return Nprintf(`
47+
# Internal HTTP load balancer with a managed instance group backend
48+
49+
# VPC
50+
resource "google_compute_network" "ilb_network" {
51+
name = "tf-test-l7-ilb-network%{random_suffix}"
52+
provider = google-beta
53+
auto_create_subnetworks = false
54+
}
55+
56+
# proxy-only subnet
57+
resource "google_compute_subnetwork" "proxy_subnet" {
58+
name = "tf-test-l7-ilb-proxy-subnet%{random_suffix}"
59+
provider = google-beta
60+
ip_cidr_range = "10.0.0.0/24"
61+
region = "europe-west1"
62+
purpose = "INTERNAL_HTTPS_LOAD_BALANCER"
63+
role = "ACTIVE"
64+
network = google_compute_network.ilb_network.id
65+
}
66+
67+
# backed subnet
68+
resource "google_compute_subnetwork" "ilb_subnet" {
69+
name = "tf-test-l7-ilb-subnet%{random_suffix}"
70+
provider = google-beta
71+
ip_cidr_range = "10.0.1.0/24"
72+
region = "europe-west1"
73+
network = google_compute_network.ilb_network.id
74+
}
75+
76+
# forwarding rule
77+
resource "google_compute_forwarding_rule" "google_compute_forwarding_rule" {
78+
name = "tf-test-l7-ilb-forwarding-rule%{random_suffix}"
79+
provider = google-beta
80+
region = "europe-west1"
81+
depends_on = [google_compute_subnetwork.proxy_subnet]
82+
ip_protocol = "TCP"
83+
load_balancing_scheme = "INTERNAL_MANAGED"
84+
port_range = "80"
85+
target = google_compute_region_target_http_proxy.default.id
86+
network = google_compute_network.ilb_network.id
87+
subnetwork = google_compute_subnetwork.ilb_subnet.id
88+
network_tier = "PREMIUM"
89+
}
90+
91+
# http proxy
92+
resource "google_compute_region_target_http_proxy" "default" {
93+
name = "tf-test-l7-ilb-target-http-proxy%{random_suffix}"
94+
provider = google-beta
95+
region = "europe-west1"
96+
url_map = google_compute_region_url_map.default.id
97+
}
98+
99+
# url map
100+
resource "google_compute_region_url_map" "default" {
101+
name = "tf-test-l7-ilb-regional-url-map%{random_suffix}"
102+
provider = google-beta
103+
region = "europe-west1"
104+
default_service = google_compute_region_backend_service.default.id
105+
}
106+
107+
# backend service
108+
resource "google_compute_region_backend_service" "default" {
109+
name = "tf-test-l7-ilb-backend-subnet%{random_suffix}"
110+
provider = google-beta
111+
region = "europe-west1"
112+
protocol = "HTTP"
113+
load_balancing_scheme = "INTERNAL_MANAGED"
114+
timeout_sec = 10
115+
health_checks = [google_compute_region_health_check.default.id]
116+
backend {
117+
group = google_compute_region_instance_group_manager.mig.instance_group
118+
balancing_mode = "UTILIZATION"
119+
capacity_scaler = 1.0
120+
}
121+
}
122+
123+
# instance template
124+
resource "google_compute_instance_template" "instance_template" {
125+
name = "tf-test-l7-ilb-mig-template%{random_suffix}"
126+
provider = google-beta
127+
machine_type = "e2-small"
128+
tags = ["http-server"]
129+
130+
network_interface {
131+
network = google_compute_network.ilb_network.id
132+
subnetwork = google_compute_subnetwork.ilb_subnet.id
133+
access_config {
134+
# add external ip to fetch packages
135+
}
136+
}
137+
disk {
138+
source_image = "debian-cloud/debian-10"
139+
auto_delete = true
140+
boot = true
141+
}
142+
143+
# install nginx and serve a simple web page
144+
metadata = {
145+
startup-script = <<-EOF1
146+
#! /bin/bash
147+
set -euo pipefail
148+
149+
export DEBIAN_FRONTEND=noninteractive
150+
apt-get update
151+
apt-get install -y nginx-light jq
152+
153+
NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname")
154+
IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip")
155+
METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])')
156+
157+
cat <<EOF > /var/www/html/index.html
158+
<pre>
159+
Name: $NAME
160+
IP: $IP
161+
Metadata: $METADATA
162+
</pre>
163+
EOF
164+
EOF1
165+
}
166+
lifecycle {
167+
create_before_destroy = true
168+
}
169+
}
170+
171+
# health check
172+
resource "google_compute_region_health_check" "default" {
173+
name = "tf-test-l7-ilb-hc%{random_suffix}"
174+
provider = google-beta
175+
region = "europe-west1"
176+
http_health_check {
177+
port_specification = "USE_SERVING_PORT"
178+
}
179+
}
180+
181+
# MIG
182+
resource "google_compute_region_instance_group_manager" "mig" {
183+
name = "tf-test-l7-ilb-mig1%{random_suffix}"
184+
provider = google-beta
185+
region = "europe-west1"
186+
version {
187+
instance_template = google_compute_instance_template.instance_template.id
188+
name = "primary"
189+
}
190+
base_instance_name = "vm"
191+
target_size = 2
192+
}
193+
194+
# allow all access from IAP and health check ranges
195+
resource "google_compute_firewall" "fw-iap" {
196+
name = "tf-test-l7-ilb-fw-allow-iap-hc%{random_suffix}"
197+
provider = google-beta
198+
direction = "INGRESS"
199+
network = google_compute_network.ilb_network.id
200+
source_ranges = ["130.211.0.0/22", "35.191.0.0/16", "35.235.240.0/20"]
201+
allow {
202+
protocol = "tcp"
203+
}
204+
}
205+
206+
# allow http from proxy subnet to backends
207+
resource "google_compute_firewall" "fw-ilb-to-backends" {
208+
name = "tf-test-l7-ilb-fw-allow-ilb-to-backends%{random_suffix}"
209+
provider = google-beta
210+
direction = "INGRESS"
211+
network = google_compute_network.ilb_network.id
212+
source_ranges = ["10.0.0.0/24"]
213+
target_tags = ["http-server"]
214+
allow {
215+
protocol = "tcp"
216+
ports = ["80", "443", "8080"]
217+
}
218+
}
219+
220+
# test instance
221+
resource "google_compute_instance" "vm-test" {
222+
name = "tf-test-l7-ilb-test-vm%{random_suffix}"
223+
provider = google-beta
224+
zone = "europe-west1-b"
225+
machine_type = "e2-small"
226+
network_interface {
227+
network = google_compute_network.ilb_network.id
228+
subnetwork = google_compute_subnetwork.ilb_subnet.id
229+
}
230+
boot_disk {
231+
initialize_params {
232+
image = "debian-cloud/debian-10"
233+
}
234+
}
235+
}
236+
`, context)
237+
}
238+
26239
func TestAccComputeForwardingRule_forwardingRuleExternallbExample(t *testing.T) {
27240
t.Parallel()
28241

0 commit comments

Comments
 (0)