Install and Configure Terraform on Windows

white and red car on green grass field

Terraform is quickly becoming a favorite infrastructure as code software tool. The Terraform binary provides a CLI workflow for deploying and managing several services such as AWS, Azure, GCP, and more. If you want to try out Terraform, follow this guide to learn how to install and configure Terraform on your Windows systems.

To follow along with this tutorial, you will need:

  • A computer with a Windows operating system installed. This tutorial uses Windows 10 version 20H2 (OS build 19042.1348). However, these steps should be pretty universal to any version of windows.

Step 1. Download Terraform Executable

To get started on the install of Terraform on Windows, you first need the Terraform binary downloaded to your local system. Visit the Download Terraform page to find your version of Terraform for Windows. Hashicorp has both 32 and 64-bit versions. Download and save the zip file to your local system.

Step 2. Extract Terraform Executable

The download zip file contains the Terraform executable (terraform.exe). The executable is responsible for validating configuration files, deploying infrastructure, and downloading required modules. Extract the terraform.exe file to a directory of your choosing.

Choose a directory that works for you. This directory may be in a folder directly off the root of your C: drive, like C:\terraform. Another option is to save in the root of your user profile found in C:\Users\<user name>. What’s important is finding a place you can remember where you saved the Terraform executable.

Step 3. Update the Global PATH Variable

PATH is an environment variable found in both Windows and Linux operating systems that contain programs and commands. The PATH variable allows running commands like ipconfig or ping no matter what directory you are located. The PATH variable contains the directory where these programs are located, and the operating system executes the programs from those paths.

Terraform works the same way. You add the path from Step 2 where you extracted the Terraform executable to the PATH variable. This action allows Windows to execute Terraform commands from any directory from a command line console.

To add the Terraform executable directory to your PATH variable:

  1. Click on the Start menu and search for Settings. Open the Settings app.
  2. Select the System icon, then on the left menu, select the About tab. Under Related settings on the right, select Advanced system settings.
  1. On the System Properties window, select Environment Variables.
  2. Select the PATH variable, then click Edit.
  3. Click the New button, then type in the path from Step 2 where the Terraform executable is located. In the screenshot below, this is C:\terraform.
  1. Click OK three times to exit and save the settings.

Step 4. Test the Configuration

Open a new shell or command line program (Bash, PowerShell, or Command Prompt in Windows). Run the following Terraform command to verify the PATH configuration. The command should return the current version of the download Terraform executable.

terraform -version
install terraform windows
Verify Terraform install on Windows

Configure Environment Using PowerShell

Instead of updating the PATH variable using the Settings app, you can also configure it using PowerShell. Here is a short script showing how to do so. You’ll need to change the value of $installPath to match where you extracted the Terraform executable back in Step 2. In the example below, the path is C:\terraform.

# Set $installPath to location of Terraform executable
$installPath = "C:\terraform"

# Save current value of PATH
$currentPath = (Get-Item -path "HKCU:\Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')

# Add $installPath to $currentPath, saved as $newPath
$newPath = $currentPath + ";$installPath"

# Set PATH environment variable to $newPath value
setx PATH ($newPath)

Update Terraform Version

To update the Terraform executable version, download the latest version from Hashicorp’s website. You then extract the executable to the same location as the current version to replace it.

Want to learn more about Terraform? Check out my other articles here!

Start a discussion!

This Post Has 3 Comments