Skip to content

Commit 68a5e94

Browse files
Adding end-to-end example for L4 ILB (#5120) (#3555)
Signed-off-by: Modular Magician <[email protected]>
1 parent d5596b4 commit 68a5e94

File tree

3 files changed

+390
-0
lines changed

3 files changed

+390
-0
lines changed

.changelog/5120.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: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,207 @@ resource "google_compute_instance" "vm-test" {
236236
`, context)
237237
}
238238

239+
func TestAccComputeForwardingRule_internalTcpUdpLbWithMigBackendExample(t *testing.T) {
240+
t.Parallel()
241+
242+
context := map[string]interface{}{
243+
"random_suffix": randString(t, 10),
244+
}
245+
246+
vcrTest(t, resource.TestCase{
247+
PreCheck: func() { testAccPreCheck(t) },
248+
Providers: testAccProvidersOiCS,
249+
CheckDestroy: testAccCheckComputeForwardingRuleDestroyProducer(t),
250+
Steps: []resource.TestStep{
251+
{
252+
Config: testAccComputeForwardingRule_internalTcpUdpLbWithMigBackendExample(context),
253+
},
254+
},
255+
})
256+
}
257+
258+
func testAccComputeForwardingRule_internalTcpUdpLbWithMigBackendExample(context map[string]interface{}) string {
259+
return Nprintf(`
260+
# Internal TCP/UDP load balancer with a managed instance group backend
261+
262+
# VPC
263+
resource "google_compute_network" "ilb_network" {
264+
name = "tf-test-l4-ilb-network%{random_suffix}"
265+
provider = google-beta
266+
auto_create_subnetworks = false
267+
}
268+
269+
# backed subnet
270+
resource "google_compute_subnetwork" "ilb_subnet" {
271+
name = "tf-test-l4-ilb-subnet%{random_suffix}"
272+
provider = google-beta
273+
ip_cidr_range = "10.0.1.0/24"
274+
region = "europe-west1"
275+
network = google_compute_network.ilb_network.id
276+
}
277+
278+
# forwarding rule
279+
resource "google_compute_forwarding_rule" "google_compute_forwarding_rule" {
280+
name = "tf-test-l4-ilb-forwarding-rule%{random_suffix}"
281+
backend_service = google_compute_region_backend_service.default.id
282+
provider = google-beta
283+
region = "europe-west1"
284+
ip_protocol = "TCP"
285+
load_balancing_scheme = "INTERNAL"
286+
all_ports = true
287+
allow_global_access = true
288+
network = google_compute_network.ilb_network.id
289+
subnetwork = google_compute_subnetwork.ilb_subnet.id
290+
}
291+
292+
# backend service
293+
resource "google_compute_region_backend_service" "default" {
294+
name = "tf-test-l4-ilb-backend-subnet%{random_suffix}"
295+
provider = google-beta
296+
region = "europe-west1"
297+
protocol = "TCP"
298+
load_balancing_scheme = "INTERNAL"
299+
health_checks = [google_compute_region_health_check.default.id]
300+
backend {
301+
group = google_compute_region_instance_group_manager.mig.instance_group
302+
balancing_mode = "CONNECTION"
303+
}
304+
}
305+
306+
# instance template
307+
resource "google_compute_instance_template" "instance_template" {
308+
name = "tf-test-l4-ilb-mig-template%{random_suffix}"
309+
provider = google-beta
310+
machine_type = "e2-small"
311+
tags = ["allow-ssh","allow-health-check"]
312+
313+
network_interface {
314+
network = google_compute_network.ilb_network.id
315+
subnetwork = google_compute_subnetwork.ilb_subnet.id
316+
access_config {
317+
# add external ip to fetch packages
318+
}
319+
}
320+
disk {
321+
source_image = "debian-cloud/debian-10"
322+
auto_delete = true
323+
boot = true
324+
}
325+
326+
# install nginx and serve a simple web page
327+
metadata = {
328+
startup-script = <<-EOF1
329+
#! /bin/bash
330+
set -euo pipefail
331+
332+
export DEBIAN_FRONTEND=noninteractive
333+
apt-get update
334+
apt-get install -y nginx-light jq
335+
336+
NAME=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/hostname")
337+
IP=$(curl -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip")
338+
METADATA=$(curl -f -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/attributes/?recursive=True" | jq 'del(.["startup-script"])')
339+
340+
cat <<EOF > /var/www/html/index.html
341+
<pre>
342+
Name: $NAME
343+
IP: $IP
344+
Metadata: $METADATA
345+
</pre>
346+
EOF
347+
EOF1
348+
}
349+
lifecycle {
350+
create_before_destroy = true
351+
}
352+
}
353+
354+
# health check
355+
resource "google_compute_region_health_check" "default" {
356+
name = "tf-test-l4-ilb-hc%{random_suffix}"
357+
provider = google-beta
358+
region = "europe-west1"
359+
http_health_check {
360+
port = "80"
361+
}
362+
}
363+
364+
# MIG
365+
resource "google_compute_region_instance_group_manager" "mig" {
366+
name = "tf-test-l4-ilb-mig1%{random_suffix}"
367+
provider = google-beta
368+
region = "europe-west1"
369+
version {
370+
instance_template = google_compute_instance_template.instance_template.id
371+
name = "primary"
372+
}
373+
base_instance_name = "vm"
374+
target_size = 2
375+
}
376+
377+
# allow all access from health check ranges
378+
resource "google_compute_firewall" "fw_hc" {
379+
name = "tf-test-l4-ilb-fw-allow-hc%{random_suffix}"
380+
provider = google-beta
381+
direction = "INGRESS"
382+
network = google_compute_network.ilb_network.id
383+
source_ranges = ["130.211.0.0/22", "35.191.0.0/16", "35.235.240.0/20"]
384+
allow {
385+
protocol = "tcp"
386+
}
387+
source_tags = ["allow-health-check"]
388+
}
389+
390+
# allow communication within the subnet
391+
resource "google_compute_firewall" "fw_ilb_to_backends" {
392+
name = "tf-test-l4-ilb-fw-allow-ilb-to-backends%{random_suffix}"
393+
provider = google-beta
394+
direction = "INGRESS"
395+
network = google_compute_network.ilb_network.id
396+
source_ranges = ["10.0.1.0/24"]
397+
allow {
398+
protocol = "tcp"
399+
}
400+
allow {
401+
protocol = "udp"
402+
}
403+
allow {
404+
protocol = "icmp"
405+
}
406+
}
407+
408+
# allow SSH
409+
resource "google_compute_firewall" "fw_ilb_ssh" {
410+
name = "tf-test-l4-ilb-fw-ssh%{random_suffix}"
411+
provider = google-beta
412+
direction = "INGRESS"
413+
network = google_compute_network.ilb_network.id
414+
allow {
415+
protocol = "tcp"
416+
ports = ["22"]
417+
}
418+
source_tags = ["allow-ssh"]
419+
}
420+
421+
# test instance
422+
resource "google_compute_instance" "vm_test" {
423+
name = "tf-test-l4-ilb-test-vm%{random_suffix}"
424+
provider = google-beta
425+
zone = "europe-west1-b"
426+
machine_type = "e2-small"
427+
network_interface {
428+
network = google_compute_network.ilb_network.id
429+
subnetwork = google_compute_subnetwork.ilb_subnet.id
430+
}
431+
boot_disk {
432+
initialize_params {
433+
image = "debian-cloud/debian-10"
434+
}
435+
}
436+
}
437+
`, context)
438+
}
439+
239440
func TestAccComputeForwardingRule_forwardingRuleExternallbExample(t *testing.T) {
240441
t.Parallel()
241442

0 commit comments

Comments
 (0)