diff --git a/infrastructure/base/main.tf b/infrastructure/base/main.tf index 934db9c1..f2992c70 100644 --- a/infrastructure/base/main.tf +++ b/infrastructure/base/main.tf @@ -8,25 +8,27 @@ terraform { } module "staging" { - source = "./modules/env" - gcp_project_id = var.gcp_project_id - gcp_region = var.gcp_region - github_org = var.github_org - github_project = var.github_project - github_branch = "develop" - project_name = var.staging_project_name - frontend_min_scale = 0 - backend_min_scale = 0 - frontend_max_scale = 1 - backend_max_scale = 2 - dns_zone_name = module.dns.dns_zone_name - domain = var.domain - subdomain = "30x30" - backend_path_prefix = "cms" - uptime_alert_email = var.uptime_alert_email - environment = "staging" - database_name = "strapi" - database_user = "strapi" + source = "./modules/env" + gcp_project_id = var.gcp_project_id + gcp_region = var.gcp_region + github_org = var.github_org + github_project = var.github_project + github_branch = "develop" + project_name = var.staging_project_name + frontend_min_scale = 0 + backend_min_scale = 0 + frontend_max_scale = 1 + backend_max_scale = 2 + dns_zone_name = module.dns.dns_zone_name + domain = var.domain + subdomain = "30x30" + backend_path_prefix = "cms" + functions_path_prefix = "functions" + analysis_function_path_prefix = "analysis" + uptime_alert_email = var.uptime_alert_email + environment = "staging" + database_name = "strapi" + database_user = "strapi" } module "dns" { @@ -36,10 +38,10 @@ module "dns" { } resource "google_service_account" "data_pipelines_service_account" { - project = var.gcp_project_id + project = var.gcp_project_id account_id = "data-pipelines" display_name = "data-pipelines" - description = "Data Pipelines Service Account" + description = "Data Pipelines Service Account" } import { @@ -53,6 +55,6 @@ data "google_storage_bucket" "data_pipelines_bucket" { resource "google_storage_bucket_iam_member" "member" { bucket = data.google_storage_bucket.data_pipelines_bucket.name - role = "roles/storage.admin" + role = "roles/storage.admin" member = "serviceAccount:${google_service_account.data_pipelines_service_account.email}" } diff --git a/infrastructure/base/modules/env/main.tf b/infrastructure/base/modules/env/main.tf index a36952b9..e870eca6 100644 --- a/infrastructure/base/modules/env/main.tf +++ b/infrastructure/base/modules/env/main.tf @@ -133,6 +133,10 @@ resource "random_password" "app_key" { } locals { + frontend_lb_url = "https://${local.domain}" + cms_lb_url = "https://${local.domain}/${var.backend_path_prefix}/" + api_lb_url = "https://${local.domain}/${var.backend_path_prefix}/api/" + analysis_cf_lb_url = "https://${local.domain}/${var.functions_path_prefix}/${var.analysis_function_path_prefix}/" cms_env = { HOST = "0.0.0.0" PORT = 1337 @@ -147,7 +151,7 @@ locals { ADMIN_JWT_SECRET = random_password.admin_jwt_secret.result TRANSFER_TOKEN_SALT = random_password.transfer_token_salt.result JWT_SECRET = random_password.jwt_secret.result - CMS_URL = "https://${local.domain}/${var.backend_path_prefix}/" + CMS_URL = local.cms_lb_url DATABASE_CLIENT = "postgres" DATABASE_HOST = module.database.database_host @@ -157,11 +161,11 @@ locals { DATABASE_SSL = false } client_env = { - NEXT_PUBLIC_URL = "https://${local.domain}" - NEXT_PUBLIC_API_URL = "https://${local.domain}/${var.backend_path_prefix}/api/" - NEXT_PUBLIC_ANALYSIS_CF_URL = module.analysis_cloud_function.function_uri - NEXT_PUBLIC_ENVIRONMENT = "production" - LOG_LEVEL = "info" + NEXT_PUBLIC_URL = local.frontend_lb_url + NEXT_PUBLIC_API_URL = local.api_lb_url + NEXT_PUBLIC_ANALYSIS_CF_URL = local.analysis_cf_lb_url + NEXT_PUBLIC_ENVIRONMENT = "production" + LOG_LEVEL = "info" } analysis_cloud_function_env = { DATABASE_CLIENT = "postgres" @@ -187,7 +191,7 @@ locals { client_repository = "${upper(var.environment)}_CLIENT_REPOSITORY" cms_service = "${upper(var.environment)}_CMS_SERVICE" client_service = "${upper(var.environment)}_CLIENT_SERVICE" - analysis_cf_name = "${upper(var.environment)}_ANALYSIS_CF_NAME" + analysis_cf_name = "${upper(var.environment)}_ANALYSIS_CF_NAME" } module "github_values" { @@ -245,16 +249,19 @@ resource "google_project_service" "iam_service" { } module "load_balancer" { - source = "../load-balancer" - region = var.gcp_region - project = var.gcp_project_id - name = var.project_name - backend_cloud_run_name = module.backend_cloudrun.name - frontend_cloud_run_name = module.frontend_cloudrun.name - domain = var.domain - subdomain = var.subdomain - dns_managed_zone_name = var.dns_zone_name - backend_path_prefix = var.backend_path_prefix + source = "../load-balancer" + region = var.gcp_region + project = var.gcp_project_id + name = var.project_name + backend_cloud_run_name = module.backend_cloudrun.name + frontend_cloud_run_name = module.frontend_cloudrun.name + analysis_function_name = module.analysis_cloud_function.function_name + domain = var.domain + subdomain = var.subdomain + dns_managed_zone_name = var.dns_zone_name + backend_path_prefix = var.backend_path_prefix + functions_path_prefix = var.functions_path_prefix + analysis_function_path_prefix = var.analysis_function_path_prefix } module "analysis_cloud_function" { diff --git a/infrastructure/base/modules/env/outputs.tf b/infrastructure/base/modules/env/outputs.tf index 1574d904..a28e3cbc 100644 --- a/infrastructure/base/modules/env/outputs.tf +++ b/infrastructure/base/modules/env/outputs.tf @@ -1,11 +1,23 @@ output "site_url" { - value = local.domain + value = local.frontend_lb_url +} + +output "cms_url" { + value = local.cms_lb_url } output "api_url" { - value = "${local.domain}/backend/api" + value = local.cms_lb_url } output "analysis_cloud_function_url" { - value = module.analysis_cloud_function.function_uri + value = local.analysis_cf_lb_url } + +output "client_env" { + value = local.client_env +} + +output "cms_env" { + value = local.cms_env +} \ No newline at end of file diff --git a/infrastructure/base/modules/env/variables.tf b/infrastructure/base/modules/env/variables.tf index 9af23a80..298ea338 100644 --- a/infrastructure/base/modules/env/variables.tf +++ b/infrastructure/base/modules/env/variables.tf @@ -106,3 +106,13 @@ variable "backend_path_prefix" { type = string description = "Path prefix for the backend service" } + +variable "functions_path_prefix" { + type = string + description = "Path prefix for the functions services" +} + +variable "analysis_function_path_prefix" { + type = string + description = "Path prefix for the analysis function" +} diff --git a/infrastructure/base/modules/load-balancer/main.tf b/infrastructure/base/modules/load-balancer/main.tf index a43eb9ab..31514711 100644 --- a/infrastructure/base/modules/load-balancer/main.tf +++ b/infrastructure/base/modules/load-balancer/main.tf @@ -85,6 +85,16 @@ resource "google_compute_url_map" "load-balancer-url-map" { } } } + + path_rule { + paths = ["/${var.functions_path_prefix}/${var.analysis_function_path_prefix}/*"] + service = google_compute_backend_service.analysis_service.id + route_action { + url_rewrite { + path_prefix_rewrite = "/" + } + } + } } } @@ -108,6 +118,15 @@ resource "google_compute_region_network_endpoint_group" "cloudrun_frontend_neg" } } +resource "google_compute_region_network_endpoint_group" "function_analysis_neg" { + name = "${var.name}-analysis-neg" + network_endpoint_type = "SERVERLESS" + region = var.region + cloud_function { + function = var.analysis_function_name + } +} + resource "google_compute_backend_service" "backend_service" { name = "${var.name}-backend-service" description = "${var.name} backend service" @@ -115,7 +134,6 @@ resource "google_compute_backend_service" "backend_service" { backend { group = google_compute_region_network_endpoint_group.cloudrun_backend_neg.id } - } resource "google_compute_backend_service" "frontend_service" { @@ -125,7 +143,15 @@ resource "google_compute_backend_service" "frontend_service" { backend { group = google_compute_region_network_endpoint_group.cloudrun_frontend_neg.id } +} + +resource "google_compute_backend_service" "analysis_service" { + name = "${var.name}-analysis-service" + description = "${var.name} analysis service" + backend { + group = google_compute_region_network_endpoint_group.function_analysis_neg.id + } } # DNS record diff --git a/infrastructure/base/modules/load-balancer/variables.tf b/infrastructure/base/modules/load-balancer/variables.tf index 7ef1c02e..0b7fdf7d 100644 --- a/infrastructure/base/modules/load-balancer/variables.tf +++ b/infrastructure/base/modules/load-balancer/variables.tf @@ -43,3 +43,19 @@ variable "backend_path_prefix" { type = string description = "Path prefix for the backend service" } + +variable "analysis_function_name" { + type = string + description = "Name of the analysis Cloud Function" +} + +variable "functions_path_prefix" { + type = string + description = "Path prefix for the functions services" +} + +variable "analysis_function_path_prefix" { + type = string + description = "Path prefix for the analysis function" +} + diff --git a/infrastructure/base/outputs.tf b/infrastructure/base/outputs.tf index 1b8eb21f..590d9586 100644 --- a/infrastructure/base/outputs.tf +++ b/infrastructure/base/outputs.tf @@ -14,6 +14,16 @@ output "dns_name_servers" { value = module.dns.dns_name_servers } +output "staging_client_env" { + value = module.staging.client_env + sensitive = true +} + +output "staging_cms_env" { + value = module.staging.cms_env + sensitive = true +} + # output "production_site_url" { # value = module.production.site_url # }