A Beginners Guide to Terraform – Part 1 Setup


As a long time IT professional who previously specialized in Enterprise Storage, I’ve been an Infrastructure person for the better part of my career. However, with the now ubiquitous presence of Cloud Computing, I’ve been challenged to view those past infrastructure skills through a new lens, Infrastructure as Code (IaC).

My goal with this series is to walk through my journey as I acquaint myself with the concepts of IaC and put them into practice with HashiCorp’s Terraform in an Azure environment.

Infrastructure as Code

Traditionally, the deployment of infrastructure in an on premises environment has required physical configuration (racking equipment), cabling, power considerations, as well as the use of any number of deployment tools to perform post installation configuration tasks. This takes a great deal of time and effort, isn’t easily repeatable, and offers limited ability for automation while simultaneously exposing several opportunities for error.

Enter Infrastructure as Code.

IaC is the process of utilizing descriptive languages (JSON, HCL, YAML, etc.) to define and automate the deployment of IT infrastructure and services quickly and consistently. As an added bonus, since infrastructure is now described in definition files, DevOps practices used for software development can be applied. We can now store full environments in source control, version them, subject them to governance and policy, all of which can be included in existing Continuous Integration and Continuous Delivery (CI/CD) pipelines!


So why Terraform?

Conventional wisdom would tend to drive me towards tools that are native to Azure. Azure Resource Manager (ARM) templates are natively used by Azure to describe and automate infrastructure, and hey the service even provides you with ARM templates of any deployed infrastructure that you can use for general learning purposes, templates for reuse, etc.!

But what if your environment doesn’t just live in Azure? What if you have a multi-cloud environment or a hybrid environment with on-premises elements and you would like to use a common infrastructure provisioning tool? Terraform’s provider model allows it to remain agnostic, supporting configurations that can combine this mix of different environments.

Getting Started

This first article will focus on getting your environment setup to work with Terraform. The installation of the binary is as simple as it gets. Visit the Terraform downloads page and select the OS and architecture that you will be working on. The download is a single binary file that can be placed anywhere on your system.

Once you have decided on the location to place the binary, edit your PATH to include the location of the Terraform executable by running the following PowerShell script in Windows. Please note that running this script will require an admin PowerShell session.

#TF Executable location
$TFPath='C:\Program Files\Terraform'

# Get PATH environmental Variable
$Path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

# Create new PATH environmental variable
$NewPath = $Path + ";" +$TFPath

# Set new PATH environmental variable
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $NewPath

# Verify the Path
(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

You can verify the download of the file and path variable have been set correctly by running the following command:

terraform version

Your output should be similar to the following:

Verifying path and executable using terraform version

You are now ready to start working with Terraform!

I currently use VS Code as my preferred platform for authoring configurations. Enabling the HashiCorp Terraform extension in VS Code provides you with some welcome syntax highlighting and other benefits. As you can see below, Terraform is accessible through the PowerShell terminal in VS Code and my extremely basic configuration file is recognized by the Terraform extension and handled accordingly.

Terraform config editing in Visual Studio Code

The steps above are specific to setting up an environment where you are working on a workstation. If you prefer to work out of the Azure Portal, the Azure Cloud Shell has the latest version of Terraform included natively! In addition, the Azure Cloud Shell built-in editor is language aware, and provides similar syntax highlighting to using the Terraform extension in VS Code. 

Terraform in Azure Cloud Shell!

At this point, you should be ready to go whether you prefer to work locally or within the Azure portal. As a starting point, I would highly suggest reading some of the information on the Terraform Providers page (particularly the Azure RM provider) in addition to the Terraform on Azure basics that can be found at the Microsoft Docs sites. My next article will focus on some of the basics of Terraform, the structure of a Terraform config file and finally deployment of a simple starting resource.

Until then!


Leave a Reply