Skip to content

Commit

Permalink
Merge pull request #114 from Vizzuality/infrastructure/cloud_function…
Browse files Browse the repository at this point in the history
…_through_lb

Cloud function traffic through load balancer
  • Loading branch information
Agnieszka Figiel authored Dec 11, 2023
2 parents 1940964 + be68531 commit f9b7f4a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 43 deletions.
46 changes: 24 additions & 22 deletions infrastructure/base/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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 {
Expand All @@ -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}"
}
41 changes: 24 additions & 17 deletions infrastructure/base/modules/env/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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" {
Expand Down Expand Up @@ -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" {
Expand Down
18 changes: 15 additions & 3 deletions infrastructure/base/modules/env/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions infrastructure/base/modules/env/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
28 changes: 27 additions & 1 deletion infrastructure/base/modules/load-balancer/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/"
}
}
}
}
}

Expand All @@ -108,14 +118,22 @@ 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"

backend {
group = google_compute_region_network_endpoint_group.cloudrun_backend_neg.id
}

}

resource "google_compute_backend_service" "frontend_service" {
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions infrastructure/base/modules/load-balancer/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

10 changes: 10 additions & 0 deletions infrastructure/base/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
# }
Expand Down

0 comments on commit f9b7f4a

Please sign in to comment.