terraform-google-examples

repository·master·Indexed 20 days ago

https://github.com/googlecloudplatform/terraform-google-examples

A 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.

Tokens
4.7K
Snippets
25
Records
39
Agent score
71%

What's inside terraform-google-examples

  1. Deploy Custom Machine Types with Terraform

    master

    This 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.
    • gcloud CLI configured.

    Deployment Steps

    1. Navigate to the directory:

      [[ `basename $PWD` != example-custom-machine-types ]] && cd example-custom-machine-types
    2. Set up the environment: Replace YOUR_PROJECT with 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)
    3. 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}"
        }
      }
      EOF
    4. Initialize and Apply:

      terraform init
      terraform apply
    terraform init
    terraform apply
  2. Set up the environment for the GKE Kubernetes Service Load Balancer example

    master

    Before running Terraform, you must configure your Google Cloud project and authentication. Replace YOUR_PROJECT with your actual Google Cloud Project ID.

    1. Set the project ID in your shell.
    2. Configure gcloud to use that project.
    3. 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)
  3. Verify Custom Machine Type and NAT Gateway connectivity

    master

    After 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-1

    3. 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/ip
    ssh tf-custom-1
    curl http://ipinfo.io/ip
  4. Set up the environment for GKE Multi-Cluster Load Balancing

    master

    Before running Terraform, you must configure your Google Cloud project and authentication. Replace YOUR_PROJECT with your actual Google Cloud Project ID.

    1. Set the project ID in your shell.
    2. Configure gcloud to use that project.
    3. 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)
  5. Cleanup resources

    master

    To avoid leaving orphaned resources (like forwarding rules or firewall rules), you must delete the nginx-ingress Helm 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 destroy
  6. Connect to the cluster using kubectl and helm

    master

    To interact with the GKE cluster directly, configure your local kubectl context using the outputs from Terraform.

    1. Get Credentials:
      gcloud container clusters get-credentials $(terraform output cluster_name) --zone $(terraform output cluster_zone)
    
    2. **Verify Connectivity**:
       ```bash
    kubectl get pods
    helm list
    gcloud container clusters get-credentials $(terraform output cluster_name) --zone $(terraform output cluster_zone)
    kubectl get pods
    helm list
  7. Deploy the GKE Kubernetes Service Load Balancer using Terraform

    master

    To 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