Terraform on AWS, GCP, and Azure

hashicorp-terraform-banner
Terraform

This is a Unix-focused guide to Terraform. It will work on Linux distributions and Mac OS X. During this video, I will be using a Mac or CentOS 7.

What is Terraform?

Terraform is an infrastructure tool for Building, changing, and Versioning infrastructure. Often referred to as Infrastructure as Code. It is released by the HashiCorp organization.

It works with two coding data languages:

  • HashiCorp Config Language (HCL)
  • JSON

What is Terraform used for?

In its simplest form, Terraform can be used to create server infrastructure on several different platforms automatically. It can also be integrated into Continuous Integration and Continuous Delivery (CICD).

Terraform is a cloud-agnostic platform and is popular for the following cloud platforms :

  • The Google Cloud Platform (GCP)
  • Amazon Web Services (AWS)
  • Microsoft Azure
  • VMware
  • Red Hat OpenStack

What is a Terraform Provider?

Providers are plugins that enable Terraform to interact with specific platforms and services (e.g., AWS, GCP, Azure). They expose resources and data sources for managing the infrastructure.

Terraform providers are now known as the Terraform Registry

For a full list of temporary providers, see:

What is the use of Terraform in DevOps?

It can also be used as a bulk configuration tool, similar to products like Ansible. It requires a Plan, Execution of the plan, and Applying to the infrastructure. There is minimal human interaction, and it is an incredibly powerful tool.

In this example, I will use Google Cloud Platform (GCP) and Github to implement Terraform infrastructure. GCP will be the cloud provider where I will build the infrastructure, and GitHub is a cloud-based code repository for storing revisions and versions of code.

Terraform: Google Cloud Platform (GCP)

Prerequisites and Tools

  • Accounts:
    • Active Google Cloud Platform (GCP) account.
    • GitHub account (for code versioning).
  • Tools:
    • A text editor (e.g., vi, nano, VS Code).
    • Basic understanding of command-line operations.

Step 1 – Terraform Installation (CentOS/Ubuntu)

For CentOS 7:

Download Terraform:

Bash
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo 
sudo yum -y install terraform 

For Ubuntu:

Download Terraform:

Bash
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" 
sudo apt-get update && sudo apt-get install terraform

How to set Terraform and connect it to GitHub

If you have not already got a GitHub account, sign up at https://github.com

Log into GitHub and create a new repository

The Next Steps are :

  • Choose the name of your GitHub repo
  • Choose public or private
  • tick initialize this report and choose the Terraform .gitignore and Apache license 2.0
  • Then click Create the repository
  • Press the Create Repository button
  • then, once created, click the clone repository and copy the GitHub URL
  • Next, go back to our CentOS installation. I had to install the CentOS git applications and pre-req files (I was using CentOS core, which doesn’t include git by default)

Bash
yum install git -y

next, Clone the GitHub repo to the CentOS shell

Bash
git clone https://github.com/TurboBailey1980/Terraform-Turbogeek.git

You are now synced to your GitHub repo, and you can read/write to your GitHub repo.

If you want to learn about GitHub, check out the GitHub website help files

How to configure Terraform on Linux (Mac / Ubuntu / Centos)

centos

Linux is my preferred Operating System for installing Terraform. It is natively supported. In this example, I will be using Centos 7 Core (or Minimal installation)

Terraform also works on Windows 10 Desktop and Windows Server products. Click here for a guide on how to install it on Windows.

Download Terraform using curl

Bash
curl -O https://releases.hashicorp.com/terraform/0.11.10/terraform_0.11.10_linux_amd64.zip

Next, unzip it to your /usr/bin directory (note I am doing this as root)

Bash
unzip terraform_0.11.10_linux_amd64.zip -d /usr/bin/

(As I am using a CentOS7 core I needed to yum install unzip first.)

Bash
[root@Ansible-Terraform-Control tmp]# terraform -v

Next, you need to create a working directory for Terraform and an empty Terraform config file.

This step is really important and will greatly help organize your projects once you start using Terraform

Bash
mkdir terraform-templates && cd terraform-templates
touch template.tf
terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

You now have terraform installed on your Linux distribution and can execute the terraform command from any location as we have installed it directly to /usr/bin system $PATH

If you want to learn more about the core command lists above – check out this page on my site.

Configure Google Cloud Platform (GCP) to work with Terraform

Log in to the Google Cloud Platform and create a new project https://console.cloud.google.com

  • Create an IAM Service Account for Terraform to use.
  • Select IAM & Admin > Service Accounts > Create Service Account
  • Enter a name for the service account
  • Set the role as Service Account Admin
  • Create a JSON key
  • Check the key has been downloaded to your computer
  • Copy the JSON file  to the CentOS Server, for ease of use save it “below” your project folder

Now we can write the connections.tf file to “connect” to GCP

I prefer to use vi or nano, but you can use whatever text editor you prefer

Bash
vi connections.tf

update the connections.tf with

Bash
provider "google" {
 credentials = "${file("../turbogeek-terraform-d64341070e0c.json")}"
 project = "turbogeek-terraform"
 region = "us-west1"
}


If you have other providers, such as AWS, Azure – you can add them here as well

Next, we test the settings are correct by running

Bash
terraform init

Test create a resource on Google Cloud Platform (GCP)

For this test to work – make sure your terraform service account has the following permissions

  • Compute Network Admin
  • Service Account Admin

Next, we can test the automated creation of a resource within GCP. We will create a simple network name

Bash
resource "google_compute_network" "our_development_network" {
 name = "turbogeek"
 auto_create_subnetworks = true
}

Bash
terraform plan
terraform apply

This will cause Terraform to reach out to GCP and see if the Turbogeek network already exists

Elsewhere On TurboGeek:  Delete GCP Project With a Lien

Now, if you check GCP, you will see the automatically created resource

MAKE SURE YOU DELETE THE VPC NETWORK TO ENSURE YOU ARE NOT BILLED BY GOOGLE 

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

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

Translate »