IntelliJ IDEA 2024.3 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 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.

  • Full Line code completion

    Complete entire lines of code using a locally run deep learning model.

    Terraform Full Line code completion

    You can disable Full-line code completion for Terraform in the Settings dialog (Ctrl+Alt+S) on the Editor | General | Inline Completion page.

  • Inspections

    You can detect duplicate, conflicting, deprecated, or missing elements, unresolved references, unknown resources, 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

    For resources, variables, locals, and data sources, IntelliJ IDEA shows the number of their usages in the form of inlay hints. Click the inlay hint (or press Ctrl+B) to show all its usages and navigate to them.

    Terraform find usages

    You can disable, enable, and change the position of inlay hints for usages in the Settings dialog (Ctrl+Alt+S) under Editor | Inlay Hints | Code vision | 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

    For more details, you can also open Terraform Registry (which is the source of this documentation) 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 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.

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 Init to run terraform init commands

    • Terraform Validate to run terraform validate commands

    • Terraform Plan to run terraform plan commands

    • Terraform Apply to run terraform apply commands

    • Terraform Destroy to run terraform destroy commands

    • Terraform, which lets you provide any other Terraform command

  4. Give the run configuration a name and, if necessary, change the working directory. If you use environment variables, specify them in the Environment variables field or select a file to use variables from it.

Reformat Terraform code

With IntelliJ IDEA, you can apply code style to your Terraform files. IntelliJ IDEA includes two formatting tools:

Use Terraform formatter

The Terraform formatting tool (terraform fmt) is based on the Terraform code style. It applies to .tf and .tfvars files.

The use of the Terraform formatting tool requires Terraform to be installed.

  • In the main menu, go to Code | Terraform Tools | Format file.

  • Alternatively, press Ctrl+Alt+Shift+F.

While the Terraform formatter terraform fmt applies to an entire file, IntelliJ IDEA formatter can also be applied to a selected piece of code.

Use IntelliJ IDEA formatter

IntelliJ IDEA formatter is based on the IntelliJ IDEA code style provided by the Terraform and HCL plugin. You can customize this code style in the IDE settings, under Editor | Code Style | Terraform.

In addition to .tf and .tfvars, it also applies to .hcl files.

  • To reformat a file, open it in the editor and, in the main menu, go to Code | Reformat File. Or press Ctrl+Alt+Shift+L.

  • To reformat part of the code, select a piece of code and, in the main menu, go to Code | Reformat Code. Or press Ctrl+Alt+L.

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 templates

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

If the language is specified in the template file extension (file.<lang>.tftpl), this language is used as a data language. For example, in file.js.tftpl, JavaScript will be used. If the data language is not specified (for example, file.tftpl), the template file is recognized as plain text.

You can select a data language (or change the detected one) if you want the file syntax to be recognized accordingly.

  1. Right-click the file tab in the editor and select Change <Language> template data language to, for example, Change JavaScript template data language to or Change Plain text template data language to.

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

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

Advanced settings

Download Terraform documentation automatically

By default, IntelliJ IDEA downloads quick-documentation for Terraform properties from the Terraform Registry. In case you have network or performance issues, you can disable it, and IntelliJ IDEA will show descriptions from metadata instead. The availability of such descriptions depends on the resource developers.

Build local metadata automatically

The Terraform and HCL plugin comes with provider metadata of a specific version, which allows you to use coding assistance, such as completion for known properties. If you have a different version of provider metadata, IntelliJ IDEA updates its own metadata every time you initialize your project (terraform init) to ensure you get correct coding assistance. You can disable this checkbox if you don't want IntelliJ IDEA to update its metadata.

Deep search for variables

When you use variables in Terraform, IntelliJ IDEA checks all Terraform files in your project for the variable definition. Clear the checkbox if you want IntelliJ IDEA to check only files within the root directory (the one with .terraform.lock.hcl).

Last modified: 29 November 2024