Skip to main content
In this lesson, we will revisit some of the most commonly used Terraform commands. These commands help ensure your configurations are valid, well-formatted, and in sync with your real-world infrastructure. Read on to learn how to validate, format, view the state, and manage resources with Terraform.

Terraform Validate

The terraform validate command checks whether your configuration syntax is correct and internally consistent. When your configuration is valid, you’ll receive a successful validation message. For example, a valid configuration like the one below:
Running the command produces:
If an error exists in your configuration file, Terraform pinpoints the problematic line and offers suggestions for a fix. For instance, using an unsupported argument such as file_permissions instead of the correct file_permission generates an error message:
Always review the error messages carefully—Terraform’s hints help you quickly identify and fix configuration mistakes.

Terraform fmt

The terraform fmt command scans all configuration files in the current directory and reformats them into a canonical format. This improves readability and consistency across your files. When you run this command, any files that are modified will be displayed in the output.

Terraform Show

The terraform show command displays the current state of your managed infrastructure. This includes a detailed view of resources and their attributes. To facilitate further processing, you can append the --json flag, which outputs the state in JSON format for easier integration with other tools.

Terraform Providers

The terraform providers command lists all providers declared in your configuration. For example, if your configuration uses only AWS resources, the command produces an output similar to:

Terraform Output

The terraform output command retrieves the output variables defined in your configuration. You can display either all outputs or a specific one by providing the output name. Consider the example below:
When you run:
You can also specify an output by appending its name to the command.

Terraform Refresh

Terraform automatically refreshes the state during commands like terraform plan and terraform apply to capture any real-world changes. The terraform refresh command explicitly syncs the state file with the actual infrastructure. For example, if a resource has been manually updated, refreshing the state will capture the latest changes:
This command updates the state file without altering your actual infrastructure, helping you determine the next steps for your deployment.

Terraform Graph

The terraform graph command generates a visual representation of resource dependencies in your configuration or execution plan. The output is produced in DOT format and can be visualized with Graphviz. For users who prefer a visual perspective, this helps in understanding complex dependency relationships within your infrastructure.
You can generate and inspect the DOT output with Graphviz to gain a deeper understanding of your infrastructure’s architecture.

Terraform State Commands

Terraform includes several commands to manage the state file—allowing you to list, view, move, pull, and remove state entries. It is important to use these commands instead of manually modifying the state file.

List Resources

Use the terraform state list command to display all resources recorded in the state file. The output lists resource addresses in the format resource_type.resource_name. For example:
You can also filter resources by specifying a pattern:

Show Resource Details

The terraform state show command displays attributes of a single resource from the state file. For example, to see details of an S3 bucket:

Move Resources

The terraform state mv command is used to rename or move a resource within the state file. This does not automatically update your configuration files, so you must adjust them manually. For example, suppose you have the following configuration for a DynamoDB table:
And your state file includes:
To rename this resource from state-locking to state-locking-db, execute:
After moving the resource in the state, update your configuration file accordingly:
A subsequent terraform apply will confirm that no changes are pending:

Pulling Remote State

If you are using remote state, you may need to download the state file locally. The terraform state pull command retrieves the remote state, which can be processed with tools like jq for JSON querying:
You can query specific details using jq:

Removing Resources from State

The terraform state rm command allows you to remove one or more resources from the state file. This is useful when you no longer want Terraform to manage a resource. Remember, removing a resource from the state does not delete the actual infrastructure.
After removal, ensure you also delete the corresponding resource block from your configuration files.

Pushing State Changes

The terraform state push command updates the remote state by pushing your local state file. Use this command with extreme caution, as it can disrupt your configuration if misused. If the local state differs significantly from the remote state, you may encounter an error message like this:
Although the --force option exists, it is recommended only as a last resort.
That concludes our lesson on basic Terraform commands. By understanding these commands, you will be better equipped to validate your configurations, maintain a clean state, and manage your infrastructure efficiently. For additional resources and more detailed explanations, consider visiting the following links:

Watch Video