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 is simple – its a self contained binary which needs adding to your PATH variable.
- Download Terraform from https://www.terraform.io/downloads.html
- 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
- 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.
- Click the ‘Environment Variables…’ button to the bottom right of the settings box that appears.
- Under System variables select Path and click on Edit
- Click New and enter the path to where you unzipped terraform.exe.
- Click OK, then OK, then OK.
- Open up command prompt and type terraform. You should see the terraform help appear.
Installing Visual Studio Code with the Terraform Extension
- Download Visual Studio Code (VSC) from https://code.visualstudio.com/download
- Run the installer, tick to add an option to open folders with VSC
- Installer will complete and VSC can now be opened from the Start Menu.
- Open VSC
- Click the extension button from the left hand menu
- Type terraform in the search bar
- Click the green ‘Install’ button next to the extension from Mikael Olenfalk
- 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.
- Download Git from https://git-scm.com/download/win
- 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.
- Setup some basic required user config for git. Open command prompt and run the following commands;
git config –global user.email “firstname.lastname@example.org”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.
- 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.
- Open VSC
- Click File > Open Folder
- Open the folder you created in Step 1
- Click View > SCM
- At the top of middle narrow pane, click ‘Initialize Repository’.
Thats Git setup for this project – easy, right? Lets create our first file and commit the changes
- Click File > New File
- Click File > Save
- 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.
- The Source Control icon on the left hand pane should change to show 1 uncommitted change for the repository.
- Click on Source Control icon.
- 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.
- Enter a short message to describe the commit and hit the ‘tick’ to perform the commit.
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!