Hello!

This is the first article in a series of articles about terraform. In it, we will learn how to install terraform using tfenv and how to configure vscode to work with terraform.

tfenv

You will need tfenv to get started. It makes it very easy to install and manage different versions of terraform. The tfenv code is located at github.

To install, you need to clone git repository

git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv

Now if I call the tfenv command I will get an error. To make tfenv work, you need to add tfenv to the PATH or execute the command ln -s ~/.tfenv/bin/* /usr/local/bin. And now if I call tfenv I will see the result

$ tfenv
tfenv 2.2.2-158-gc05c364
Usage: tfenv <command> [<options>]

Commands:
   install       Install a specific version of Terraform
   use           Switch a version to use
   uninstall     Uninstall a specific version of Terraform
   list          List all installed versions
   list-remote   List all installable versions
   version-name  Print current version
   init          Update environment to use tfenv correctly.
   pin           Write the current active version to ./.terraform-version

Finally tfenv is ready to use. In order to install terraform, you can call the tfenv install command, which downloads the latest available version of terraform. To install a specific version, you need to execute the command tfenv install 0.13.2, and terraform version 0.13.2 will be installed.

To see the list of versions available for installation, you need to execute the tfenv list-remote command

$ tfenv list-remote
1.3.0-alpha20220706
1.3.0-alpha20220622
1.3.0-alpha20220608
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc2
1.2.0-rc1
1.2.0-beta1
1.2.0-alpha20220413
1.2.0-alpha
1.1.9
1.1.8
...

You can also check the versions that are already installed using tfenv list

$ tfenv list
  1.2.4
  1.1.9
  1.1.6
  1.0.11
  1.0.5
  1.0.1
  1.0.0
  0.14.11
  0.14.10784023458628

And to specify which version of terraform to use, you need to execute the command tfenv use VERSION.

$ tfenv use 0.14.6
Switching default version to v0.14.6
Switching completed
$ terraform version
Terraform v0.14.6
Your version of Terraform is out of date! The latest version
is 1.2.5. You can update by downloading from https://www.terraform.io/downloads.html

Make sure terraform version is already installed

In addition, tfenv supports the .terraform-version file. In this file, you can specify the version of terraform and tfenv will automatically download and start using the correct version of terraform. For example, I will create a file .terraform-version with the content 1.1.1 and in the same directory I execute the command tfenv use

$ tfenv use
No installed versions of terraform matched '1.1.1:^1.1.1$'. Trying to install a matching version since TFENV_AUTO_INSTALL=true
Installing Terraform v1.1.1
Downloading release tarball from https://releases.hashicorp.com/terraform/1.1.1/terraform_1.1.1_linux_amd64.zip
################################################################################################################################################ 100,0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.1.1/terraform_1.1.1_SHA256SUMS
Not instructed to use Local PGP (/home/maksym/.tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
Archive:  /tmp/tfenv_download.xtUx15/terraform_1.1.1_linux_amd64.zip
  inflating: /home/maksym/.tfenv/versions/1.1.1/terraform
Installation of terraform v1.1.1 successful. To make this your default version, run 'tfenv use 1.1.1'
Switching default version to v1.1.1
Default version file overridden by /tmp/.terraform-version, changing the default version has no effect
Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: 1.1.1

$ Terraform v1.1.1
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.2.5. You can update by downloading from https://www.terraform.io/downloads.html

Although it is not necessary to call tfenv use. I will change the version of terraform in .terraform-version to 1.1.2 and execute terraform. And version will be automatically installed and selected for use

$ terraform version
version '1.1.2' is not installed (set by /tmp/.terraform-version). Installing now as TFENV_AUTO_INSTALL==true
Installing Terraform v1.1.2
Downloading release tarball from https://releases.hashicorp.com/terraform/1.1.2/terraform_1.1.2_linux_amd64.zip
################################################################################################################################################ 100,0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.1.2/terraform_1.1.2_SHA256SUMS
Not instructed to use Local PGP (/home/maksym/.tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
Archive:  /tmp/tfenv_download.6MKarM/terraform_1.1.2_linux_amd64.zip
  inflating: /home/maksym/.tfenv/versions/1.1.2/terraform
Installation of terraform v1.1.2 successful. To make this your default version, run 'tfenv use 1.1.2'
Terraform v1.1.2
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.2.5. You can update by downloading from https://www.terraform.io/downloads.html

With .terraform-version it is very easy to control which version of terraform is used in different projects.

VSCode

To make it easier to write terraform code, I use the Visual Studio Code. After the vscode is installed, you need to install HashiCorp Terraform extension for it. To install the extension, you need to press Ctrl+Shift+X or click on extension. Enter terraform in the search, select HashiCorp Terraform and click Install

tf

terraform has a command called terraform fmt and it formats terraform code. This process can be automated using vscode. To do this, you need to press Ctrl+Shift+P and in the panel that appears, type >Open Settings (Json) and select the corresponding item in the menu and add editor.formatOnSave: true in the right window that opens

settings

Every time a file is saved, terraform fmt will automatically be called for that file.

Video