terraform-google-examples
repository·master·Indexed 20 days ago
https://github.com/googlecloudplatform/terraform-google-examplesA collection of Terraform module examples demonstrating best practices and common architectural patterns for deploying Google Cloud Platform resources. Examples include deploying custom machine types with bastion hosts and NAT gateways, GKE clusters with Helm, GKE multi-cluster load balancing, and GKE Kubernetes service load balancers.
What's inside terraform-google-examples
- This example demonstrates how to implement multi-region ingress using an L7 HTTP Load Balancer distributed across regional Google Kubernetes Engine (GKE) clusters. It uses Terraform to provision the necessary Google Cloud resources to support multi-cluster traffic management.
Test the Multi-Cluster Load Balancer
masterOnce the deployment is complete, use the provided test script to verify the setup and retrieve the Load Balancer's IP address.
- Run
./test.shand wait for the provisioning to finish. - Retrieve the HTTP URL using the
load-balancer-ipTerraform output.
./test.sh echo http://$(terraform output load-balancer-ip)- Run
Install Terraform
masterIf Terraform is not already installed on your system, you can use the provided installation script from the parent directory.
../terraform-install.shDeploy Custom Machine Types with Terraform
masterThis example demonstrates how to deploy a Google Cloud architecture consisting of an instance with a custom machine type, a bastion host, and a NAT gateway.
Prerequisites
- A Google Cloud Project ID.
- Terraform installed on your local machine or Cloud Shell.
gcloudCLI configured.
Deployment Steps
Navigate to the directory:
[[ `basename $PWD` != example-custom-machine-types ]] && cd example-custom-machine-typesSet up the environment: Replace
YOUR_PROJECTwith your actual project ID and authenticate:PROJECT=YOUR_PROJECT gcloud config set project ${PROJECT} [[ $CLOUD_SHELL ]] || gcloud auth application-default login export GOOGLE_PROJECT=$(gcloud config get-value project)Configure Remote Backend: Create a GCS bucket to store the Terraform state file:
BUCKET=${GOOGLE_PROJECT}-terraform gsutil mb gs://${BUCKET} PREFIX=tf-es-custom-machine/state cat > backend.tf <<EOF terraform { backend "gcs" { bucket = "${BUCKET}" prefix = "${PREFIX}" } } EOFInitialize and Apply:
terraform init terraform apply
terraform init terraform applySet up the environment for the GKE Kubernetes Service Load Balancer example
masterBefore running Terraform, you must configure your Google Cloud project and authentication. Replace
YOUR_PROJECTwith your actual Google Cloud Project ID.- Set the project ID in your shell.
- Configure
gcloudto use that project. - Authenticate for Terraform using Application Default Credentials (ADC) if you are not running in Cloud Shell.
PROJECT=YOUR_PROJECT gcloud config set project ${PROJECT} [[ $CLOUD_SHELL ]] || gcloud auth application-default login export GOOGLE_PROJECT=$(gcloud config get-value project)Verify Custom Machine Type and NAT Gateway connectivity
masterAfter running
terraform apply, you can verify the deployment by performing the following tests:1. Access the Bastion Host
Use SSH agent forwarding to access the bastion host. This allows you to forward ports to services like Cerebro or Kibana:
eval $(ssh-agent) ssh-add ~/.ssh/google_compute_engine eval $(terraform output bastion)2. Access the Custom Machine
SSH directly into the custom instance:
ssh tf-custom-13. Verify NAT Gateway Routing
To ensure the custom machine is correctly routing its external traffic through the NAT gateway, check its external IP address. The returned IP should match the IP of the NAT gateway:
curl http://ipinfo.io/ipssh tf-custom-1 curl http://ipinfo.io/ipSet up the environment for GKE Multi-Cluster Load Balancing
masterBefore running Terraform, you must configure your Google Cloud project and authentication. Replace
YOUR_PROJECTwith your actual Google Cloud Project ID.- Set the project ID in your shell.
- Configure
gcloudto use that project. - Authenticate for Terraform using Application Default Credentials (ADC). If you are not in Cloud Shell, run
gcloud auth application-default login.
PROJECT=YOUR_PROJECT gcloud config set project ${PROJECT} [[ $CLOUD_SHELL ]] || gcloud auth application-default login export GOOGLE_PROJECT=$(gcloud config get-value project)Test the Load Balancer and verify connectivity
masterOnce Terraform has finished applying, you can verify the deployment by running the provided test script and then querying the load balancer's IP address directly via
curl.# Wait for the load balancer to be provisioned ./test.sh # Verify response from load balancer curl http://$(terraform output load-balancer-ip)Cleanup deployed resources
masterTo avoid ongoing costs, delete all resources created by this Terraform configuration using the destroy command.
terraform destroyCleanup resources
masterTo avoid leaving orphaned resources (like forwarding rules or firewall rules), you must delete the
nginx-ingressHelm release specifically before destroying the rest of the infrastructure.Run the following command to target the release, wait for the GCE controller to clean up, and then destroy everything else:
terraform destroy -input=false -auto-approve -target helm_release.nginx-ingress && sleep 60 && \ terraform destroyConnect to the cluster using kubectl and helm
masterTo interact with the GKE cluster directly, configure your local
kubectlcontext using the outputs from Terraform.- Get Credentials:
gcloud container clusters get-credentials $(terraform output cluster_name) --zone $(terraform output cluster_zone)
2. **Verify Connectivity**: ```bash kubectl get pods helm listgcloud container clusters get-credentials $(terraform output cluster_name) --zone $(terraform output cluster_zone) kubectl get pods helm list- Get Credentials:
Deploy the GKE Kubernetes Service Load Balancer using Terraform
masterTo deploy the infrastructure, navigate to the example directory, ensure Terraform is installed, and run the standard Terraform initialization and application commands.
# Change to the example directory [[ `basename $PWD` != example-g8s-service-lb ]] && cd example-gke-k8s-service-lb # Install Terraform (if not present) ../terraform-install.sh # Initialize and apply terraform init terraform apply