Visual Studio Terraform



Visual Studio 2017/2019 utilizes a self signed certificate which is stored in the windows certificate store. Specifically, it uses a dotnet global tool built into.NET Core 2.1 to help with certs.

The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server.

Features

  • Manages installation and updates of the Terraform Language Server (terraform-ls), exposing its features:
    • Completion of initialized providers: resource names, data source names, attribute names
    • Diagnostics to indicate HCL errors as you type
    • Initialize the configuration using 'Terraform: init' from the command palette
    • Run terraform plan and terraform apply from the command palette
    • Validation diagnostics using 'Terraform: validate' from the command palette or a validateOnSave setting
  • Includes syntax highlighting for .tf and .tfvars files -- including all syntax changes new to Terraform 0.12
  • Closes braces and quotes
  • Includes for_each and variable syntax shortcuts (fore, vare, varm)
  1. Jan 05, 2017 Reading this blog post by Shawn Melton Introduction of Visual Studio Code for DBAs reminded me that whilst I use Visual Studio Code (which I shall refer to as Code from here on) for writing PowerShell and Markdown and love how easily it interacts with Github I hadn't tried T-SQL.
  2. Launch Visual Studio Code. From the menu bar, select File Open Folder and navigate to and select the folder you created in the previous step. Initialize Terraform. Before you can begin using the Terraform commands from within Visual Studio Code, you download the plug-ins for two Azure providers: random and azurerm. In the Terminal pane of the.

Getting Started

IMPORTANT: After installing, you must perform a terraform init to provide terraform-ls with an up-to-date provider schemas. The language server will not work correctly without first completing this step!

  1. Install the extension from the Marketplace
  2. Reload VS Code after the installation (click the reload button next to the extension)
  3. Perform a terraform init to provide terraform-ls with an up-to-date provider schema
  4. Open your desired workspace and/or the root folder containing your Terraform files. Note: see Known Issues below about multi-folder workspaces
  5. Depending on your settings in VS Code, completion will start automatically (if not inside quotes/string literal, on certain trigger characters), or you can explicitly trigger completion via keyboard combination (Ctrl+Space on Windows, control+space on Mac).

Configuration

This extension offers several configuration options. To modify these, navigate to the extension view within VS Code, select the settings cog and choose Extension settings, or alternatively, modify the .vscode/settings.json file in the root of your working directory.

Telemetry

We use telemetry to send error reports to our team, so we can respond more effectively. If you want to disable this setting, add 'telemetry.enableTelemetry': false to your settings.json and that will turn off all telemetry in VSCode. You can also monitor what's being sent in your logs.

Multiple Workspaces

If you have multiple root modules in your workspace, you can configure the language server settings to identify them. Edit this through the VSCode Settings UI or add a .vscode/settings.json file using the following template:

If you want to automatically search root modules in your workspace and exclude some folders, you can configure the language server settings to identify them.

Formatting

To enable formatting, it is recommended that the following be added to the extension settings for the Terraform extension:

Validation

An experimental validate-on-save option can be enabled with the following setting:

This will create diagnostics for any elements that fail validation. terraform validate can also be run using the setting in the command palette.

Release History

v2.0.0 is the first official release from HashiCorp, prior releases were by Mikael Olenfalk.

The 2.0.0 release integrates a new Language Server package from HashiCorp. The extension will install and upgrade terraform-ls to continue to add new functionality around code completion and formatting. See the terraform-ls CHANGELOG for details.

In addition, this new version brings the syntax highlighting up to date with all HCL2 features, as needed for Terraform 0.12 and above.

Configuration Changes Please note that in 2.x, the configuration differs from 1.4.0, if you are having issues with the Language Server starting, you can reset the configuration to the following:

See the CHANGELOG for more information.

Terraform 0.11

If you are using a Terraform version prior to 0.12.0, you can install the pre-transfer version of this extension manually by following the instructions in the wiki.

Visual Studio Code Remote Terraform

Known Issues

  • Multi-folder workspaces are not yet supported. (info)
  • A number of different folder configurations (specifically when your root module is not a parent to any submodules) are not yet supported. (info)

User Group

We're starting a user group for the Terraform VS Code extension. This group offers opportunities to provide feedback, access pre-release versions, and help influence the direction of this project. Collaboration and updates will be via HashiCorp Discuss (discuss.hashicorp.com), along with occasional live events.

Terraform

If you are interested, please complete and submit our participation form, and we will get back to you with an invite!

Credits

  • Mikael Olenfalk - creating and supporting the vscode-terraform extension, which was used as a starting point and inspiration for this extension.
-->

In this article, you'll learn how to use the combination of Terraform and Yeoman. Terraform is a tool for creating infrastructure on Azure. Yeoman makes it easy to create Terraform modules.

In this article, you learn how to do the following tasks:

  • Create a base Terraform template using the Yeoman module generator.
  • Test the Terraform template using two different methods.
  • Run the Terraform module using a Docker file.
  • Run the Terraform module natively in Azure Cloud Shell.

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Visual Studio Code: Download Visual Studio Code for your platform.
  • Terraform: Install Terraform to run the module created by Yeoman.
  • Docker: Install Docker to run the module created by the Yeoman generator.
  • Go programming language: Install Go as Yeoman-generated test cases are code using the Go language.

Note

Most of the procedures in this article involve the command line interface. The steps described apply to all operating systems and command line tools. For the examples, PowerShell was chosen for the local environment and Git Bash for the cloud shell environment.

Prepare your environment

Install Node.js

To use Terraform in the Cloud Shell, you need to install Node.js 6.0+.

Note

To verify that Node.js is installed, open a terminal window and enter node --version.

Install Yeoman

Run the following command:

Install the Yeoman template for Terraform module

Run the following command:

To verify that Yeoman is installed, run the following command:

Create a directory for the Yeoman-generated module

The Yeoman template generates files in the current directory. For this reason, you need to create a directory.

This empty directory is required to be put under $GOPATH/src. For more information about this path, see the article Setting GOPATH.

  1. Navigate to the parent directory from which to create a new directory.

  2. Run the following command replacing the placeholder. For this example, a directory name of GeneratorDocSample is used.

  3. Navigate to the new directory:

Create a base module template

  1. Run the following command:

  2. Follow the on-screen instructions to provide the following information:

    • Terraform module project Name - A value of doc-sample-module is used for the example.

    • Would you like to include the Docker image file? - Enter y. If you enter n, the generated module code will support running only in native mode.

  3. List the directory contents to view the resulting files that are created:

Review the generated module code

  1. Launch Visual Studio Code

  2. From the menu bar, select File > Open Folder and select the folder you created.

Terraform

The following files were created by the Yeoman module generator:

  • main.tf - Defines a module called random-shuffle. The input is a string_list. The output is the count of the permutations.
  • variables.tf - Defines the input and output variables used by the module.
  • outputs.tf - Defines what the module outputs. Here, it's the value returned by random_shuffle, which is a built-in, Terraform module.
  • Rakefile - Defines the build steps. These steps include:
    • build - Validates the formatting of the main.tf file.
    • unit - The generated module skeleton doesn't include code for a unit test. If you want to specify a unit test scenario, you would you add that code here.
    • e2e - Runs an end-to-end test of the module.
  • test
    • Test cases are written in Go.
    • All codes in test are end-to-end tests.
    • End-to-end tests attempt to provision all of the items defined under fixture. The results in the template_output.go file are compared with the pre-defined expected values.
    • Gopkg.lock and Gopkg.toml: Defines the dependencies.

For more information about the Yeoman generator for Azure https://github.com/Azure/generator-az-terra-module, see the Terratest documentation.

Test your new Terraform module using a Docker file

This section shows how to test a Terraform module using a Docker file.

Note

This example runs the module locally; not on Azure.

Confirm Docker is installed and running

From a command prompt, enter docker version.

The resulting output confirms that Docker is installed.

To confirm that Docker is actually running, enter docker info.

Set up a Docker container

  1. From a command prompt, enter

    docker build --build-arg BUILD_ARM_SUBSCRIPTION_ID= --build-arg BUILD_ARM_CLIENT_ID= --build-arg BUILD_ARM_CLIENT_SECRET= --build-arg BUILD_ARM_TENANT_ID= -t terra-mod-example ..

    The message Successfully built will be displayed.

  2. From the command prompt, enter docker image ls to see your created module terra-mod-example listed.

  3. Enter docker run -it terra-mod-example /bin/sh. After running the docker run command, you're in the Docker environment. At that point, you can discover the file by using the ls command.

Build the module

  1. Run the following command:

  2. Run the following command:

Terraform Installation Visual Studio Code

Run the end-to-end test

Visual Studio Terraform
  1. Run the following command:

  2. After a few moments, the PASS message will appear.

  3. Enter exit to complete the test and exit the Docker environment.

Use Yeoman generator to create and test a module in Cloud Shell

In this section, the Yeoman generator is used to create and test a module in Cloud Shell. Using Cloud Shell instead of using a Docker file greatly simplifies the process. Using Cloud Shell, the following products are all pre-installed:

  • Node.js
  • Yeoman
  • Terraform

Start a Cloud Shell session

How To Use Visual Studio

  1. Start an Azure Cloud Shell session via either the Azure portal, shell.azure.com, or the Azure mobile app.

  2. The Welcome to Azure Cloud Shell page opens. Select Bash (Linux).

  3. If you have not already set up an Azure storage account, the following screen appears. Select Create storage.

  4. Azure Cloud Shell launches in the shell you previously selected and displays information for the cloud drive it just created for you.

Prepare a directory to hold your Terraform module

  1. At this point, Cloud Shell will have already configured GOPATH in your environment variables for you. To see the path, enter go env.

  2. Create the $GOPATH directory, if one doesn't already exist: Enter mkdir ~/go.

  3. Create a directory within the $GOPATH directory. This directory is used to hold the different project directories created in this example.

  4. Create a directory to hold your Terraform module replacing the placeholder. For this example, a directory name of my-module-name is used.

  5. Navigate to your module directory:

Create and test your Terraform module

Visual Studio Terraform Azure

  1. Run the following command and follow the instructions. When asked if you want to create the Docker files, you enter N.

  2. Run the following command to install the dependencies:

  3. Run the following command to build the module:

  4. Run the following command to run the test:

Troubleshooting

For Terraform-specific support, use one of HashiCorp's community support channels to Terraform:

Visual Studio Code

  • Questions, use-cases, and useful patterns: Terraform section of the HashiCorp community portal
  • Provider-related questions: Terraform Providers section of the HashiCorp community portal

Next steps

Best Ide For Terraform

Install and use the Azure Terraform Visual Studio Code extension.