|
| 1 | +# Setup custom domains and external load balancer for a given bucket |
| 2 | + |
| 3 | +# Enable the services needed for this module |
| 4 | +resource "google_project_service" "services" { |
| 5 | + for_each = local.services |
| 6 | + project = data.google_client_config.current.project |
| 7 | + service = each.value |
| 8 | + disable_on_destroy = false |
| 9 | +} |
| 10 | + |
| 11 | +# Reserve an external IP |
| 12 | +resource "google_compute_global_address" "static_ip" { |
| 13 | + name = "${var.namespace}-ip" |
| 14 | + labels = local.labels |
| 15 | + |
| 16 | + depends_on = [google_project_service.services] |
| 17 | +} |
| 18 | + |
| 19 | +# Create a backend service for the given bucket |
| 20 | +resource "google_compute_backend_bucket" "backend_bucket" { |
| 21 | + name = "${var.namespace}-backend" |
| 22 | + description = "Contains files needed by ${var.bucket_name}" |
| 23 | + bucket_name = var.bucket_name |
| 24 | + enable_cdn = var.enable_cdn |
| 25 | +} |
| 26 | + |
| 27 | +# Create HTTPS certificate |
| 28 | +resource "google_compute_managed_ssl_certificate" "ssl_cert" { |
| 29 | + name = "${var.namespace}-cert" |
| 30 | + managed { |
| 31 | + domains = var.domains |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +# GCP URL MAP |
| 36 | +resource "google_compute_url_map" "url_map" { |
| 37 | + name = "${var.namespace}-lb" |
| 38 | + default_service = google_compute_backend_bucket.backend_bucket.self_link |
| 39 | +} |
| 40 | + |
| 41 | +# GCP target proxy |
| 42 | +resource "google_compute_target_https_proxy" "target_proxy" { |
| 43 | + name = "${var.namespace}-target-proxy" |
| 44 | + url_map = google_compute_url_map.url_map.self_link |
| 45 | + ssl_certificates = [google_compute_managed_ssl_certificate.ssl_cert.self_link] |
| 46 | +} |
| 47 | + |
| 48 | +# GCP forwarding rule |
| 49 | +resource "google_compute_global_forwarding_rule" "forwarding_rule" { |
| 50 | + name = "${var.namespace}-fwd-rule" |
| 51 | + load_balancing_scheme = "EXTERNAL" |
| 52 | + ip_address = google_compute_global_address.static_ip.address |
| 53 | + ip_protocol = "TCP" |
| 54 | + port_range = "443" |
| 55 | + target = google_compute_target_https_proxy.target_proxy.self_link |
| 56 | + labels = local.labels |
| 57 | +} |
0 commit comments