Working with Terraform – Git and Visual Studio Code

I’m becoming a huge fan of Terraform, having started using it at work to manage our AWS environment and using it more recently with Virtual Design Master. When working with Terraform there are a few simple things that can make a big difference, using a good development environment and using a source code repository. These are obvious things for any developer but as an infrastructure guy I’ve grown into using them as I’ve used Terraform. The following is how to get all of this setup on Windows.

Installing Terraform

Installing Terraform is simple – its a self contained binary which needs adding to your PATH variable.

  1. Download Terraform from https://www.terraform.io/downloads.html
  2. Unzip it anywhere, but as we are going to add this to the PATH environment variable, make it somewhere fairly permenant. For me thats c:\users\chris\tools\terraform
  3. Add the location where you unzipped Terraform to your PATH environment variable. To that, hit Start, type environment and select the result shown as ‘Edit the system environment variables.capture1
  4. Click the ‘Environment Variables…’ button to the bottom right of the settings box that appears.
  5. Under System variables select Path and click on Edit
  6. Click New and enter the path to where you unzipped terraform.exe.
  7.  Click OK, then OK, then OK.
  8. Open up command prompt and type terraform. You should see the terraform help appear.capture2

Installing Visual Studio Code with the Terraform Extension

  1. Download Visual Studio Code (VSC) from https://code.visualstudio.com/download
  2. Run the installer, tick to add an option to open folders with VSCcapture3
  3. Installer will complete and VSC can now be opened from the Start Menu.
  4. Open VSC
  5. Click the extension button from the left hand menucapture4
  6. Type terraform in the search bar
  7. Click the green ‘Install’ button next to the extension from Mikael Olenfalkcapture5
  8. That’s it, you now have a development environment which also includes syntax highlighting and automatic formatting in Terraform files.

Setting up Git in VSC

Git integration is  included out of the box with VSC but we do need to install Git for Windows first to install the git.exe executable VSC uses.

  1. Download Git from https://git-scm.com/download/win
  2. Run the Git installer. There are lots of options for the install of Git but the defaults are fine. For VSC, ensure the options ‘Use Git from Windows Command Prompt’ and ‘Enable Git Credential Manager’ are selected so the VSC can find git.exe and secure credential storage.
  3. Setup some basic required user config for git. Open command prompt and run the following commands;
    git config –global user.email “you@example.com”
    git config –global user.name “Your Name”

Once Git is installed and configured, we need to create a folder and initialise it as Git repository.

  1. Create a folder for this Terraform project. Terraform projects (technically know as modules) live within a folder so create something specific for this tutorial. For me this is c:\users\chris\projects\terraform-tutorial.
  2. Open VSC
  3. Click File > Open Folder
  4. Open the folder you created in Step 1
  5. Click View > SCM
  6. At the top of middle narrow pane, click ‘Initialize Repository’.Capture7

Thats Git setup for this project – easy, right? Lets create our first file and commit the changes

  1. Click File > New File
  2. Click File > Save
  3. As the file has not been saved before, the Save As dialog opens. It should be in the same folder as we opened earlier on but double check. Name the file main.tf and click save.Capture6
  4. The Source Control icon on the left hand pane should change to show 1 uncommitted change for the repository.

    Capture8

  5. Click on Source Control icon.
  6. Hover of the main.tf file and click the ‘+’ to stage the file. This means it will be included in the next commit, allowing you to decide which files are committed and which files are not.
  7. Enter a short message to describe the commit and hit the ‘tick’ to perform the commit.

    Capture10

From now on, as you update files in the folder, you should get into the habit of periodically staging and committing your changes.

A good place to start with Terraform is to follow Hashicorp’s Getting Started guide, which can be found here; https://www.terraform.io/intro/getting-started/build.html

That’s the basics of git, terraform and VSC covered!

Advertisements
Working with Terraform – Git and Visual Studio Code

One thought on “Working with Terraform – Git and Visual Studio Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s