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.
tip
For more information about Terraform, refer to the official documentation.
This functionality relies on the Terraform and HCL plugin, which you need to install and enable.
Press CtrlAlt0S to open settings and then select Plugins.
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 (CtrlAlt0S) , on the Editor | Color Scheme | HCL and Editor | Color Scheme | HashiCorp Interpolation Language pages.
You can access code style settings related to Terraform in the Settings dialog (CtrlAlt0S) on the Editor | Code Style | Terraform 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 (CtrlAlt0S) on the Editor | General | Inline Completion page.
The floating toolbar, which allows you to quickly comment and reformat code, as well as invoke context and AI actions.
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 (CtrlAlt0S) under Editor | Inspections | Terraform.
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 Ctrl0B) 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 (CtrlAlt0S) under Editor | Inlay Hints | Code vision | Usages.
Refactorings
To rename the element and all its usages, press ShiftF6.
Structure view
Use the Structure tool window to quickly preview and navigate Terraform files.
The tool window is available in View | Tool Windows | Structure or by pressing Alt07.
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 ShiftF1.
tip
The Terraform and HCL plugin comes with provider metadata of a specific version (you can see the version number when you start typing a resource name). If your provider version differs, and you want to show relevant quick documentation for it, you may need to perform
terraform init
to retrieve documentation for that specific version.
Coding assistance also includes brace matching CtrlShift0M, code folding CtrlNumPad +/CtrlNumPad -, adding line comments Ctrl0/ and block comments CtrlShift0/.
To run Terraform commands from the IDE using run configurations, 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 CtrlAlt0S to open settings and then select Tools | Terraform and OpenTofu.
In the Terraform executable path field, specify the path to Terraform.
tip
You can also specify a path in WSL:
\\wsl$\<Distribution>\<path to Terraform>
For example:
\\wsl$\Ubuntu\usr\bin\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.
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.
Go to Run | Edit Configurations. Alternatively, click in the Run widget and select Edit Configurations from the drop-down menu.
Click the Add New Configuration button () and start typing Terraform.
Select a type of the run configuration:
Terraform Init to run
terraform init
commandsTerraform Validate to run
terraform validate
commandsTerraform Plan to run
terraform plan
commandsTerraform Apply to run
terraform apply
commandsTerraform Destroy to run
terraform destroy
commandsTerraform, which lets you provide any other Terraform command
In the Command list, select a Terraform command. To run a command not included in this list, select Custom and specify it in the Program arguments field.
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.
With PyCharm, you can apply code style to your Terraform files. PyCharm includes two formatting tools:
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 CtrlAltShift0F.
tip
If you also want to run the Terraform formatter when you invoke Code | Reformat File, you can configure PyCharm to do so: Open the IDE settings (CtrlAlt0S), go to Editor | Code Style | Terraform, open the Other tab, and, under Terraform fmt, select On 'Reformat File' action.
While the Terraform formatter terraform fmt
applies to an entire file, PyCharm formatter can also be applied to a selected piece of code.
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 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 CtrlAltShift0L.
To reformat part of the code, select a piece of code and, in the main menu, go to Code | Reformat Code. Or press CtrlAlt0L.
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.
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 Ctrl0B.
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 AltEnter (Show Context Actions) in the file and select Choose template data language.
In the list of languages that opens, select the language.
- 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.
OpenTofu is a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation.
PyCharm provides coding assistance for OpenTofu files, including syntax highlighting, completion, and inspections. It also has dedicated run configurations for OpenTofu that are basically the same as for Terraform, but they use the tofu
command.
To run OpenTofu commands from the IDE using run configurations, OpenTofu must be installed on your computer.
In most cases, PyCharm detects the OpenTofu installation path. If it is not the case or if the path is detected incorrectly, you can specify it manually.
Press CtrlAlt0S to open settings and then select Tools | Terraform and OpenTofu.
In the OpenTofu executable path field, specify the path to OpenTofu.
Thanks for your feedback!