Terraform GCP
You can deploy CubeAPM on GCP (Compute Engine VM) using our Terraform code.
Prerequisites
Before getting started, ensure you have the following configuration ready:
- Terraform installed (v1.0.0 or higher).
- Google Cloud Platform (GCP) credentials configured locally (e.g., via
gcloud auth application-default login). - An existing GCP VPC and Subnetwork where CubeAPM will be deployed.
Installation
-
Clone the
CubeAPM-Terraform-VM-GCPrepository:git clone https://github.com/cubeapm/CubeAPM-Terraform-VM-GCP.git -
Initialize Terraform:
cd CubeAPM-Terraform-VM-GCP
terraform init -
Deploy CubeAPM:
While applying terraform it asks for following values, provide as per your requirement:
Variable Type Default Description gcp_project_idstringThe GCP Project ID where resources will be deployed. gcp_regionstring"asia-south1"The GCP Region. gcp_zonestring"asia-south1-a"The GCP zone to deploy the resources to. gcp_imagestring"ubuntu-os-cloud/ubuntu-2404-lts-amd64"The GCP image to use for the instance. gcp_machine_typestring"e2-standard-4"The GCP machine type to use. gcp_networkstringVPC network name or self-link. gcp_subnetworkstringSubnetwork name or self-link. load_balancer_scopestringSelect the load balancer scope: "global"or"regional".load_balancer_typestringSelect the load balancer type: "internal"or"internet-facing".create_lbbooleantrueSet to trueto create a new Load Balancer (URL Map, Target Proxy, Forwarding Rule). Iffalse, only the Backend Service is created.warningTo avoid being prompted for these variables every time you run Terraform, create a
terraform.tfvarsfile in the same directory: -
Here's a sample
terraform.tfvarsfile you can take a reference from here and update your terraform.tfvars file based on your infrastructure requirements:- Creating new LB
- Using existing LB
Example 1: Creating a New Load Balancer
# GCP Authentication & Location
gcp_project_id = "my-cubeapm-project-12345"
gcp_region = "asia-south1"
gcp_zone = "asia-south1-a"
# VM Specifications
gcp_image = "ubuntu-os-cloud/ubuntu-2404-lts-amd64"
gcp_machine_type = "e2-standard-4"
# Networking
gcp_network = "my-vpc-network"
gcp_subnetwork = "my-app-subnet"
# Load Balancer Configuration
create_lb = true
load_balancer_scope = "global"
load_balancer_type = "internet-facing"Example 2: Using an Existing Load Balancer
# GCP Authentication & Location
gcp_project_id = "my-cubeapm-project-12345"
gcp_region = "asia-south1"
gcp_zone = "asia-south1-a"
# VM Specifications
gcp_image = "ubuntu-os-cloud/ubuntu-2404-lts-amd64"
gcp_machine_type = "e2-standard-4"
# Networking
gcp_network = "my-vpc-network"
gcp_subnetwork = "my-app-subnet"
# Load Balancer Configuration
create_lb = false
load_balancer_scope = "global"
load_balancer_type = "internet-facing"infoWhy are
load_balancer_scopeandload_balancer_typerequired whencreate_lb = false? Even when you are using an existing Load Balancer, Terraform still needs to create a Backend Service for you to attach to your URL Map. In GCP, Backend Services are categorized by load balancer type. Terraform needs these variables to know whether to provision a Global or Regional backend service, and whether to configure it for Internal or External traffic, ensuring it is compatible with your existing Load Balancer. -
Once your variables are configured, run the following command to provision the infrastructure:
# use this syntax if you have created `terraform.tfvars` file.
terraform apply
# use this syntax if you don't want to create the `terraform.tfvars` file.
terraform apply -var="variable_name=variable_value" ...
Knowledge Base
For more information about Terraform for GCP, please refer to the Terraform GCP documentation.