Installing the Windows Subsystem for Linux and use Terraform with VS Code

I am not a developer and have been looking for a reason to use WSL for a while and found a good use case to Terraform using VS Code on Linux.

In my opinion Hashicorp’s Terraform is the de facto choice in the infrastructure as code space just like Kubernetes is for container orchestration.  It provides the ability to version your infrastructure and automate the provisioning of your resources across different cloud vendors as well as on-premise.

To get this working requires a couple of steps which I will provide here. Also at the time of writing this I am running Windows 10 Pro, with Version 10.0.18362 Build 18362.

Install WSL:

  1. Open Powershell as Administrator and run the following command to enable this feature
    1. “Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux”
  2. Open MS store and download your favorite distribution,  I selected Ubuntu.
    1. Don’t close the store just yet and wait for the installation to complete.
      1. You can also open from command prompt by typing “Ubuntu.exe” from the distro installation folder, or selecting ubuntu from app menu.
  3. Create a UNIX username
  4. Create a UNIX password
  5. Now lets update our distro to latest
    1. Run “sudo apt-get update”
    2. Run “sudo apt-get upgrade”
  6. Done

Install Terraform on linux distro:

  1. Run the following commands to install unzip
    1. “sudo apt-get install unzip”
  2. Copy the link address to latest Linux 64-bit download from this page here
  3. Run the following command to install Terraform
    1. “wget https://releases.hashicorp.com/terraform/0.12.7/terraform_0.12.7_linux_amd64.zip”
    2. “unzip terraform_0.12.7_linux_amd64.zip”
    3. “sudo mv terraform /usr/local/bin”
  4. Run the following command to verify its has been implemented successfully
    1. “terraform version”
    2. Should show “Terraform v0.12.7” (based on the version I downloaded)

Install the Azure and AWS CLI on the linux distro

This is not necessary but super useful if you have deploying to these cloud vendors.

  1. Azure CLI installation steps
    1. Run the following command to verify its working
      1. “az -v”
  2. AWS CLI installation steps
    1. Run the following command to install
      1. “sudo apt-get install awscli”
    2. Run the following command to verify its working
      1. “aws version”

Install Visual Studio Code:

  1. Download here
  2. Install
  3. Install the Visual Studio Code Remote Development Extension Pack, which will allow you to open any folder in a container, remote machine or WSL!
    1. Download and install from here
    2. Install Windows OpenSSH client with PowerShell
      1. Get-WindowsCapability -Online | ? Name -like ‘OpenSSH*’
      2. Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
  4. There are also a lots of other useful extensions available to have a look at
    1. Mikael Olenfalk’s Terraform extension available here
    2. If you are running Terraform => 0.12 then on the command Palette (Ctrl+Shift+P) run the command “Terraform: Enable/Disable Language Server”. This will enable the new HCL language server.

Optionally, install GitBash if using Windows directory for repo:

  1. Download and install GitBash for Windows here
    1. For Default Editor option I selected Visual Studio Code as my default editor.
    2. For Path environment I selected Git from command line and also 3rd-party software
    3. For SSH executables I selected to use OpenSSH
    4. For line ending conversions I selected “Checkout Windows-style, commit Unix-style line ending
    5. For terminal emulator I selected “Use Windows’ default console window”
  2. To verify if it installed successfully, open Windows command prompt or powershell and enter the following command
    1. “git –version”

Now lets create a Git repository and open in VS code:

Now you have two options, either create your git repository on a Windows or Linux folder.  I will provide steps for both using Gitlab.

Windows:

  1. Create a folder on your Windows 10 OS
  2. Open VS Code
  3. Select File -> Open Folder
    1. Browse to newly create folder
  4. Select Terminal -> New Terminal
  5. Make sure Gitbash is installed
  6. Set Git configuration in terminal
    1. Set username and email that will be attached to your commits and tags. This should also be the same as your GitLab account!
      1. git config –global user.name “your name”
      2. git config –global user.email “email address”
    2. Make sure you settings are good by typing:
      1. git config –global –list
  7. From the terminal you can now get your git on!
    1. At the project root level make sure to exclude the commit of sensitive Terraform data
      1. Create the .gitignore file
        1. touch .gitignore (case sensitive)
      2. Edit the .gitignore file
        1. Here is a good content example.
    2. Initialize git
      1. git init
    3. Add changes from all tracked and untracked files
      1. git add -A
      2. (Another option is to exclusively add your Terraform files)
    4. Add a new remote with project URL from GitLab
      1. git remote add origin [GitLab project URL]
    5. Commit changes
      1. git commit -m “Just getting started”
    6. Now we push the project to GitLab
      1. git push origin master
      2. Enter username/password

Linux

  1. Open VS Code
  2. Select Terminal -> New Terminal
  3. Type “bash” to change from the default Powershell terminal
    1. You will now be in your Linux sub-system, pointing to your mounted windows folder.
  4. In the terminal create a Linux folder where you want your git repo to reside
  5. Change directory to the newly created folder
  6.  Type “code .” in the terminal, which will fetch components needed to run in WSL
    1. You only need to do this once in this folder!
  7. Now voilà a new VS Code window will appear, with your folder already added. So cool!
  8. New the new VS Code window
    1. select Terminal -> New Terminal
  9. Set Git configuration in terminal
    1. Set username and email that will be attached to your commits and tags. This should also be the same as your GitLab account!
      1. git config –global user.name “your name”
      2. git config –global user.email “email address”
    2. Make sure you settings are good by typing:
      1. git config –global –list
  10. From the terminal you can now get your git on!
    1. At the project root level make sure to exclude the commit of sensitive Terraform data
      1. Create the .gitignore file
        1. touch .gitignore (case sensitive)
      2. Edit the .gitignore file
        1. Here is a good content example.
    2. Initialize git
      1. git init
    3. Add changes from all tracked and untracked files
      1. git add -A
      2. (Another option is to exclusively add your Terraform files)
    4. Add a new remote with project URL from GitLab
      1. git remote add origin [GitLab project URL]
    5. Commit changes
      1. git commit -m “Just getting started”
    6. Now we push the project to GitLab
      1. git push origin master
      2. Enter username/password

Links / References:

https://docs.microsoft.com/en-us/windows/wsl/install-win10#install-the-windows-subsystem-for-linux

https://docs.gitlab.com/ee/gitlab-basics/start-using-git.html

https://code.visualstudio.com/docs/remote/wsl

 

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 )

Facebook photo

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

Connecting to %s