Unable to create http to https redirect using terraform for GCP Classic Load balancer

134 Views Asked by At

I am trying to create a HTTP load balancer which can redirect request to http to https. Please refer to this image I am trying to enable this but am not able to figure how to enable this in terraform. HTTP to HTTPS redirect option

Code which I am using:

provider "google" {
  credentials = file(var.sa_account)
  project     = var.project_id
  region      = "us-central1" # Replace with your desired region
}
#This block creates a Static Public IP
resource "google_compute_global_address" "lb_ip" {
  name                  = "lb-ip"
  project               = var.project_id
  purpose               = var.purpose_ip
  address_type          = var.address_type
}
#This block creates an ssl certificate
resource "google_compute_ssl_certificate" "lb_ssl_certificate" {
  name        = var.ssl_certificate_name
  description = "SSL certificate for load balancer"
  project     = var.project_id
  private_key = file(var.private_key_path)
  certificate = file(var.ca_cert_path)
}
#
resource "google_compute_http_health_check" "lb_http_health_check" {
  name               = "lb-http-health-check"
  project            = var.project_id
  request_path       = "/incomes"
  port               = 80
  check_interval_sec = 60
  timeout_sec        = 60
  healthy_threshold  = 2
  unhealthy_threshold = 10
}
#This created a backend service

resource "google_compute_backend_service" "lb_backend_service" {
  name                    = "lb-backend-service"
  project                 = var.project_id
  protocol                = "HTTP"
  timeout_sec             = 300
  enable_cdn              = true
  port_name               = "http"
  backend {
    group = "https://www.googleapis.com/compute/v1/projects/corded-dragon-404510/zones/us-central1-c/instanceGroups/instance-group-1" # Replace with the backend instance group
  }
  health_checks = [
    google_compute_http_health_check.lb_http_health_check.self_link
  ]
}

#created a URL Map
resource "google_compute_url_map" "lb_url_map" {
  name            = "https-load-balancer"
  project         = var.project_id
  default_service = google_compute_backend_service.lb_backend_service.self_link
#   path_matcher {
#     name = "allpaths"
#     route_rules {
#       priority = 1
#       url_redirect {
#         https_redirect = true
#       }
#     }
#   }
}

resource "google_compute_target_https_proxy" "lb_target_proxy" {
  name             = "lb-target-proxy"
  project          = var.project_id
  url_map          = google_compute_url_map.lb_url_map.self_link
  ssl_certificates = [google_compute_ssl_certificate.lb_ssl_certificate.id]
}

resource "google_compute_global_forwarding_rule" "lb_forwarding_rule" {
  name       = "lb-forwarding-rule"
  project    = var.project_id
  target     = google_compute_target_https_proxy.lb_target_proxy.self_link
  port_range = 443
  ip_address = google_compute_global_address.lb_ip.address
}


resource "google_compute_url_map" "http-redirect" {
  name = "http-redirect"

  default_url_redirect {
    redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"  // 301 redirect
    strip_query            = false
    https_redirect         = true  // this is the magic
  }
}
#This creates a HTTP rule but does not redirect traffic from port 80 to 443
resource "google_compute_target_http_proxy" "http-redirect" {
  name    = "http-redirect"
  url_map = google_compute_url_map.http-redirect.self_link
}
resource "google_compute_target_http_proxy" "lb_target_proxy" {
  name             = "lb-target-proxy"
  project          = var.project_id
  url_map          = google_compute_url_map.lb_url_map.self_link
}
resource "google_compute_global_forwarding_rule" "http_to_static_pages" {
  name       = "http-products-forward-rule"
  target     = google_compute_target_http_proxy.lb_target_proxy.self_link
  ip_address = google_compute_global_address.lb_ip.address
  port_range = "80"
}

I have referred the below but adding that did not help me Stackoverflow reference

This helped me to create a HTTP and HTTPS but what I would like is a redirect. But this is not the thing I am trying to achieve.

Result which I get after running tf script

I want it to create a redirect. This is what I expect the script to do. what I am trying to achieve

Can someone help me to understand what I am missing ?

0

There are 0 best solutions below