-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add Terraform support for Cross-regional failover w/ RXLB Example #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
611f88f
b3eb8d5
7575f8f
ed17531
ad6c5d0
c228482
381e1bb
ce68f37
5af0114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ resource "google_compute_region_security_policy" "armor" { | |
| for_each = var.regions | ||
| name = "regional-${each.key}-armor" | ||
| region = each.key | ||
| type = "CLOUD_ARMOR" | ||
|
|
||
| rules { | ||
| priority = 1000 | ||
|
|
@@ -58,6 +59,10 @@ resource "google_compute_instance_template" "tmpl" { | |
| name_prefix = "regional-${each.key}-tmpl" | ||
| machine_type = var.instance_machine_type | ||
|
|
||
| lifecycle { | ||
| create_before_destroy = true | ||
| } | ||
|
|
||
| disk { | ||
| source_image = var.instance_image | ||
| auto_delete = true | ||
|
|
@@ -66,8 +71,9 @@ resource "google_compute_instance_template" "tmpl" { | |
|
|
||
| network_interface { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that without The VMs won't get an external IP address. This could work, but not without some more configuration. For one, I think this causes the startup script to fail because apt-get doesn't work with cloud nat. Also, in order to allow SSH into that machines, you'd need to configure IAP tunneling. Not sure what we usually do with these templates, but I think you either need to make sure that this works correctly without an external IP, or just enable an external IP for the VMs. |
||
| subnetwork = google_compute_subnetwork.vm[each.key].self_link | ||
| access_config {} | ||
| } | ||
|
|
||
| metadata_startup_script = <<-EOT | ||
| #!/bin/bash | ||
| apt-get update -y | ||
|
|
@@ -147,14 +153,20 @@ resource "google_compute_region_target_http_proxy" "http" { | |
| } | ||
|
|
||
| resource "google_compute_region_target_https_proxy" "https" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting an error during resource creation here, which I don't fully understand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still getting this same error when testing. |
||
| provider = "google-beta" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got a deprecation warning here so I think you can just make |
||
| for_each = var.regions | ||
| name = "regional-${each.key}-https-proxy" | ||
| region = each.key | ||
| url_map = google_compute_region_url_map.um[each.key].self_link | ||
|
|
||
| certificate_manager_certificates = [ | ||
| google_certificate_manager_certificate.cert[each.key].id | ||
| google_certificate_manager_certificate.cert[each.key].id | ||
| ] | ||
|
|
||
| depends_on = [ | ||
| google_certificate_manager_certificate.cert | ||
| ] | ||
|
|
||
| } | ||
|
|
||
| resource "google_compute_address" "addr" { | ||
|
|
@@ -192,14 +204,20 @@ resource "google_compute_forwarding_rule" "https" { | |
| } | ||
|
|
||
| resource "google_dns_record_set" "regional_geo_a" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This resource should also have --enable-health-check set, correct? That is, the health checking feature should be enabled so that failover from to the other can happen, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you still need |
||
| count = var.enable_dns_records && length(local.dns_managed_zone_name) > 0 ? 1 : 0 | ||
| count = var.enable_dns_records && length(local.ordered_regions) > 0 ? 1 : 0 | ||
|
|
||
| lifecycle { | ||
| replace_triggered_by = [google_compute_address.addr] | ||
| ignore_changes = [rrdatas] | ||
| } | ||
|
|
||
| managed_zone = local.dns_managed_zone_name | ||
| name = "${var.regional_hostname}." | ||
| type = "A" | ||
| ttl = 60 | ||
|
|
||
| routing_policy { | ||
| enable_geo_fencing = false | ||
| dynamic "geo" { | ||
| for_each = var.regions | ||
| content { | ||
|
|
@@ -209,6 +227,7 @@ resource "google_dns_record_set" "regional_geo_a" { | |
| } | ||
| } | ||
| } | ||
|
|
||
| resource "google_certificate_manager_dns_authorization" "auth" { | ||
| for_each = var.regions | ||
| provider = google-beta | ||
|
|
@@ -218,27 +237,36 @@ resource "google_certificate_manager_dns_authorization" "auth" { | |
| } | ||
|
|
||
| locals { | ||
| dns_managed_zone_name = var.create_public_zone && var.enable_dns_records ? google_dns_managed_zone.public_new[0].name : var.public_zone_name | ||
| ordered_regions = sort(keys(var.regions)) | ||
| dns_managed_zone_name = coalesce( | ||
| try(google_dns_managed_zone.public_new[0].name, null), | ||
| var.public_zone_name | ||
| ) | ||
| } | ||
|
|
||
| resource "google_dns_record_set" "acme_txt" { | ||
| for_each = (var.enable_dns_records && length(local.dns_managed_zone_name) > 0) ? google_certificate_manager_dns_authorization.auth : {} | ||
| for_each = (var.enable_dns_records && length(local.ordered_regions) > 0) ? google_certificate_manager_dns_authorization.auth : {} | ||
| managed_zone = local.dns_managed_zone_name | ||
| name = each.value.dns_resource_record[0].name | ||
| type = "TXT" | ||
| ttl = 60 | ||
| rrdatas = [each.value.dns_resource_record[0].data] | ||
| } | ||
|
|
||
|
|
||
| resource "google_certificate_manager_certificate" "cert" { | ||
| for_each = var.regions | ||
| provider = google-beta | ||
| name = "regional-${each.key}-cm-cert" | ||
| location = each.key | ||
| location = each.key | ||
|
|
||
| managed { | ||
| domains = [var.regional_hostname] | ||
| dns_authorizations = [google_certificate_manager_dns_authorization.auth[each.key].id] | ||
| } | ||
| depends_on = [ | ||
| google_dns_record_set.acme_txt | ||
| ] | ||
| } | ||
|
|
||
| resource "google_dns_managed_zone" "public_new" { | ||
|
|
@@ -248,19 +276,6 @@ resource "google_dns_managed_zone" "public_new" { | |
| description = "Public zone for ${var.regional_hostname}" | ||
| } | ||
|
|
||
| resource "google_dns_record_set" "regional_a" { | ||
| count = var.enable_dns_records && length(var.regions) > 0 ? 1 : 0 | ||
|
|
||
| managed_zone = local.dns_managed_zone_name | ||
| name = "${var.regional_hostname}." | ||
| type = "A" | ||
| ttl = 60 | ||
|
|
||
| rrdatas = [ | ||
| for r in keys(var.regions) : google_compute_address.addr[r].address | ||
| ] | ||
| } | ||
|
|
||
| resource "google_compute_firewall" "allow_hc" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you also need a firewall rule covering traffic from the proxy to the backend, no? For instance, the google_compute_firewall on https://docs.cloud.google.com/load-balancing/docs/l7-internal/int-https-lb-tf-examples |
||
| name = "allow-hc" | ||
| network = google_compute_network.auto.name | ||
|
|
@@ -272,3 +287,18 @@ resource "google_compute_firewall" "allow_hc" { | |
| source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] | ||
| target_tags = ["allow-hc"] | ||
| } | ||
|
|
||
| resource "google_compute_firewall" "allow_proxy_to_backend" { | ||
| name = "allow-proxy-to-backend" | ||
| network = google_compute_network.auto.name | ||
|
|
||
| direction = "INGRESS" | ||
| priority = 1000 | ||
| source_ranges = ["35.191.0.0/16", "130.211.0.0/22"] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The source range here should be the source range of the proxy subnet (the |
||
| target_tags = ["allow-proxy"] | ||
|
|
||
| allow { | ||
| protocol = "tcp" | ||
| ports = ["80", "443"] | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
type = "CLOUD_ARMOR"here as well. In my testing, not having this caused the resource to be destroyed and recreated even with no changes, because it thought the type was null.