From 4d388f0bc43dd7c3083a0aa17e73642cf53efe7c Mon Sep 17 00:00:00 2001 From: Julio Diez Date: Tue, 31 Oct 2023 15:21:27 +0100 Subject: [PATCH] Add prefix to project names --- .../cloud-run-microservices/README.md | 31 +++++++++++++------ .../cloud-run-microservices/main.tf | 2 ++ .../cloud-run-microservices/variables.tf | 21 +++++++++---- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/blueprints/serverless/cloud-run-microservices/README.md b/blueprints/serverless/cloud-run-microservices/README.md index 0c5424ba4..aa7a3774b 100644 --- a/blueprints/serverless/cloud-run-microservices/README.md +++ b/blueprints/serverless/cloud-run-microservices/README.md @@ -18,9 +18,10 @@ Depending on the use case, you will need one or two projects with [billing enabl ```tfvars # Create the main project -prj_main_create = { +main_project = { billing_account_id = "ABCDE-12345-ABCDE" parent = "organizations/0123456789" + project_id = "spiritual-hour-331417" } ``` @@ -47,13 +48,17 @@ You should see this README and some terraform files. 3. To deploy a specific use case, you will need to create a file in this directory called `terraform.tfvars` and follow the corresponding instructions to set variables. Values that are meant to be substituted will be shown inside brackets but you need to omit these brackets. E.g.: ```tfvars -project_id = "[your-project_id]" +main_project = { + project_id = "[project_id]" +} ``` may become ```tfvars -project_id = "spiritual-hour-331417" +main_project = { + project_id = "spiritual-hour-331417" +} ``` Use cases are self-contained so you can deploy any of them at will. @@ -83,15 +88,20 @@ https://github.com/willypalacin/vpc-network-tester/tree/main Build an image and push it to Artifact Registry. Set the corresponding image variable to its URL, and the main project ID, in `terraform.tfvars`. E.g.: ```tfvars -prj_main_id = "[your-main-project-id]" -svc_a_image = "us-docker.pkg.dev/[project-id]/[repo-name]/[tester-app]" +main_project = { + project_id = "[main-project-id]" +} +prefix = "[prefix]" +svc_a_image = "us-docker.pkg.dev/[project-id]/[repo-name]/[tester-app]" ``` +Note that final project ids will be of the form [prefix]-[main-project-id]. + The service B default URL is automatically created and shown as a terraform output variable. It will be similar to the one shown in the picture above. Get into service A and try to reach service B URL as shown below:

-Note that service A is resolving service B to an internal IP, 10.0.0.100, the PSC endpoint. Public access is restricted, if you try to e.g. `curl` from your laptop you will get an error. +You can see service A is resolving service B to an internal IP, 10.0.0.100, the PSC endpoint. Public access is restricted, if you try to e.g. `curl` from your laptop you will get an error. ### Use case 2: Service to service communication in different projects @@ -102,9 +112,12 @@ The second option for internal service to service communication is to use an int Set the following in `terraform.tfvars`: ```tfvars -prj_main_id = "[your-main-project-id]" # Used as host project -prj_svc1_id = "[your-service-project1-id]" -svc_a_image = "us-docker.pkg.dev/[project-id]/[repo-name]/[tester-app]" +main_project = { + project_id = "[main-project-id]" # Used as host project +} +prefix = "[prefix]" +service_project = "[service-project-id]" +svc_a_image = "us-docker.pkg.dev/[project-id]/[repo-name]/[tester-app]" ``` The blueprint uses an HTTP connection to the ALB to avoid management of SSL certificates. Try to reach service B custom URL as shown below: diff --git a/blueprints/serverless/cloud-run-microservices/main.tf b/blueprints/serverless/cloud-run-microservices/main.tf index 5460d0d42..d178a7726 100644 --- a/blueprints/serverless/cloud-run-microservices/main.tf +++ b/blueprints/serverless/cloud-run-microservices/main.tf @@ -26,6 +26,7 @@ locals { module "main-project" { source = "../../../modules/project" name = var.main_project.project_id + prefix = var.prefix project_create = var.main_project.billing_account_id != null billing_account = try(var.main_project.billing_account_id, null) parent = try(var.main_project.parent, null) @@ -47,6 +48,7 @@ module "service-project" { source = "../../../modules/project" count = var.service_project.project_id != null ? 1 : 0 name = var.service_project.project_id + prefix = var.prefix project_create = var.service_project.billing_account_id != null billing_account = try(var.service_project.billing_account_id, null) parent = try(var.service_project.parent, null) diff --git a/blueprints/serverless/cloud-run-microservices/variables.tf b/blueprints/serverless/cloud-run-microservices/variables.tf index 47e829074..9ae9cd773 100644 --- a/blueprints/serverless/cloud-run-microservices/variables.tf +++ b/blueprints/serverless/cloud-run-microservices/variables.tf @@ -43,6 +43,21 @@ variable "main_project" { }) } +variable "prefix" { + description = "Prefix used for project names." + type = string + validation { + condition = var.prefix != "" + error_message = "Prefix cannot be empty." + } +} + +variable "region" { + description = "Cloud region where resources will be deployed." + type = string + default = "europe-west1" +} + variable "service_project" { description = "Service project." type = object({ @@ -53,12 +68,6 @@ variable "service_project" { default = {} } -variable "region" { - description = "Cloud region where resources will be deployed." - type = string - default = "europe-west1" -} - variable "svc_a_image" { description = "Container image to deploy in service A." type = string