Skip to content

Example use global HTTP load ballance use terraform google modules for it.

Notifications You must be signed in to change notification settings

pksmall/terraform-google-lb-http-mig

Repository files navigation

HTTP Load Balancer Example

button

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

architecture diagram

Install Terraform

Install Terraform if it is not already installed (visit terraform.io for other distributions):

../terraform-install.sh

Set up the environment

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

Get Terraform Google Modules

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

Run Terraform

terraform init
terraform apply

Testing

  1. Wait for the load balancer to be provisioned:
./test.sh
  1. 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.

Test balancing to other region

Resize the instance group of your closest region to cause traffic to flow to the other group.

  1. If you are getting traffic from group1 (us-west1), scale group 1 to 0 instances:
TF_VAR_group1_size=0 terraform apply
  1. Otherwise scale group 2 (us-east1) to 0 instances:
TF_VAR_group2_size=0 terraform apply
  1. 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.

Cleanup

  1. Remove all resources created by terraform:
terraform destroy

About

Example use global HTTP load ballance use terraform google modules for it.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published