Terraform in Google Compute Made Easy

1007 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
    and save the file
    • A file with all of the populated variables will look something similar to the image below:
  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
  • Then run the command
  • Then run the command
  1. Once the authentication variables have been exported, run the command
  1. Then run the command
  1. And finally run the command
    • 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
          (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:
        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
        1. Then run the command
        1. And finally run the command
        • 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
        1. To destroy the networking infrastructure you have built (from the compute directory), run the command

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

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

Click on a tab to select how you'd like to leave your comment

Leave a Comment

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

Menu