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.
Press Ctrl+Alt+S to open settings and then select
.Open the Marketplace tab, find the Terraform and HCL plugin, and click Install (restart the IDE if prompted).
PyCharm provides the following features:
You can customize color settings in the Settings dialog (Ctrl+Alt+S) , on the and pages.
You can access code style settings related to Terraform in the Settings dialog (Ctrl+Alt+S) on the page.
Complete entire lines of code using a locally run deep learning model.
You can disable Full-line code completion for Terraform in the Settings dialog (Ctrl+Alt+S) on the page.
Inspections
You can detect duplicate, conflicting, deprecated, or missing elements, unresolved references, unknown resources, and more.
You can check all Terraform inspections in the Settings dialog (Ctrl+Alt+S) under .
This includes completion for known providers, resources, data sources, and arguments.
For resources, variables, locals, and data sources, PyCharm 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.
You can disable, enable, and change the position of inlay hints for usages in the Settings dialog (Ctrl+Alt+S) under .
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.
The tool window is available in
or by pressing Alt+7.Hover over an argument to get quick documentation for it.
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, PyCharm detects the Terraform installation path. If it is not the case or if the path is detected incorrectly, you can specify it manually.
Press Ctrl+Alt+S to open settings and then select
.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
In a Terraform file, click in the gutter.
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.
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, PyCharm 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 in the run widget and selecting Save Configuration.
Create Terraform run configuration manually
Go to Run widget and select Edit Configurations from the drop-down menu.
. Alternatively, click in theClick the Add New Configuration button () and start typing Terraform.
Select a type of the run configuration:
Terraform Plan to run
terraform plan
commandsTerraform Apply to run
terraform apply
commandsTerraform, which lets you provide any other Terraform command
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 PyCharm, you can apply code style to your Terraform files. PyCharm 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
.Alternatively, press Ctrl+Alt+Shift+F.
While the Terraform formatter terraform fmt
applies to an entire file, PyCharm formatter can also be applied to a selected piece of code.
Use PyCharm formatter
PyCharm formatter is based on the PyCharm code style provided by the Terraform and HCL plugin. You can customize this code style in the IDE settings, under .
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
. Or press Ctrl+Alt+Shift+L.To reformat part of the code, select a piece of code and, in the main menu, go to
. Or press Ctrl+Alt+L.
Terraform template files
PyCharm 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.
Navigate to template file
In a Terraform file, you can use the templatefile function to inject a template. With PyCharm, you can quickly navigate to the template file from your Terraform file.
In your Terraform file, locate the
templatefile
function.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.
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.
In the list of languages that opens, select the language.
Advanced settings
- Download Terraform documentation automatically
By default, PyCharm downloads quick-documentation for Terraform properties from the Terraform Registry. In case you have network or performance issues, you can disable it, and PyCharm 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, PyCharm 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 PyCharm to update its metadata.- Deep search for variables
When you use variables in Terraform, PyCharm checks all Terraform files in your project for the variable definition. Clear the checkbox if you want PyCharm to check only files within the root directory (the one with
.terraform.lock.hcl
).- Terraform fmt
Use the Terraform formatting tool
terraform fmt
instead of PyCharm formatter when you invoke the Reformat File action.