How to Install Terraform on Windows

Terraform, a widely embraced tool in the Linux community, has seamlessly extended its reach to Windows. To ensure a smooth installation process, make sure you have PowerShell version 4 or later installed. While it’s explicitly recommended for Windows 10/11 Desktop, Windows Server 2012, and Windows Server 2016, it may also be compatible with other Windows versions, though untested.

The Easy Way To Install Terraform on Windows

The easiest way to install Terraform on Windows is to use the Chocolatey package manager.

Step 1 – Enable Chocolatey on your Windows Desktop

To use Chocolatey, you need to have it installed on your desktop

First, check if you have it installed. Open PowerShell and type:

PowerShell
choco 

You should see output like this:

PowerShell
PS C:\Windows\system32> choco
Chocolatey v2.2.2
Please run 'choco -?' or 'choco <command> -?' for help menu.

If you get no response or an error, you need to install Chocolatey.

PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

If Chocolatey is already installed, you will get this message:

PowerShell
WARNING: 'choco' was found at 'C:\ProgramData\chocolatey\bin\choco.exe'.
WARNING: An existing Chocolatey installation was detected. Installation will not continue. This script will not
overwrite existing installations.
If there is no Chocolatey installation at 'C:\ProgramData\chocolatey', delete the folder and attempt the installation
again.

Please use choco upgrade chocolatey to handle upgrades of Chocolatey itself.
If the existing installation is not functional or a prior installation did not complete, follow these steps:
 - Backup the files at the path listed above so you can restore your previous installation if needed.
 - Remove the existing installation manually.
 - Rerun this installation script.
 - Reinstall any packages previously installed, if needed (refer to the lib folder in the backup).

Once installation is completed, the backup folder is no longer needed and can be deleted.

Step 2 – Install Terraform Via Chocolatey

PowerShell
choco install terraform

When Prompted – type A for ALL

Upon completion, you should see the following output:

PowerShell
PS C:\Windows\system32> choco install terraform
Chocolatey v2.2.2
Installing the following packages:
terraform
By installing, you accept licenses for the packages.
Progress: Downloading terraform 1.7.0... 100%

terraform v1.7.0 [Approved]
terraform package files install completed. Performing other installation steps.
The package terraform wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): a

Removing old terraform plugins
Downloading terraform 64 bit
  from 'https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_windows_amd64.zip'
Progress: 100% - Completed download of C:\Users\richi\AppData\Local\Temp\chocolatey\terraform\1.7.0\terraform_1.7.0_windows_amd64.zip (25.05 MB).
Download of terraform_1.7.0_windows_amd64.zip (25.05 MB) completed.
Hashes match.
Extracting C:\Users\richi\AppData\Local\Temp\chocolatey\terraform\1.7.0\terraform_1.7.0_windows_amd64.zip to C:\ProgramData\chocolatey\lib\terraform\tools...
C:\ProgramData\chocolatey\lib\terraform\tools
 ShimGen has successfully created a shim for terraform.exe
 The install of terraform was successful.
  Software installed to 'C:\ProgramData\chocolatey\lib\terraform\tools'

Chocolatey installed 1/1 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
PS C:\Windows\system32>

Step 3 – Run Terraform on Windows

Now, simply type Terraform in your PowerShell window, and it should all be good to go.

PowerShell
PS C:\Windows\system32> terraform
Usage: terraform [global options] <subcommand> [args]

The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.

Main commands:
  init          Prepare your working directory for other commands
  validate      Check whether the configuration is valid
  plan          Show changes required by the current configuration
  apply         Create or update infrastructure
  destroy       Destroy previously-created infrastructure

All other commands:
  console       Try Terraform expressions at an interactive command prompt
  fmt           Reformat your configuration in the standard style
  force-unlock  Release a stuck lock on the current workspace
  get           Install or upgrade remote Terraform modules
  graph         Generate a Graphviz graph of the steps in an operation
  import        Associate existing infrastructure with a Terraform resource
  login         Obtain and save credentials for a remote host
  logout        Remove locally-stored credentials for a remote host
  metadata      Metadata related commands
  output        Show output values from your root module
  providers     Show the providers required for this configuration
  refresh       Update the state to match remote systems
  show          Show the current state or a saved plan
  state         Advanced state management
  taint         Mark a resource instance as not fully functional
  test          Execute integration tests for Terraform modules
  untaint       Remove the 'tainted' state from a resource instance
  version       Show the current Terraform version
  workspace     Workspace management

Global options (use these before the subcommand, if any):
  -chdir=DIR    Switch to a different working directory before executing the
                given subcommand.
  -help         Show this help output, or the help for a specified subcommand.
  -version      An alias for the "version" subcommand.
PS C:\Windows\system32>

Step 4 – Basic Terraform Commands To Get Started

Core Workflow Commands:

  1. terraform init: Initializes your Terraform working directory. This downloads provider plugins, sets up the backend, and prepares your project for use.
  2. terraform validate: Checks the syntax and structure of your configuration files (*.tf) for errors. Always run this before applying changes.
  3. terraform plan: Creates an execution plan, showing you what changes Terraform would make to your infrastructure. This is a dry run; no actual changes are applied.
  4. terraform apply: Applies the changes outlined in the plan. This is where Terraform provisions or modifies your resources.
  5. terraform destroy: Destroys the infrastructure managed by Terraform. Use this with caution as it permanently deletes resources.

Additional Useful Commands:

  • terraform fmt: Formats your configuration files to a standardized style, improving readability.
  • terraform show: Displays the current state of your managed infrastructure.
  • terraform output: Shows the outputs defined in your configuration (useful for retrieving values like IP addresses).
  • terraform taint: Marks a resource for recreation on the next apply.
  • terraform state: Allows you to inspect and manipulate the Terraform state file, which tracks the current status of your resources.

Working with Providers:

  • terraform providers: Lists the providers used in your configuration.
  • terraform providers schema -json: Gets the schema of a provider in JSON format (useful for understanding available resources and attributes).
Elsewhere On TurboGeek:  Arcserve Backup & Disaster Recovery: Bare Metal Restore Made Easy

Example Workflow:

  1. Create a directory for your Terraform project.
  2. Create main.tf and define your infrastructure (e.g., an AWS EC2 instance).
  3. Run terraform init.
  4. Run terraform validate to check for errors.
  5. Run terraform plan to preview changes.
  6. If the plan looks good, run terraform apply to create the resources.
  7. When you’re done, run terraform destroy to clean up (if you’re in a testing environment).

Installing Terraform on Windows Q&A

Q: What is Terraform, and why should I use it?

A: Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp. It allows you to define and manage your infrastructure resources (servers, databases, networks, etc.) in a declarative way, using a simple configuration language. This makes it easier to version, automate, and collaborate on infrastructure changes.

Q: What are the system requirements for Terraform on Windows?

A: Terraform runs on Windows 7 or later. It requires no special software or libraries, just a command-line environment (PowerShell or Command Prompt).

Q: Where can I download Terraform for Windows?

A: You can download the latest version of Terraform for Windows from the official HashiCorp website: https://www.terraform.io/downloads.html

Q: How do I install Terraform on Windows?

  1. Download the Terraform zip archive for Windows.
  2. Extract the contents of the zip file to a directory of your choice (e.g., C:\Program Files\terraform).
  3. Add the directory containing the terraform.exe file to your system’s PATH environment variable. This allows you to run Terraform from any location in your command line.

Q: How do I verify that Terraform is installed correctly?

A: Open a new command prompt or PowerShell window and type terraform -version. You should see the installed version of Terraform printed out.

Q: How do I update Terraform on Windows?

A: To update Terraform, simply download the latest version and replace the existing terraform.exe file in your installation directory. Make sure your PATH variable still points to the correct location.

Q: Where can I find more information and help on Terraform?

A: The official Terraform documentation is an excellent resource: https://www.terraform.io/docs You can also find a wealth of information, tutorials, and examples online, as well as community forums for getting help.

Q: What are some common issues people face when installing Terraform on Windows?

A:

Incorrect PATH: Make sure the directory containing terraform.exe is in your PATH.
Execution Policy: In PowerShell, you might need to adjust your execution policy to allow running Terraform.
Antivirus: Some antivirus software might flag or block Terraform. You might need to add an exception for it.

If you encounter any problems, refer to the troubleshooting section in the Terraform documentation or seek help online.

I hope this Q&A helps you with installing and getting started with Terraform on Windows!

You may also like...

3 Responses

  1. 19/10/2022

    […] How to Install Terraform on Windows […]

  2. 02/11/2022

    […] Learn how to install Terraform on Windows here. […]

  3. 28/06/2024

    […] Terraform also works on Windows 10 Desktop and Windows Server products. Click here for a guide to install on Windows. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป