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:
- Log into the Google Developers Console and select a project
- The API Manager view should be selected, click on “Credentials” on the left, then “Create credentials”, and finally “Service account key”
- Select “Compute Engine default service account” in the “Service account” dropdown, and select “JSON” as the key type
- Clicking “Create” will download your credentials
- This repository cloned locally (install Git here)
- You have Terraform installed (install it here)
Instructions:
Basic Networking
- From the command-line, change directory into:{location_of_this_repo}/azure/environments/{environment_you_wish_to_build}/networking
- 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”]
- 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}
- Once the authentication variables have been exported, run the command
terraform get
- Then run the command
terraform plan -var-file=./terraform.tfvars
- 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
-
-
-
- From the command-line, change directory to: cd ../compute
-
- 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
- In this directory, open “terraform.tfvars” with your editor of choice and enter a value for variables
-
-
- 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”
- A file with all of the populated variables will look something similar to the image below:
- 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
-
-
- Then run the command
terraform plan -var-file=./terraform.tfvars
- Then run the command
-
- And finally run the command
terraform apply -state=./terraform.tfstate -var-file=./terraform.tfvars
- And finally run the command
-
- You have now built basic compute and networking infrastructure into Google Cloud Platform!
- To destroy the compute infrastructure you have built, run the command
terraform destroy -state=./terraform.tfstate -var-file=./terraform.tfvars
-
- 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
- To destroy the networking infrastructure you have built (from the compute directory), run the command
-
-