Visual Studio Code Terraform



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. Terraform Visual Studio Code Extension The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server. Terraform This is a Visual Studio Code extension. Adds syntax support for the Terraform and Terragrunt configuration language. Terraform Visual Studio Code Extension The HashiCorp Terraform Visual Studio Code (VS Code) extension adds syntax highlighting and other editing features for Terraform files using the Terraform Language Server.

-->

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.

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:

Run the end-to-end test

  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

  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

  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:

  • 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

Install and use the Azure Terraform Visual Studio Code extension.

-->

The Azure Terraform Visual Studio Code extension enables you to work with Terraform from the editor. With this extension, you can author, test, and run Terraform configurations. The extension also supports resource graph visualization.

In this article, you learn how to:

  • Automate the provisioning of Azure services using Terraform
  • install and use the Terraform Visual Studio Code extension for Azure services.
  • Use Visual Studio Code to write, plan, and execute Terraform plans.

Prerequisites

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Terraform: Install and configure Terraform.
  • Visual Studio Code: Install the version of Visual Studio Code that is appropriate for your environment.

Prepare your dev environment

Visual Studio Code Terraform No Schema Found

Install Git

To complete the exercises in the article, you need to install Git.

Install HashiCorp Terraform

Follow the instructions on the HashiCorp Install Terraform webpage, which covers:

  • Downloading Terraform
  • Installing Terraform
  • Verifying Terraform is correctly installed

Tip

Be sure to follow the instructions regarding setting your PATH system variable.

Install Node.js

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

Note

Terraform

Visual Studio Code Terraform Autocomplete

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

Install GraphViz

To use the Terraform visualize function, you need to install GraphViz.

Note

To verify if GraphViz is installed, open a terminal window and enter dot -V.

Install the Azure Terraform Visual Studio Code extension

  1. Launch Visual Studio Code.

  2. Select Extensions.

  3. Use the Search Extensions in Marketplace text box to search for the Azure Terraform extension:

  4. Select Install.

    Note

    When you select Install for the Azure Terraform extension, Visual Studio Code will automatically install the Azure Account extension. Azure Account is a dependency file for the Azure Terraform extension, which it uses to perform Azure subscription authentications and Azure-related code extensions.

Verify the Terraform extension is installed in Visual Studio Code

  1. Select Extensions.

  2. Enter @installed in the search text box.

The Azure Terraform extension will appear in the list of installed extensions.

You can now run all supported Terraform commands in your Cloud Shell environment from within Visual Studio Code.

Exercise 1: Basic Terraform commands walk-through

In this exercise, you create and execute a basic Terraform configuration file that provisions a new Azure resource group.

Prepare a test plan file

  1. In Visual Studio Code, select File > New File from the menu bar.

  2. In your browser, navigate to the Terraform azurerm_resource_group page and copy the code in the Example Usage code block:

  3. Paste the copied code into the new file you created in Visual Studio Code.

    Note

    You may change the name value of the resource group, but it must be unique to your Azure subscription.

  4. From the menu bar, select File > Save As.

  5. In the Save As dialog, navigate to a location of your choice and then select New folder. (Change the name of the new folder to something more descriptive than New folder.)

    Note

    In this example, the folder is named TERRAFORM-TEST-PLAN.

  6. Make sure your new folder is highlighted (selected) and then select Open.

  7. In the Save As dialog, change the default name of the file to main.tf.

  8. Select Save.

  9. In the menu bar, select File > Open Folder. Navigate to and select the new folder you created.

Run Terraform init command

  1. Launch Visual Studio Code.

  2. From the Visual Studio Code menu bar, select File > Open Folder... and locate and select your main.tf file.

  3. From the menu bar, select View > Command Palette... > Azure Terraform: Init.

  4. When the confirmation appears, select OK.

  5. The first time you launch Cloud Shell from a new folder, you're prompted to create a web application. Select Open.

  6. The Welcome to Azure Cloud Shell page opens. Select Bash or PowerShell.

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

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

  9. You may now exit the Cloud Shell

  10. From the menu bar, select View > Command Palette > Azure Terraform: init.

Visualize the plan

Earlier in this article, you installed GraphViz. Terraform can use GraphViz to generate a visual representation of either a configuration or execution plan. The Azure Terraform Visual Studio Code extension implements this feature via the visualize command.

  • From the menu bar, select View > Command Palette > Azure Terraform: Visualize.

Run Terraform plan command

The Terraform plan command is used to check whether the execution plan for a set of changes will do what you intended.

Note

Terraform plan does not make any changes to your real Azure resources. To actually make the changes contained in your plan, we use the Terraform apply command.

  • From the menu bar, select View > Command Palette > Azure Terraform: plan.

Run Terraform apply command

After being satisfied with the results of Terraform plan, you can run the apply command.

  1. From the menu bar, select View > Command Palette > Azure Terraform: apply.

  2. Enter yes.

Verify your Terraform plan was executed

To see if your new Azure resource group was successfully created:

  1. Open the Azure portal.

  2. Select Resource groups in the left navigation pane.

Your new resource group should be listed in the NAME column.

Note

You may leave your Azure Portal window open for now; we will be using it in the next step.

Run Terraform destroy command

  1. From the menu bar, select View > Command Palette > Azure Terraform: destroy.

  2. Enter yes.

Verify your resource group was destroyed

To confirm that Terraform successfully destroyed your new resource group:

  1. Select Refresh on the Azure portal Resource groups page.

  2. Your resource group will no longer be listed.

Exercise 2: Terraform compute module

In this exercise, you learn how to load the Terraform compute module into the Visual Studio Code environment.

Clone the terraform-azurerm-compute module

  1. Use this link to access the Terraform Azure Rm Compute module on GitHub.

  2. Select Clone or download.

    Note

    In this example, our folder was named terraform-azurerm-compute.

Open the folder in Visual Studio Code

  1. Launch Visual Studio Code.

  2. 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.

  1. In the Terminal pane of the Visual Studio Code IDE, enter terraform init.

  2. Enter az login, press <Enter, and follow the on-screen instructions.

Module test: lint

  1. From the menu bar, select View > Command Palette > Azure Terraform: Execute Test.

  2. From the list of test-type options, select lint.

  3. When the confirmation appears, select OK, and follow the on-screen instructions.

Note

When you execute either the lint or end to end test, Azure uses a container service to provision a test machine to perform the actual test. For this reason, your test results may typically take several minutes to be returned.

After a few moments, you see a listing in the Terminal pane similar to this example:

Test the module

  1. From the menu bar, select View > Command Palette > Azure Terraform: Execute Test.

  2. From the list of test type options, select end to end.

  3. When the confirmation appears, select OK, and follow the on-screen instructions.

Note

When you execute either the lint or end to end test, Azure uses a container service to provision a test machine to perform the actual test. For this reason, your test results may typically take several minutes to be returned.

After a few moments, you see a listing in the Terminal pane similar to this example:

Troubleshooting

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

  • 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





Comments are closed.