Skip to content

Commit 6ff308f

Browse files
committed
Add backend custom domain extra tasks
1 parent ee99c96 commit 6ff308f

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,19 @@ To enable HTTPS for the CDN we need to create a certificate. Managed SSL certifi
426426
```
427427
428428
5. Run `terraform apply` and to start the _provisioning_ of the certificate. Provisioning a Google-managed certificate might take up to 60 minutes from the moment your DNS and load balancer configuration changes have propagated across the internet. If you have updated your DNS configuration recently, it can take a significant amount of time for the changes to fully propagate. Sometimes propagation takes up to 72 hours worldwide, although it typically takes a few hours. But if you go to the "Load balancing" and in to your load balancing in the GCP console you should see that HTTPS in the list of the frontend tab.
429+
430+
### Backend custom domain name
431+
432+
To get a custom domain for the GCR instance, the [recommended way](https://cloud.google.com/run/docs/mapping-custom-domains) is to use a global external Application Load Balancer. We could create a new load balancer, but for the purposes of this workshop we're going to share a load balancer for the frontend and backend and use rules to direct the traffic.
433+
434+
1. You'll need to create a [`google_compute_region_network_endpoint_group`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network_endpoint_group) that contains the Google Cloud Run service previously created. Then, create a [`google_compute_backend_service`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_backend_service) using the network endpoint group (NEG). The `google_compute_backend_service` plays a similar role to the `google_compute_backend_bucket` we created for the frontend.
435+
436+
2. Create a new public IP address and DNS record for the backend, similar to what we did for the frontend. The backend should use `api.<yourid42>.cloudlabs-gcp.no`. You can verify using `dig` or similar tools.
437+
438+
3. Set up a `google_compute_target_http_proxy` and `google_compute_global_forwarding_rule` for the backend, similar to what was done for the frontend. After applying these changes, the load balancer won't direct traffic properly until the next step is completed.
439+
440+
4. Modify the previously created load balancer to create `host_rule`s and `path_matcher`. You should route based on host name (domain name) only, the `path_matcher`s are required, but should match all paths. Refer to [`google_compute_url_map` documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_url_map). Please note: These changes can take a couple of minutes to propagate, meaning it can be hard to test. Using the [`test` block](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_url_map#nested_test) on the URL map will let you verify that paths are properly redirected when you run apply.
441+
442+
### Backend HTTPS for custom domain name
443+
444+
Create a `google_compute_managed_ssl_certificate`, `google_compute_target_https_proxy` and `google_compute_global_forwarding_rule` similar to what was done for the frontend.

solutions/backend.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,36 @@ output "backend_url" {
4141
value = google_cloud_run_v2_service.backend.uri
4242
}
4343

44+
resource "google_compute_region_network_endpoint_group" "backend" {
45+
name = "gcr-backend-${local.id}-neg"
46+
region = "europe-west1"
47+
cloud_run {
48+
service = google_cloud_run_v2_service.backend.name
49+
}
50+
}
51+
52+
resource "google_compute_backend_service" "backend" {
53+
name = "gcr-backend-${local.id}"
54+
description = "Backend service for an extnernal appliation load balancer"
55+
56+
backend {
57+
group = google_compute_region_network_endpoint_group.backend.id
58+
}
59+
}
60+
61+
resource "google_compute_target_http_proxy" "backend" {
62+
name = "http-proxy-${local.id}-backend"
63+
url_map = google_compute_url_map.lb.id
64+
}
65+
66+
resource "google_compute_global_address" "backend_public_address" {
67+
name = "backend-public-address-${local.id}"
68+
}
69+
70+
resource "google_compute_global_forwarding_rule" "backend" {
71+
name = "backend-forwarding-rule-${local.id}"
72+
target = google_compute_target_http_proxy.backend.id
73+
port_range = "80"
74+
load_balancing_scheme = "EXTERNAL"
75+
ip_address = google_compute_global_address.backend_public_address.address
76+
}

solutions/cdn.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ output "cdn_public_ip" {
1717
resource "google_compute_url_map" "lb" {
1818
name = "cdn-url-map-${local.id}"
1919
default_service = google_compute_backend_bucket.cdn_bucket.self_link
20+
21+
# Modifications to LB are from extra tasks
22+
host_rule {
23+
hosts = [ local.frontend_subdomain ]
24+
path_matcher = "allpaths-frontend"
25+
}
26+
27+
path_matcher {
28+
name = "allpaths-frontend"
29+
default_service = google_compute_backend_bucket.cdn_bucket.self_link
30+
}
31+
32+
host_rule {
33+
hosts = [ local.backend_subdomain ]
34+
path_matcher = "allpaths-backend"
35+
}
36+
37+
path_matcher {
38+
name = "allpaths-backend"
39+
default_service = google_compute_backend_service.backend.self_link
40+
}
41+
42+
test {
43+
host = local.backend_subdomain
44+
service = google_compute_backend_service.backend.self_link
45+
path = "/todos"
46+
}
2047
}
2148

2249
resource "google_compute_target_http_proxy" "frontend" {

solutions/dns.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,17 @@ resource "google_dns_record_set" "frontend" {
1414
managed_zone = data.google_dns_managed_zone.cloudlabs_gcp_no.name
1515
rrdatas = [google_compute_global_address.cdn_public_address.address]
1616
}
17+
18+
resource "google_dns_record_set" "backend" {
19+
name = "api.${local.id}.${data.google_dns_managed_zone.cloudlabs_gcp_no.dns_name}"
20+
type = "A"
21+
ttl = 60
22+
managed_zone = data.google_dns_managed_zone.cloudlabs_gcp_no.name
23+
rrdatas = [google_compute_global_address.backend_public_address.address]
24+
}
25+
26+
locals {
27+
frontend_subdomain = trimsuffix(google_dns_record_set.frontend.name, ".")
28+
backend_subdomain = trimsuffix(google_dns_record_set.backend.name, ".")
29+
30+
}

0 commit comments

Comments
 (0)