Install the Azure Terraform Visual Studio Code extension

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure. For more information about using Terraform in Azure, see the Azure Terraform developer center

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

In this article, you learn how to:

  • Install the Azure Terraform Visual Studio Code extension
  • Use the extension to create an Azure resource group
  • Verify the resource group was created
  • Delete the resource group when finished testing using the extension

1. Configure your environment

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

2. Install the Azure Terraform Visual Studio Code extension

  1. Launch Visual Studio Code.

  2. From the left menu, select Extensions, and enter Azure Terraform in the search text box.

    Search Visual Studio Code extensions in Marketplace.

  3. From the list of extensions, locate the Azure Terraform extension. (It should be the first extension listed.)

  4. If the extension isn't yet installed, select the extension's Install option.

    Key points:

    • When you select Install for the Azure Terraform extension, Visual Studio Code automatically installs the Azure Account extension.
    • Azure Account is a dependency file for the Azure Terraform extension. This file is used to authenticate to Azure and Azure-related code extensions.
  5. To confirm the installation of the extensions, enter @installed in the search text box. Both the Azure Terraform extension and the Azure Account extension will appear in the list of installed extensions.

    View installed Terraform extensions.

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

3. Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and open that directory in Visual Studio. The files you create in this section should be created in your new directory.

  2. Create a file named main.tf and insert the following code:

    resource "random_pet" "rg-name" {
      prefix    = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      name      = random_pet.rg-name.id
      location  = var.resource_group_location
    }
    
  3. Create a file named variables.tf to contain the project variables and insert the following code:

    variable "resource_group_name_prefix" {
      default       = "rg"
      description   = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    
    variable "resource_group_location" {
      default = "eastus"
      description   = "Location of the resource group."
    }
    

    Key points:

    • The resource_group_name and resource_group_location values are shown with test values. You can set these values to whatever makes sense for your environment.
  4. Create a file named output.tf to contain the project variables and insert the following code:

    output "resource_group_name" {
        value = azurerm_resource_group.rg.name
    }
    

    Key points:

    • The output.tf file displays the randomized resource group name.

4. Push your code to Cloud Shell

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure Terraform: Push and select it when it displays.

  3. Select OK to confirm the opening of Cloud Shell.

    Confirm the opening of Cloud Shell.

    Key points:

    • Your workspace files that meet the filter defined in the azureTerraform.files setting in your configuration are copied to Cloud Shell.

5. Initialize Terraform within Visual Studio Code

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure Terraform: Init and select it when it displays.

    Key points:

    • Selecting this option is the same as running terraform init from the command line and will initialize your Terraform deployment.
    • This command downloads the Azure modules required to create an Azure resource group.
  3. Follow the prompts to install any dependencies - such as the latest supported version of nodejs.

  4. If this is the first time you're using Cloud Shell with your default Azure subscription, follow the prompts to configure the environment.

6. Create a Terraform execution plan within Visual Studio Code

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure Terraform: Plan and select it when it displays.

    Key points:

    • This command runs terraform plan to create an execution plan from the Terraform configuration files in the current directory.

7. Apply a Terraform execution plan within Visual Studio Code

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure Terraform: Apply and select it when it displays.

  3. When prompted for confirmation, enter yes and press <Enter>.

8. Verify the results

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure: Open Bash in Cloud Shell and select it when it displays.

  3. Run az group show to display the resource group. Replace the <resource_group_name> placeholder with the randomly generated name of the resource group displayed after applying the Terraform execution plan.

az group show --name <resource_group_name>

9. Clean up resources

  1. From the View menu, select Command Palette....

  2. In the Command Palette text box, start entering Azure Terraform: Destroy and select it when it displays.

  3. When prompted for confirmation, enter yes and press <Enter>.

  4. To confirm that Terraform successfully destroyed your new resource group, run the steps in the section, Verify the results.

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps