IntelliJ IDEA 2024.1 Help

Terraform

Terraform is an open-source tool that enables developers and operations teams to define, manage, and provision cloud resources and infrastructure components with code as opposed to manual configuration through a cloud provider's UI.

With it, you manage infrastructure resources, such as virtual machines, networks, and storage, in a declarative and automated manner. The replacement of manual configuration with codified templates ensures consistent and reproducible infrastructure setups.

Install the Terraform and HCL plugin

This functionality relies on the Terraform and HCL plugin, which you need to install and enable.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Plugins.

  2. Open the Marketplace tab, find the Terraform and HCL plugin, and click Install (restart the IDE if prompted).

IntelliJ IDEA provides the following features:

  • Syntax highlighting

    You can customize color settings in the Settings dialog (Ctrl+Alt+S) , on the Editor | Color Scheme | HCL and Editor | Color Scheme | HashiCorp Interpolation Language pages.

  • Code formatting

    You can access code style settings related to Terraform in the Settings dialog (Ctrl+Alt+S) on the Editor | Code Style | Terraform page.

  • Inspections

    You can detect duplicate, conflicting, deprecated, or missing elements, unresolved references, and more.

    Terraform missing property inspection

    You can check all Terraform inspections in the Settings dialog (Ctrl+Alt+S) under Editor | Inspections | Terraform.

  • Code completion

    This includes completion for known providers, resources, data sources, and arguments.

    Terraform resource completion
  • Code navigation

    Select a module or a variable and press Ctrl+B to navigate to its usages.

    Terraform find usages
  • Refactorings

    To rename the element and all its usages, press Shift+F6.

  • Structure view

    Use the Structure tool window to quickly preview and navigate Terraform files.

    Terraform Structure

    The tool window is available in View | Tool Windows | Structure or by pressing Alt+7.

  • Quick documentation

    Hover over an argument to get quick documentation for it.

    Terraform quick documentation

    The window that pops up contains a link to Terraform Registry for more details. You can also open this link by pressing Shift+F1.

Coding assistance also includes brace matching Ctrl+Shift+M, code folding Ctrl+NumPad +/Ctrl+NumPad -, adding line comments Ctrl+/ and block comments Ctrl+Shift+/.

Specify path to Terraform executable

To run Terraform commands from the IDE (create execution plans and apply plans), Terraform must be installed on your computer.

In most cases, IntelliJ IDEA detects the Terraform installation path. If it is not the case or if the path is detected incorrectly, you can specify it manually.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Tools | Terraform.

  2. In the Terraform executable path field, specify the path to Terraform.

Run Terraform

The Terraform and HCL plugin provides dedicated run configurations for Terraform. These run configurations allow you to customize the execution of terraform commands, such as adding arguments or passing environment variables.

Run Terraform using gutter icon

  1. In a Terraform file, click Run in the gutter.

  2. In the window that opens, select either Plan to create an execution plan or Apply if you already have a Terraform plan and want to apply it.

    Terraform run from gutter

If the Terraform initialization step was not performed for this directory, there will be a warning sign in the Run gutter icon. In this case, IntelliJ IDEA suggests you running the terraform init command before running terraform plan or terraform apply.

Terraform Init

Running Terraform from the gutter icon creates a temporary run configuration. You can save it as a permanent one by clicking More Actions in the run widget and selecting Save Configuration.

Create Terraform run configuration manually

  1. Go to Run | Edit Configurations. Alternatively, press Alt+Shift+F10, then 0.

  2. Click the Add New Configuration button (Add a run/debug configuration) and start typing Terraform.

    Terraform run configuration
  3. Select a type of the run configuration:

    • Terraform Plan to run terraform plan commands

    • Terraform Apply to run terraform apply commands

    • Terraform, which lets you provide any other Terraform command

  4. Give the run configuration a name and, if necessary, change the working directory and provide environment variables.

Terraform template files

IntelliJ IDEA provides support for Terraform template files that typically use the .tftpl extension. Support for template files includes coding assistance (syntax highlighting, inspections, and more), navigation to template files, and live templates.

To use live templates, start typing if or for in a Terraform template file.

Live tempalates

In a Terraform file, you can use the templatefile function to inject a template. With IntelliJ IDEA, you can quickly navigate to the template file from your Terraform file.

  1. In your Terraform file, locate the templatefile function.

  2. Place the caret at the template file name and press Ctrl+B.

Select template data language

By default, template files are recognized as plain text. You can change it to another supported programming language if you want the file syntax to be recognized accordingly.

  1. Right-click the file tab in the editor and select Change Plain text template data language to.

    Alternatively, press Alt+Enter (Show Context Actions) and select Choose template data language.

  2. In the list of languages that opens, select the language.

Last modified: 08 April 2024