Configuring Terraform on Windows 10 Linux Sub-System

This post has been republished via RSS; it originally appeared at: Azure Developer Community Blog articles.

Want to Terraform using Visual Studio Code on Linux but have a Windows 10 machine?   Follow these easy steps to quickly get your environment up in running.

 

Step 1: Install Visual Studio Code - https://code.visualstudio.com/download

 

Step 2: Install Windows Subsystem on your Windows 10 Machine.  I tend to lean towards Ubuntu, however there are several distros to choose from.   This article will walk  you through installing the Windows Subsystem for Linux - https://docs.microsoft.com/en-us/windows/wsl/install-win10#install-the-windows-subsystem-for-linux

 

Once the subsystem is installed please ensure its updated. 

  1. Click Start
  2. Search for Ubuntu (or your installed distro) and open
  3. Run the following commands to make sure Ubuntu is updated:
sudo apt-get update 
sudo apt-get upgrade 

 

Step 3: Install Visual Studio Code Terraform Extension which will allow you to have intelli-sense and support for the different Terraform file types..

  1. Open this link.
  2. Click Install
  3. Reload VScode window

Step 4: Will walk you through how to download, extract and install Terraform on the Linux Sub-System using the terminal.

  1. Open Ubuntu application (or your installed distro)
  2. Run the following command which will install unzip (its my preference to extract files)
    sudo apt-get install unzip
  3. Navigate to the Terraform download page and grab the most recent download URL.
  4. Using the above URL, run the following commands which will download, unzip, move the binary to users bin.
    wget <terraform_url> -O terraform.zip;
    unzip terraform.zip;
    sudo mv terraform /usr/local/bin;
    rm terraform.zip;
    example:
    wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip -O terraform.zip; 
    unzip terraform.zip; 
    sudo mv terraform /usr/local/bin; 
    rm terraform.zip;
  5. Run the following command to verify terraform is installed.
    terraform -v


Step 5:  Terraform can authenticate a few different ways.  For this example we will use Azure CLI, which needs to be installed on the linux sub-system.  Steps to install on several different distros can be found here.  If you installed Ubuntu in Step 2 then you can following these steps.

 

Verify that Azure CLI was installed correctly by running the following:

az -v

Step 6:  Log in to Azure CLI for your target environment by running the below command.  If successful it should launch a browser window.

az login

Enter your credentials in the browser window and walk through the MFA process (if enabled).  Once

you sign in your browser window should look like...

 

2019-04-05_10h05_19.png

 

The Azure CLI window should now say your logged in and will automatically list all subscriptions you have access.

 

Run the following command to show all subscriptions.

az account list -o table

Locate the subscription that you want to target and copy the subscription guid to your clipboard. Run the following command to set the subscription as your active account.

az account set -s <subscription_guid>; az account show -o table

 

Now that we have completed the below steps we are ready to move onto working with Terraform in Visual Studio Code.

 

Working with Terraform...  In this example we will deploy a simple Resource Group from Visual Studio Code leveraging Terraform and Azure CLI on the Linux Sub-system.

 

  1. Launch Visual Studio Code.
  2. Go to the Terminal menu and select New Terminal which will launch a terminal at the bottom. By default, the terminal is set to powershell, type bash to switch the terminal to bash shell.  You can make this permanent by modify a few settings in Visual Studio Code.
    *Take note that your terminal path should now show the path from the Linux sub-system to your folder or current directory on your windows machine.  Just because your using bash doesn't mean your code needs to reside in the Linux sub-system, although it can.  It is my preference to have my code in a windows directory tied to a Git repo.
    2019-04-05_10h34_15.png

     

  3. Create a folder on your computer called Terraform Example (note the path).
  4. From Visual Studio Code, open the folder from File > Open Folder.   You will need to reopen the terminal window and switch to bash (step 2 from above).
  5. Create a file inside of the folder with a name of resource_group.tf
  6. Paste the following TCL code in the resource_group.tf
    resource "azurerm_resource_group" "rg" {
      name     = "rg_test"
      location = "West US"
    
      tags = {
        environment = "Terraform Test RG"
      }
    }
    
    Your vs-code window should look like this:
    2019-04-05_10h47_47.png

     

  7. Run a terraform init from the terminal which will prepare Terraform in this directory.
    2019-04-05_10h52_44.png

    Based on the code Terraform knows that we need the azurerm provider and automatically downloads it.

  8. Lets run our first plan to see what changes Terraform has pending.
    terraform plan
    
    2019-04-05_11h16_45.png
    As we can see the only pending change is the resource group called rg_test.
  9. Now that we have evaluated the plan we can run a apply.
    terraform apply
  10. When prompted type yes to apply the the changes shown.
    2019-04-05_11h36_13.png

     

  11. Run this command to show the resource group which was just created or open the portal to validate.
    az group list --query "[?name=='rg_test']"
    2019-04-05_11h48_04.png

     

 

 

 

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.