REW

How Do I Open A Terraform File?

Published Aug 29, 2025 5 min read
On this page

To open and work with a Terraform file, you use a plain text editor or an Integrated Development Environment (IDE).

There is no "open" command in the way you would open a document in a word processor. Instead, you edit the plain text file using a tool of your choice and then execute Terraform commands in your terminal to process and apply the configurations defined in that file.

Part 1: How to open a Terraform file for editing

Terraform configuration files are human-readable, plain-text files written in the HashiCorp Configuration Language (HCL). This means you can use almost any text editor to view or modify them.

The best tools for the job

  • Visual Studio Code (VS Code): This is the recommended and most popular option. For the best experience, install the official HashiCorp Terraform extension, which provides features like syntax highlighting, autocompletion, and error checking.
  • JetBrains IDEs (e.g., IntelliJ IDEA, PyCharm): These powerful IDEs offer excellent support for Terraform via dedicated plugins.
  • Sublime Text: A lightweight yet powerful text editor with Terraform syntax highlighting available through package control.
  • Vim/Neovim: For users who prefer a terminal-based editor, Vim is a highly customizable option with excellent HCL support.
  • Notepad++: A popular, simple editor for Windows that supports custom syntax highlighting.

Part 2: Understanding common Terraform file types

Not all Terraform files serve the same purpose. Knowing which file you're dealing with is crucial for understanding how to proceed.

File Type Extension Purpose How to Open/View
Configuration Files .tf or .tf.json The core files where you define your infrastructure as code using HCL or JSON. These are what you edit with a text editor. Text Editor / IDE. This is the file you open directly to write or edit your code. Common examples include main.tf, variables.tf, and outputs.tf.
State Files terraform.tfstate A critical file that stores the state of your managed infrastructure. It is a JSON file, but manual editing is strongly discouraged as it can corrupt your state. Terminal. Use the terraform state command to safely inspect or modify state. To view all resources: terraform state listTo see a specific resource's details: terraform state show <resource_address>
Backup State Files terraform.tfstate.backup An automatic backup created by Terraform whenever the state file is modified. Terminal. As with the state file, use terraform state commands. In an emergency, it can be viewed in a text editor to recover information, but manipulation should be done with Terraform CLI.
Plan Files .tfplan (often saved as plan.out) A binary file that saves the execution plan generated by terraform plan -out=.... Terminal. To view its human-readable contents, use the terraform show command: terraform show <planfile.out>.
Variable Definitions .tfvars or .auto.tfvars Plain-text files used to set values for input variables declared in your .tf files. Text Editor / IDE. These are plain text and are meant to be opened and edited directly.
Lock Files .terraform.lock.hcl A file that tracks the provider versions used in your configuration to ensure all team members use the same versions. Text Editor / IDE. Though it's a text file, it should typically not be manually edited. It is managed by Terraform and should be committed to version control.

Part 3: The Terraform workflow (from file to infrastructure)

Opening a file is only the first step. The true "opening" is a series of terminal commands that brings your code to life.

Step 1: Write and edit your configuration

Use your chosen text editor or IDE to create or modify your .tf files. For a basic setup, you might start with these common files:

  • main.tf: Contains the main resource blocks that define your infrastructure.
  • variables.tf: Declares the variables your configuration will accept.
  • outputs.tf: Defines the data you want to retrieve after applying your configuration.

Step 2: Initialize your working directory

After creating or cloning a Terraform project, you must run the initialization command. This downloads the necessary provider plugins specified in your configuration.

terraform init

Use code with caution.

Step 3: Preview changes with a plan

Before making any changes, run terraform plan. This command reads your .tf files and your state file (if it exists) and produces an execution plan detailing exactly what will be created, changed, or destroyed.

terraform plan

Use code with caution.

If you need to review the plan later or get a JSON output for automation, you can save it to a file:

terraform plan -out="my-plan.tfplan"

Use code with caution.

To read the saved plan file, use terraform show:

terraform show "my-plan.tfplan"

Use code with caution.

Step 4: Apply the configuration

Once you are satisfied with the plan, use terraform apply to execute it. Terraform will apply the changes to your cloud provider and update the state file to reflect the new infrastructure.

terraform apply

Use code with caution.

If you saved your plan in the previous step, you can use that file to apply the exact same set of changes:

terraform apply "my-plan.tfplan"

Use code with caution.

Step 5: View the state and outputs

After applying, you can inspect the current state of your infrastructure or retrieve important information like IP addresses and IDs.

  • To view outputs: terraform output
  • To list all resources in the state: terraform state list
  • To show a specific resource's details: terraform state show <resource_address>

Step 6: Destroy infrastructure (when finished)

When you are done with your infrastructure, you can use terraform destroy to tear down all the resources managed by your configuration.

terraform destroy

Use code with caution.

Enjoyed this article? Share it with a friend.