Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Getting Started with Terraform in 5 Minutes

5 min read

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. This includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

GitHub - hashicorp/terraform: Terraform enables you to safely and  predictably create, change, and improve infrastructure. It is an open  source tool that codifies APIs into declarative configuration files that  can be shared

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

Following are the key features of Terraform:

  1. Infrastructure as a Code: As discussed earlier, Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.
  2. Execution Plans: Terraform has a “planning” step where it generates an execution plan. The execution plan shows what Terraform will do when you call apply. This lets you avoid any surprises when Terraform manipulates infrastructure.
  3. Resource Graph: Terraform builds a graph of all your resources, and parallelizes the creation and modification of any non-dependent resources. Because of this, Terraform builds infrastructure as efficiently as possible, and operators get insight into dependencies in their infrastructure.
  4. Change Automation: Complex changesets can be applied to your infrastructure with minimal human interaction. With the previously mentioned execution plan and resource graph, you know exactly what Terraform will change and in what order, avoiding many possible human errors.

Installing Terraform on MacOS

Homebrew is a free and open-source package management system for Mac OS X. Install the Terraform formula from the terminal.

$ brew install terraform

NOTE: Homebrew and the Terraform formula are NOT directly maintained by HashiCorp. The latest version of Terraform is always available by manual installation.

Verify the latest release

terraform version
Terraform v1.0.11
on darwin_amd64

Verify the installation

Verify that the installation worked by opening a new terminal session and listing Terraform’s available subcommands.

$ terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you’re just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. ##…

Add any subcommand to terraform -help to learn more about what it does and available options.

$ terraform -help plan

Troubleshoot

If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.

Enable tab completion

If you use either bash or zsh you can enable tab completion for Terraform commands. To enable autocomplete, run the following command and then restart your shell.

$ terraform -install-autocomplete

Installing Terraform on Linux

A binary distribution is avaialble for all environments. Let’s grab the latest version of it for linux.

$ wget https://releases.hashicorp.com/terraform/1.0.11/terraform_1.0.11_linux_amd64.zip

Then unzip the archieve,

$ unzip terraform_1.0.11_linux_amd64.zip

Check the executable permission on the binary, if it’s not executable, make it executable using the below commmand,

$ chmod +x terraform

Finally make sure that terrform is avaiable in PATH. So, let’s move the binary into /usr/local/bin directroy,

$ sudo mv terraform /usr/local/bin

Now you are ready to run terraform commands. Open up a new termnal and run a command terraform and enter,

$ terraform

Verify the installation

Verify that the installation worked by opening a new terminal session and listing Terraform’s available subcommands.

$ terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you’re just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. ##…

Add any subcommand to terraform -help to learn more about what it does and available options.

$ terraform -help plan

Troubleshoot

If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.

Enable tab completion

If you use either bash or zsh you can enable tab completion for Terraform commands. To enable autocomplete, run the following command and then restart your shell.

$ terraform -install-autocomplete

Installing Terraform on Windows

A binary distribution is avaialble for all environments. Let’s grab the latest version of it for windows.

Open up a powershell on your windows machine, cd to a directroy to D drive and create an Terraform directory,

PS C:\Users\user>D:

Get an exe from the below url,

PS D:\> curl.exe -O https://releases.hashicorp.com/terraform/0.12.26/terraform_0.12.26_windows_amd64.zip

Then unzip this archieve, rename a directory to terraform and we will see a single binary file name terraform and add it’s path into environment variables.

PS D:\Terraform> Expand-Archive terraform_0.12.26_windows_amd64.zip
PS D:\> Rename-Item -path .\terraform_0.12.26_windows_amd64\ .\terraform

Regarding setting up an environment variable, you can add terraform path in Path variable as shown in below screenshot,

And, your are done. Now open up a terminal and run a command terrform and enter

PS D:\terraform> terraform

Verify the installation

Verify that the installation worked by opening a new powershell or cmd session and listing Terraform’s available subcommands.

PS D:\terraform> terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you’re just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. ##…

Add any subcommand to terraform -help to learn more about what it does and available options.

PS D:\terraform> terraform -help plan

Troubleshoot

If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your Path variable contains the directory where Terraform was installed.

How to setup Terraform for Google Cloud Engine

Pre-requiste

In order to avoid explicitly using the GCP service key, we are going to use GOOGLE_APPLICATION_CREDENTIAL environment variable that points to our GCP service key.

Installing Terraform ~> 0.12.26

If you have a service key at your disposal

export GOOGLE_APPLICATION_CREDENTIAL = {path to your service key file}

If you have not created a service account and a service key then follow below steps

Install gcloud cli

The gcloud cli is a part of Google Cloud SDK. We must download and install the SDK on your system and initialize it before you can use the gcloud command-line tool.

Note: You can follow the install script given in the Google Cloud SDK documentation.

Google Cloud SDK Quickstart script for CentOS

sudo tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM

[google-cloud-sdk]

name=Google Cloud SDK baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOM

Installing Google cloud SDK

yum install google-cloud-sdk

Once the SDK is installed, run gcloud init to initialize the SDK,

gcloud init

Run the following scripts

export PROJECT_ID={Name of your GCP Project}

export GOOGLE_APPLICATION_CREDENTIALS=~/.config/gcloud/${PROJECT_ID}-terraform-admin.json

gcloud iam service-accounts create terraform --display-name "Terraform admin account"

gcloud projects add-iam-policy-binding ${PROJECT_ID} --member serviceAccount:terraform@${PROJECT_ID}.iam.gserviceaccount.com   --role roles/owner

gcloud services enable compute.googleapis.com

gcloud services enable iam.googleapis.com

gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} --iam-account terraform@${PROJECT_ID}.iam.gserviceaccount.com```

Note – you would need to export the GOOGLE_APPLICATION_CREDENTIALS every time you work with terraform when interacting with your configurations.

References:

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index