Getting started with DevOps is a great way to introduce yourself to the more modern way of controlling infrastructure as code with tools that treat building infrastructure by its definition in infrastructure code. In this way, you can provision infrastructure in a declarative way to define what the infrastructure should look like, and then the tools make the infrastructure “look” like the code. Terraform is one such tool that allows DevOps engineers to build out complex infrastructure as code.
Table of contents
- Introduction to Terraform and Its Role in DevOps
- Preparing to Install Terraform on Windows
- Install Terraform on Linux Systems
- Verifying Your Terraform Installation
- The shift to Immutable Infrastructure
- Terraform and Cloud Providers
- Advanced Terraform Features for DevOps
- Wrapping up and Next Steps in Your Terraform and DevOps Journey
Introduction to Terraform and Its Role in DevOps
Terraform, a tool by HashiCorp, allows developers and operations teams to define, provision, and manage various cloud services and on-premises infrastructure like VMware vSphere, using simple, declarative syntax. To begin, we need to look at the process of installing Terraform. Terraform is support for installation in many operating systems, including mac OS. We will consider how to do this in both Windows and Linux.
Preparing to Install Terraform on Windows
First, let’s understand how we install Terraform on Windows. You can use the GUI, or you can use the lesser-known winget command. I prefer the winget approach as it is less cumbersome and automatically pulls the binary from the package repository, but let’s look at both.
Understanding the Terraform Executable File for Windows
Before you install Terraform on a Windows machine, it’s crucial to comprehend the nature of the Terraform executable file. Terraform is distributed as a single binary, so you don’t need a complex installation process. You simply need to download the correct version of the Terraform binary for your system.
Step-by-Step Guide to Installing Terraform on Windows
To install Terraform on Windows, follow these steps:
- Download the appropriate Terraform zip file from HashiCorp’s official repository.
- Extract the Terraform executable file from the downloaded zip file.
- Place the extracted file in a suitable directory, like C:\Terraform.
- Add the Terraform directory to your system environment variables to make Terraform commands accessible from the command prompt.
To download the appropriate Terraform ZIP file from here: HashiCorp Developer. Choose the appropriate architecture for your Windows machine. Hashicorp distributes Terraform repository downloads in the ZIP format. The download defaults to the latest Terraform version.
Once you have the ZIP file, extract the ZIP file to reveal the EXE file.
You will then need to copy this to the directory in which you want to house Terraform. This is important as we will want to add a Path variable to reference the Terraform location so we don’t have to change the directory each time to run Terraform.
Launch the sysdm.cpl tool and navigate to Advanced > Environment variables > System variables > PATH
Once you have the PATH variable updated, you will need to relaunch your PowerShell or command prompt so the new variable takes effect. Then, you should be able to check to see if the terraform command is found:
Install Terraform on Windows using Winget
Many do not know, if you are using Windows installing Terraform can be accomplished using a very “linux like” experience installing Terraform on Windows using the winget command. Winget is a fantastic command line tool to install and update software in Windows 11, and it is native to the operating system.
Using winget it automatically installs the terraform package, using the extracted terraform file, and sets up the tool accordingly. One thing about this approach as well is you don’t have to install the environment variable to allow the terraform executable to be referenced properly.
You can see the terraform packages available for installation with winget using the command:
winget search terraform
To install Terraform using winget, we can use the below command:
winget install Hashicorp.Terraform
After installation, you can check the version of Terraform and that it has been installed correctly, using the command:
Install Terraform on Linux Systems
Installing Terraform on a Linux system requires a different approach to the installation process, but it is one that most are familiar with if you have worked with Linux from the command line.
Terraform Installation on Debian and Ubuntu
- Add the official HashiCorp Linux repository to your package repositories using sudo apt-add-repository.
- Update your package list with sudo apt-get update.
- Install Terraform using sudo apt install terraform.
The above steps for Debian and Ubuntu can be accomplished using the following:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
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
Pulling down the GPG key.
Adding the repository via the GPG key.
Below is running the command to install Terraform in Ubuntu Linux.
Installing Terraform on Red Hat, Fedora, and CentOS
For RPM-based Linux distributions, such as Red Hat, Fedora, and CentOS, you can use dnf or yum package managers. The process involves adding the HashiCorp repository and then installing Terraform.
For CentOS and RHEL:
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
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf -y install terraform
Verifying Your Terraform Installation
After installation, like Windows, you can run terraform –version to see the installed version. This step ensures that the Terraform executable is correctly placed and that your path environment variable is properly set.
The shift to Immutable Infrastructure
Terraform allows you to make a shift towards immutable infrastructure. What does this mean? It is where changes are made by replacing entire configurations rather than modifying or upgrading existing infrastructure, as we have done with legacy systems in years past.
Terraform and Cloud Providers
Terraform supports most of the major cloud providers like AWS, Azure Cloud, and Oracle Cloud. This allows for integration and management of cloud infrastructure across different platforms. So, no matter which cloud or multi-clouds are used, Terraform can be at the center of your DevOps strategy.
Advanced Terraform Features for DevOps
Terraform also provides many capabilities and features for advanced DevOps practices.
Utilizing Terraform for Complex Infrastructure Management
It uses a declarative syntax and pluggable modules. It allows the management of complex, multi-tier infrastructure in code.
Terraform in automation and orchestration
With Terraform, you can define infrastructure in machine-readable definition files, enabling automation of deployment and scaling processes. This helps to reduce manual tasks and processes and the potential for human error. It also opens up the possibility of using Terraform and infrastructure as code in Gitops processes using a CI/CD pipeline.
Wrapping up and Next Steps in Your Terraform and DevOps Journey
If you are starting your DevOps journey, installing Terraform on Windows or Linux systems, is a crucial first step in starting to learn about infrastructure as code. In the parts of this series to follow, we will delve deeper into using Terraform. We will look at provisioners, and some beginner projects to get started. After that, we will look at advanced infrastructure management and best practices in Terraform configuration and deployment.