2
Terraform
Installation Guide
In the previous chapter, we discussed Infrastructure as Code (IaC), mainly focusing on
what Terraform is, and then we covered a comparison of Terraform with other available
IaC options, such as AWS CloudFormation templates, Azure ARM templates, and Google
Cloud Deployment Manager. Moving on, we had a detailed discussion about Terraform
architecture and further learned about different versions of Terraform and their respective
available features.
In this chapter, we will be focusing on how you can install Terraform on your local
machine, whether it is Windows, Linux, or macOS. Once you are done with the installation
of Terraform, you should be able to start dra ing your configuration code in Terraform and
run it locally from your system.
e following topics will be covered in this chapter:
• Installing Terraform on Windows
• Installing Terraform on Linux
• Installing Terraform on macOS
34 Terraform Installation Guide
Technical requirements
To follow along with this chapter, you need to have an understanding of what Terraform
is and in what scenarios you should use it, and some basic knowledge of major cloud
providers, such as GCP, AWS, and Azure, would add more benefits.
Installing Terraform on Windows
Welcome to the Terraform installation guide. In this section, we are planning to install
Terraform on a Windows machine. en, a er the installation of Terraform, we will
try to define it in an environment variable so that you will be able to run the Terraform
configuration file from any command line, such as Windows CMD, PowerShell, or Bash.
Downloading Terraform
Before you start using Terraform, you need to download the appropriate Terraform
package for your operating system and architecture. e latest version v1.0.0 of Terraform
is available and can be downloaded. A er downloading the latest Terraform binary,
extract it and update terraform.exe in your environment path so that you can run
it from any command line.
Follow these steps to install Terraform on a Windows machine:
1. Visit the Terraform download URL, https://www.terraform.io/
downloads.html, and download the Terraform package by selecting the
Windows 32-bit or 64-bit operating system depending on your Windows
operating system, as shown in the following screenshot:
Installing Terraform on Windows 35
Figure 2.1 – Download Terraform (Windows)
2. Extract the downloaded file to any location on your computer, as shown in the
following screenshot:
Figure 2.2 – Extract the terraform file
36 Terraform Installation Guide
3. Now, you need to set the environment variable path for terraform.exe. To
do that, go to is PC, right-click on it, and go to Properties | Advanced system
settings | Environment Variables…, as shown in Figure 2.3:
Figure 2.3 – Environment variable
Installing Terraform on Windows 37
4. From the Environment Variables screen, go to the System variables section, and
under Path, just add a path by locating the terraform.exe file where you saved
it a er doing the extraction. en, click OK | OK, as shown in Figure 2.4:
Figure 2.4 – Environment path variable
38 Terraform Installation Guide
Now, let's try to verify whether we have Terraform on our system or not. Just open any
CLI, such as Windows CMD or Windows PowerShell, and then type terraform –v or
terraform -h; you should see the output shown in Figure 2.5:
Figure 2.5 – Validating Terraform (Windows)
Installing Terraform on Linux 39
Important note
You can put terraform.exe in the C:\Windows\system32 location,
provided you don't want to define a separate path in the environment variables.
We have covered the installation of Terraform on Windows and learned how we
can download it from the Terraform website, https://www.terraform.io/
downloads.html, and then saw how we can set up Terraform locally on our system.
With all this, you are ready to start dra ing your configuration code. So now, let's learn
how to install Terraform on a Linux machine.
Installing Terraform on Linux
As we have already learned how to install Terraform on Windows, now let's learn how
to install Terraform on a Linux machine. en, once we're done with the installation
of Terraform, we will learn how we can verify whether a Linux machine has Terraform
installed or not. For this section, we have considered an Ubuntu Linux machine;
considering multiple Linux platforms such as CentOS, Red Hat, and SUSE is beyond the
scope of this book, but in a nutshell, if you know how to install Terraform on one of the
Linux platforms, you should easily be able to do so on other Linux platforms too.
Downloading Terraform
Before you start using Terraform, you need to download the appropriate Terraform
package for your Linux operating system and architecture. e latest version v1.0 of
Terraform is available and can be downloaded from https://www.terraform.io/
downloads.html or https://releases.hashicorp.com/terraform/1.0.0/
terraform_1.0.0_linux_amd64.zip. We have used v1.0.0 of Terraform.
Follow these steps to install Terraform on a Linux machine:
1. SSH to your Linux, that is, Ubuntu, machine using putty.exe or any client, then
update its library using the sudo apt update -y command:
Figure 2.6 – Linux VM update
40 Terraform Installation Guide
2. Create a directory with any name and get inside it using the cd command. We are
creating a directory named terraform using the mkdir terraform && cd
terraform command.
3. en, download Terraform v1.0.0 using the wget command (https://
releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_
linux_amd64.zip):
Figure 2.7 – Download Terraform (Linux)
4. Install a so ware package called unzip that will help us to extract files from the
existing ZIP file:
sudo apt install unzip -y
5. Once installed, you can unpack the already-downloaded Terraform ZIP file using
the following command:
sudo unzip terraform_1.0.0_linux_amd64.zip
Installing Terraform on Linux 41
A er running the preceding command, you will see the following:
Figure 2.8 – Unzipping Terraform
6. Set the Linux path to point to Terraform with the following command:
export PATH=$PATH:$HOME/terraform
You can check whether Terraform installed or not by typing the terraform
–-version or terraform –-help command. We will validate the presence of
Terraform by using terraform console commands. You should be able to perform
some of the operations from terraform console as shown:
inmishrar@terraform-vm:~/terraform$ echo "1+7" | terraform
console
8
inmishrar@terraform-vm:~/terraform$
e previously defined terraform console command gives us an output by
calculating 1 + 7 = 8, which tells us that Terraform is working on our Linux machine.
42 Terraform Installation Guide
We have covered the installation of Terraform on Linux on an Ubuntu machine,
and learned how we can download it from the Terraform website, https://www.
terraform.io/downloads.html or https://releases.hashicorp.com/
terraform, and then saw how we can set up Terraform locally on our Linux system.
A er covering the initial setup of Terraform, you should be ready to start dra ing the
configuration file. So now, let's learn how to install Terraform on a macOS machine.
Installing Terraform on macOS
As you already know how to install Terraform on Linux, mainly on an Ubuntu machine,
now let's learn how to install Terraform on a Mac machine. Once we're done with
the installation of Terraform, we will learn how to verify whether a Mac machine has
Terraform installed or not.
Downloading Terraform
Before you start using Terraform, you need to download the appropriate Terraform
package for your Mac operating system and architecture. e latest version, such as v1.0.0,
of Terraform is available and can be downloaded from https://www.terraform.
io/downloads.html for a Mac system. For our case, we have used v1.0.0 of Terraform.
Follow these steps to install Terraform on a Mac machine:
1. To install Terraform on a MacBook, you simply have to download the Terraform
binary from https://www.terraform.io/downloads.html, as you can
see in Figure 2.9:
Figure 2.9 – Download Terraform (Mac)
Installing Terraform on macOS 43
2. A er downloading the Terraform binary, unzip it and place it in any folder path;
you can either update the $PATH variable to point to the location where the
Terraform binary is or you can just move it to the local bin directory. If you choose
the latter, then on MacBook, you will need to execute the following command:
sudo mv ./terraform /usr/local/bin:
Figure 2.10 – Terraform path
44 Terraform Installation Guide
3. You can validate whether Terraform was installed successfully on your MacBook by
using either the terraform -h or terraform -v commands. You can see the
output in Figure 2.11:
Figure 2.11 – Validate Terraform (Mac)
We have covered the installation of Terraform on macOS and learned how to download it
from the Terraform website, https://www.terraform.io/downloads.html, and
then saw how to set up Terraform locally on our Mac system. A er covering the initial
setup of Terraform, you should be ready to start dra ing your first configuration file.
Summary 45
Summary
In this chapter, you learned how to install terraform.exe on different machines, such
as Windows, Mac, and Linux, which will help you to get started with Terraform.
In the next chapter, we will discuss all the Terraform core components, such as resources,
data, variables, output, inbuilt functions, backend, locals, iterations, providers, and
provisioners. ese core components that will be discussed will cover multiple providers,
such as Azure, GCP, and AWS.
Questions
e answers to these questions can be found in the Assessment section at the end of this
book:
1. How can you check which version of Terraform is installed on your system?
A. terraform -psversion
B. terraform --version
C. terraform --help
D. terraform fmt
2. You are using a Linux machine where Terraform is already installed. As you are
very new to Terraform, you are looking for the "help" option in Terraform. Which
command will you be executing? Select all the possible answers:
A. terraform -h
B. terraform -help
C. terraform fmt
D. terraform plan
3. Do you need to install the Go language library on your local system if you want to
install Terraform?
A. Yes
B. No
46 Terraform Installation Guide
Further reading
You can check out the following links for more information about the topics that were
covered in this chapter:
• Terraform download: https://www.terraform.io/downloads.html
• Terraform releases: https://github.com/hashicorp/Terraform/
releases
• Terraform installation guide: https://learn.hashicorp.com/tutorials/
terraform/install-cli