Deploy Private Worker Pool in Cloud Build with Terraform
Deploy Private Worker Pool in Cloud Build with Terraform Objects to be created: Global IP Address: Reserve a global IP address for the Cloud Build worker range, used in network configuration. Network Service Connection: Establishes a networ
Source comparison and review notes
- Automated pair-review warning (AI bilingual content review): The English pre-existing-VPC module example begins with 'odule' rather than 'module', making that copied Terraform snippet invalid.
- Automated pair-review warning (AI bilingual content review): English omits the Spanish Terraform service-account prerequisite link, concrete tfvars example, reserved_peering_ranges explanation, and the precise warning that the provider takes total ownership of Private connections to services and destroys that object with the Terraform resources.

Objects to be created:
- Global IP Address: Reserve a global IP address for the Cloud Build worker range, used in network configuration.
- Network Service Connection: Establishes a network connection between the project’s VPC network and Google services, facilitating secure communication.
- Configuring Peering Routes: Defines how custom routes are imported and exported between the VPC and Google services, essential for network traffic management.
- Cloud Build Worker Pool: Create a pool of private workers to execute construction tasks in a controlled, secure environment.
Other:
- GCP APIs Enabled: Activation of project-critical services such as Compute Engine, Service Usage, Cloud Resource Manager, Service Networking, Cloud Build, and IAM.
- Cloud Build Service Identity: Generates a service identity specific to Cloud Build, ensuring proper authentication and authorization for the service.
- IAM permission: Assigns IAM roles to the Cloud Build service account to use the pool of private workers, enabling secure execution of the builds.
Terraform Registry Module
https://registry.terraform.io/modules/mdiloreto/cloud_build-private-worker_pool/google/1.1.3
How to call the code
module "cloud-build-private-worker-pool" { source = "mdiloreto/cloud_build-private-worker_pool/google" project_id = var.project_id cb_region = var.region network = var.network network_id = var.network_id workerpool_name = var.workerpool_name alloc_range_address = var.alloc_range_address allocation_prefix_length = var.allocation_prefix_length cb_prefix_length = var.cb_prefix_length reserved_peering_ranges = var.reserved_peering_ranges workerpool_range_name = var.workerpool_range_name peering_export_custom_routes = var.peering_export_custom_routes peering_import_custom_routes = var.peering_import_custom_routes}Prerequisites
Create Service Account for Terraform
First, we need to make sure that we have created a Service Account to authenticate to GCP with Terraform.
Enable Service Usage API
Enable the Service Usage API in your project:

Enable Cloud Resource Manager API
Enable the Resource Manager API:

Possible Scenarios
Pre-existing VPC (outside of Terraform)
- Description: A VPC already exists manually or by another method outside of Terraform before the module is run, in the same GCP project where the module is planned to be deployed.
- Considerations: Ensure the VPC is properly configured with necessary subnets and firewall rules.
Pre-Existing VPC with Private Services Access Enabled
- Description: Similar to the first scenario, with Private Services Access enabled for communication with Google Cloud managed services privately.
- Considerations: Verify peering configurations and routes are set for private communication.
No Pre-Existing VPC
- Description: A pre-configured VPC does not exist, and the module will create a new VPC.
- Considerations: Requires additional Terraform resources for VPC creation, increasing complexity but offering flexibility.
Important
Due to limitations of the GCP Provider, modifications in production should be handled with care to avoid unintended resource destruction.
Configuration with Pre-existing VPC
To configure the module with a pre-existing VPC, use the following Terraform code snippet:
odule "cloud-build-private-worker-pool" { source = "mdiloreto/cloud_build-private-worker_pool/google" project_id = var.project_id cb_region = var.region network = var.network network_id = var.network_id workerpool_name = var.workerpool_name alloc_range_address = var.alloc_range_address allocation_prefix_length = var.allocation_prefix_length cb_prefix_length = var.cb_prefix_length reserved_peering_ranges = var.reserved_peering_ranges workerpool_range_name = var.workerpool_range_name peering_export_custom_routes = var.peering_export_custom_routes peering_import_custom_routes = var.peering_import_custom_routes}Example TFVARS for custom configurations.
No Pre-existing VPC
When a pre-existing VPC doesn’t exist, creation is necessary through Terraform code:
resource "google_compute_network" "vpc_network" { project = var.project_id name = var.network auto_create_subnetworks = false}
resource "google_compute_subnetwork" "vpc_subnetwork" { project = var.project_id name = var.subnet ip_cidr_range = var.subnet_ip_range region = var.region network = google_compute_network.vpc_network.id}
module "cloud-build-private-worker-pool" { source = "mdiloreto/cloud_build-private-worker_pool/google" project_id = var.project_id cb_region = var.region network = var.network network_id = var.network_id workerpool_name = var.workerpool_name alloc_range_address = var.alloc_range_address allocation_prefix_length = var.allocation_prefix_length cb_prefix_length = var.cb_prefix_length reserved_peering_ranges = var.reserved_peering_ranges workerpool_range_name = var.workerpool_range_name peering_export_custom_routes = var.peering_export_custom_routes peering_import_custom_routes = var.peering_import_custom_routes}This Terraform configuration defines the necessary resources for setting up a new VPC and subnet before creating a private worker pool for Cloud Build. It includes the creation of the network and subnetwork within the specified project, and then it integrates these resources with the Cloud Build private worker pool module by Mateo Di Loreto, providing a comprehensive solution for environments without a pre-existing VPC.
Thanks for reading. The learning continues in the next article.


