This example creates a global HTTP forwarding rule to forward traffic to instance groups in the us-west1 and us-east1 regions.
Figure 1. diagram of Google Cloud resources
Install Terraform if it is not already installed (visit terraform.io for other distributions):
../terraform-install.sh
Set the project, replace YOUR_PROJECT
with your project ID:
PROJECT=YOUR_PROJECT
gcloud config set project ${PROJECT}
Create service account and use it in terraform.tfvars
or edit terrafrom.tfvars
cp terraform.tfvars.examples terrform.tfvars
Pre download modules to work. Better so, if you can edit the source code of the module itself.
cd ../
git clone https://github.com/terraform-google-modules/terraform-google-project-factory
git clone https://github.com/terraform-google-modules/terraform-google-cloud-nat.git
git clone https://github.com/terraform-google-modules/terraform-google-lb-http.git
git clone https://github.com/terraform-google-modules/terraform-google-vm.git
terraform init
terraform apply
- Wait for the load balancer to be provisioned:
./test.sh
- Open the URL of the load balancer in your browser:
echo http://$(terraform output load-balancer-ip)
You should see the instance details from the region closest to you.
Resize the instance group of your closest region to cause traffic to flow to the other group.
- If you are getting traffic from
group1
(us-west1), scale group 1 to 0 instances:
TF_VAR_group1_size=0 terraform apply
- Otherwise scale group 2 (us-east1) to 0 instances:
TF_VAR_group2_size=0 terraform apply
- Open the external IP again and verify you see traffic from the other group:
echo http://$(terraform output load-balancer-ip)
It may take several minutes for the global load balancer to be created and the backends to register.
- Remove all resources created by terraform:
terraform destroy