Terraform in Google Compute Made Easy

5622 VIEWS

·

This blog provides some basic examples of templated infrastructure for Google Cloud Platform and how to deploy said infrastructure into google compute following their CLI/API authentication processes.

Terraform and Google Cloud Platform

This basic compute demo will consist of the orchestration of blank n1-standard-1 Google Compute instances and the basic networking that is required for this into Microsoft Azure.

Pre-requisites:

  • It is assumed that this demo is being run from a UNIX based machine and that basic existing experience with UNIX based systems has been acquired
  • You have a Google Cloud Platform account – Sign up here
  • You have GCP API authentication credentials for your account stored locally, the steps to do this are as follows:
  1. Log into the Google Developers Console and select a project
  2. The API Manager view should be selected, click on “Credentials” on the left, then “Create credentials”, and finally “Service account key”
  3. Select “Compute Engine default service account” in the “Service account” dropdown, and select “JSON” as the key type
  4. Clicking “Create” will download your credentials

Instructions:

Basic Networking

  1. From the command-line, change directory into:{location_of_this_repo}/azure/environments/{environment_you_wish_to_build}/networking
  1. In this directory, open ‘terraform.tfvars‘  with your editor of choice and enter a value for variable
    network_name

    and save the file

    • A file with all of the populated variables will look something similar to the image below:
# Network #
network_name = “demo-network”

# Allow TCP FW #
fw_name		= “ssh”
allow_ports	= [“22”]
  1. Export all of the environment variables required for authentication with the Google Cloud Platform API, steps are as follows:
  • From the command-line, run the command
    export GOOGLE_CREDENTIALS=$(cat {enter_the absolute_path_to_the_credentials_json_file_downloaded_in_prereqs here})
  • Then run the command
    export GOOGLE_PROJECT={enter_your_gcp_project_name_here}
  • Then run the command
    export GOOGLE_REGION={enter_your_selected_gcp_region_here}
  1. Once the authentication variables have been exported, run the command
    terraform get
  1. Then run the command
    terraform plan -var-file=./terraform.tfvars
  1. And finally run the command
    terraform apply -state=./terraform.tfstate -var-file=./terraform.tfvars
    • The base networking infrastructure has now been built, now the compute infrastructure can be built on-top of it

Basic Compute

        1. From the command-line, change directory to: cd ../compute
        1. In this directory, open “terraform.tfvars” with your editor of choice and enter a value for variables
          network_name

          (the name of the network create above), “owner” (used to identify the creator) and save the file

          • A file with all of the populated variables will look something similar to the image below:
            # Demo Node #
            demo_node_image			= “centos-cloud/centos-6”
            demo_node_machine_type	= “f1-micro”
            demo_node_zone			= “europe-west1-b”
            demo_node_network_id		= “demo-network”
            demo_node_count			= “1”
            demo_node_owner			= “demo_users_name”
            
        1. As the Google Cloud Platform API authentication variables have already been exported above they don’t need to be exported again (unless a new shell/terminal has been opened, then please re-do step 3 of the ‘Basic Networking’ section), so now we can run the command
          terraform get
        1. Then run the command
          terraform plan -var-file=./terraform.tfvars
        1. And finally run the command
          terraform apply -state=./terraform.tfstate -var-file=./terraform.tfvars
        • You have now built basic compute and networking infrastructure into Google Cloud Platform!
        1. To destroy the compute infrastructure you have built, run the command
          terraform destroy -state=./terraform.tfstate -var-file=./terraform.tfvars
        1. To destroy the networking infrastructure you have built (from the compute directory), run the command
          terraform destroy -state=../networking/terraform.tfstate -var-file=../networking/terraform.tfvars


Jordan Taylor is a DevOps Practitioner. His goal is to learn every DevOps tool and technology, developing an arsenal of knowledge that covers every aspect of the DevOps space. With a specialization in automation, configuration management, cloud orchestration and CI/CD, Jordan is always looking to implement forward-thinking ideas that result in ultimate efficiency and value, while up-skilling and enabling those around him in the technologies used to innovate. Jordan's current favorite tools are Terraform, Docker and Vault.


Discussion

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar