-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

HashiCorp Terraform Associate (003) Exam Guide
By :

Before exploring the book, you need to set up Terraform CLI and configure one of the cloud platforms for Terraform usage. We use the AWS cloud platform in this book.
Terraform works seamlessly across multiple operating systems. It offers multiple installation options. The following sections will help you understand the Terraform components and the installation and configuration of Terraform for AWS:
By the end of this section, you will understand the different components of Terraform and the different ways of installing Terraform across Linux, Windows, and macOS.
When Terraform was launched in 2014, it was a single component that included the provider plugins as part of the binary. In 2017, HashiCorp made the decision to separate the providers to allow the provider’s code to be managed independently from the Terraform binary (from Terraform version 0.10).
Currently, Terraform has a plugin-based architecture in which the Terraform core makes a remote procedure call (RPC) to the Terraform plugins to provision and manage the infrastructure.
Terraform is composed of two components:
At the time of this book, Terraform supports the Windows, macOS, FreeBSD, OpenBSD, Solaris, and Linux (Ubuntu/Debian, CentOS/RHEL, Fedora, and Amazon Linux) operating systems. Figure 0.2 summarizes the details of the Terraform installation.
Figure 0.2: Terraform installation
In the following section, you will learn how to install Terraform on macOS, Linux, and Windows.
The Terraform installation process varies depending on the operating system and the CPU architecture.
You can install Terraform on macOS using a package manager (Homebrew), by downloading the pre-compiled binary, or by compiling it from the source. In this section, you will learn how to install via the package manager and pre-compiled binary.
Homebrew is a free, open source package management system for macOS. The following steps will help you install Terraform using the Homebrew package manager:
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Figure 0.3: Terraform installation using Homebrew
terraform -v
Figure 0.4: Terraform installation validation
If you are looking for a specific version of Terraform, it is recommended to go with the pre-compiled binary option. Another reason to go with this installation option is that the package managers may not always have the latest version of the software.
Pre-compiled binaries are available in both AMD64 and ARM64. Make sure to choose the right binary depending on your CPU architecture. The following steps will help you install Terraform using a pre-compiled binary:
https://developer.hashicorp.com/terraform/install
Figure 0.5: Pre-compiled binary for macOS
~/Downloads
and hence the command is being run from the Downloads
folder:~/Downloads $ unzip terraform_1.7.3_darwin_arm64.zip
Figure 0.6: Unzipping the Terraform ZIP file
/usr/local/bin
:mv ~/Downloads/terraform /usr/local/bin/
terraform -v
You should see the output detailing the Terraform version as shown in Figure 0.7.
Figure 0.7: Pre-compiled binary installation validation
Note
If you get an error when you run terraform -v
, make sure your PATH
variable has the location where the Terraform binary is moved to. Here, the binary is moved to /usr/local/bin
, and this should be present in your PATH
environment variable on your operating system.
Terraform is available for Ubuntu/Debian-based OS, CentOS/RHEL-based OS, Fedora, and Amazon Linux operating systems. All operating systems support installation via the package manager, pre-compiled binary, and compilation from the source.
In the following sections, you will learn how to install Terraform in Ubuntu and Amazon Linux.
Use the package manager specific to your Linux OS distribution to install Terraform. Here, you will be using Ubuntu and Amazon Linux as examples. For other OS, refer to https://developer.hashicorp.com/terraform/install.
The following steps will help you install Terraform via the package manager in Ubuntu:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
apt
repository that contains Terraform:echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
terraform version
If you get the version details as output, then Terraform has been installed successfully.
The following steps will help you install Terraform via the package manager in Amazon Linux:
yum-config-manager
to manage the repositories:sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
terraform version
Copy the appropriate binary’s link for your OS and CPU architecture from the following URL:
https://developer.hashicorp.com/terraform/install
All the operating systems have similar steps: install the unzip
application, download the Terraform binary, unzip the archive, move the binary to the right location, and test it. To avoid repetition, this section will cover Ubuntu only. For other OS, please make the required changes in the commands (mostly in the unzip
installation) before proceeding with this method.
The following steps will help you install Terraform using a pre-compiled binary in Ubuntu:
unzip
package, which is required to unzip the Terraform file:sudo apt-get install unzip
wget https://releases.hashicorp.com/terraform/1.7.3/terraform_1.7.3_linux_amd64.zip
unzip terraform_1.7.3_linux_amd64.zip
PATH
variable:mv terraform /usr/local/bin/
terraform version
The compile from source option is typically used for operating systems that are not directly supported by Terraform, but where you still want to install it.
The steps to compile the Terraform from source remain the same across the Linux OS. You will have to make sure you use an appropriate package manager to install go
. The following steps are done on Ubuntu:
git clone https://github.com/hashicorp/terraform.git
cd terraform
go
, which is required to compile the binary:sudo snap install go
$GOPATH/bin/terraform
:go install
bin
folder of the go
installation and run the following command:mv terraform /usr/local/bin/
terraform version
You can install Terraform on Windows using the Chocolatey package manager. You do not have to add the location of the Terraform binary in the PATH
variable when installed through Chocolatey. The following steps will help you install Terraform in Windows using Chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
choco version
Figure 0.8: Chocolatey installation validation
choco install -y terraform
The output will look like Figure 0.9 when Terraform is successfully installed:
Figure 0.9: Terraform installation via Chocolatey
terraform version
The following steps will help you install Terraform on Windows using a pre-compiled binary:
https://developer.hashicorp.com/terraform/install
terraform.exe
file.terraform
under C:\Program Files (x86)
and move this terraform.exe
file into that folder (i.e., to C:\Program
Files (x86)\terraform
).PATH
environment variable to make it available for the command line.SystemPropertiesAdvanced
. This should open the System Properties window on the Advanced tab, as shown in Figure 0.10. (Alternatively, you can search for advanced system settings in the search bar.)Figure 0.10: System properties
Figure 0.11: Modifying the PATH environment variable
terraform.exe
file’s location manually as shown in Figure 0.12, or browse to the location of the terraform.exe
file to select it. Once the new location appears on the screen, click OK to save it.Figure 0.12: Adding the Terraform location to the PATH environment variable
terraform version
For Terraform to manage the resources in any platform, it must first authenticate against the platform. The following steps will help you in configuring Terraform to interact with AWS to manage the resources:
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
$HOME/.aws/credentials
)$HOME/.aws/config
)task
role)In the following steps, you will learn how to configure the Terraform to authenticate to AWS via Shared credentials files:
aws configure
Figure 0.13: Configuring the AWS CLI
~/.aws/credentials
file in Linux and macOS.provider.tf
containing the following code:terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "ap-south-1" }
Note
Note that we have not made any reference to the file where the credentials are stored or the profile used by the AWS CLI. If we are using the default options, we do not have to explicitly specify this as Terraform will automatically detect and use them.
When you run terraform init
and terraform plan
with the provider code, it should go through without any issues as shown in Figure 0.14:
terraform init terraform plan
Figure 0.14: Configuring AWS provider
You can test the connectivity to AWS by adding a simple VPC creation code from the public module either in the same provider.tf
file or by creating a new file named vpc.tf
(make sure both files are in the same folder) and then running terraform plan
:
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "5.5.2" }
You will see terraform plan
trying to create four resources (NACL, security group, route table, and VPC). Only the VPC plan is shown in Figure 0.15 for brevity, but note that the plan shows the overall addition of four resources.
Figure 0.15: Terraform plan for VPC creation in AWS
The terraform plan
output confirms that we have successfully configured Terraform to communicate with AWS to manage the resources.
Note
There are multiple editors that can be used for writing Terraform code. The choice of editor is very subjective. However, the most common ones are Visual Studio Code (VSCode), Atom, and PyCharm. Irrespective of the editor you choose, make sure you install the Terraform plugin/extension, which helps a lot when writing Terraform code.
Change the font size
Change margin width
Change background colour