First time at Zeet?

17 Apr
2024
-
18
min read

A Complete Walkthrough of 23 Crucial Terraform Commands

Terraform commands can be daunting. With this complete walkthrough, you'll learn crucial commands to help you navigate Terraform like a pro.

Jack Dwyer

Product
How To
Content
heading2
heading3
heading4
heading5
heading6
heading7

Share this article

What Is Terraform and Its Role in IaC?

woman trying out new Terraform Commands

What is Terraform? Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp. It allows me to define, provision, and manage infrastructure resources across multiple cloud providers using a declarative configuration language. This is critical because treating infrastructure as code means teams can version control their configurations, collaborate seamlessly, and apply changes across different environments, reducing the risk of manual errors and inconsistencies. 

The Power of Terraform Commands

Terraform has multiple advantages that make managing infrastructure a breeze. Terraform commands boast multi-cloud support that allows you to manage infrastructure across multiple platforms from a single configuration. 

It promotes an immutable infrastructure approach, ensuring consistency and reducing the risk of configuration drift. Terraform's declarative approach allows you to define desired state configurations, making it easier to provision and manage large-scale infrastructure deployments efficiently.

Related Reading

Getting Started with Terraform Commands

person helping junior with Terraform Commands

The Terraform Command Line Interface (CLI) is a powerful tool used for deploying and managing infrastructure through Terraform. As part of the CLI, there are multiple commands that help in this process, from initializing your Terraform directory to planning, applying, and destroying resources.

CLI Commands for Infrastructure Automation

The primary function of the CLI is to automate infrastructure management and with Terraform, it integrates seamlessly with various cloud providers to establish infrastructure as code. These commands are integral for automating infrastructure management and deploying and managing resources.

Essential Workflow for Terraform Management

The basic workflow Terraform commands are critical for efficient infrastructure management Terraform init, validate, plan, apply, and destroy are essential for deploying and managing infrastructure efficiently.

​​Advanced Operations with Terraform

With Terraform, you not only get simple commands but also the ability to perform multiple operations such as checking outputs, doing state-related operations, and testing different expressions. These commands are crucial for managing infrastructure effectively.

Enhancing Infrastructure Management with Zeet

Zeet helps you to get more from your cloud, Kubernetes, and Terraform investments and helps your engineering team become strong individual contributors through our CI/CD & deployment platform. 

Contact Zeet to learn more about how Zeet can help you get seamless cloud deployments every time, and ensure your team becomes a top-performing engineering team.

Zeet Terraform and Helm Product Overview

Related Reading

Resource and Module Management Commands

junior dev using Terraform Commands

The Terraform command line interface (CLI) provides several commands that are designed to help manage resources and modules effectively. One of the most essential commands is `terraform get`, which enables you to download and install modules needed for the configuration. 

When working with a Terraform configuration that references modules, you can retrieve the module files by running this command. This will make sure that the modules are properly downloaded and installed, ensuring that the Terraform configuration is accurate.

Visualizing Infrastructure with Terraform Graph

Another powerful Terraform command is `terraform graph. When you execute this command, it generates a visual representation of the configuration or execution plan. This can be incredibly beneficial when you want to visualize the relationships between different resources and modules in your Terraform configuration. By providing a graphical representation, the `terraform graph` command can help you understand and troubleshoot the configuration more effectively.

Managing Existing Resources with Terraform Import

As you work with existing infrastructure that you want to manage using Terraform, the `terraform import` command is particularly useful. By using this command, you can add existing resources to Terraform management without creating new resources. This is an excellent way to integrate previously deployed infrastructure into your Terraform configuration and take advantage of Terraform's management capabilities.

Managing Existing Resources with Terraform Import

Sometimes, you may need to mark a resource for recreation or deletion and re-creation during the next plan/apply cycle. The `terraform taint` command allows you to do exactly that. By marking a resource as 'tainted', you are indicating that it needs to be recreated during the next Terraform operation. This can be especially helpful when you need to force a resource to be re-created due to changes in the configuration.

Undoing Resource Tainting with Terraform Untaint

After marking a resource as 'tainted', if you decide that you no longer need to recreate it, the `terraform untaint` command can be used. This command removes the 'tainted' status from a resource, allowing it to be managed normally without being recreated during the next Terraform operation.

Terraform State Management Commands

Managing Terraform's State Files

Using the Terraform state command, I can manage Terraform's state files. This command provides various subcommands for managing the state file throughout the lifecycle of a project.

Updating the Local State

To update the local state file for a Terraform project, I use the Terraform refresh command. This command fetches real-world resource information and updates the local state file to ensure that it aligns with the actual resources deployed in the configured environment.

Unlocking the State

In instances where automatic unlocking of the state file fails, the terraform force-unlock command can be employed to manually release a state lock. This command is useful for addressing lock contention issues or stalled deployments that have left the state file in a locked state.

Configuration Inspection and Outputs Commands

senior dev teaching Terraform Commands

Terraform show

When you need to easily view the outputs from a state or plan file, Terraform offers the 

Terraform show command. This command displays outputs in a readable human format , making it a simple and straightforward way to examine the outputs of your Terraform deployment.

Terraform output

To extract values from the state file and have them printed in the console, the terraform output command will be your go-to. This command not only extracts the desired values but also ensures they are displayed for easy viewing. It’s an essential command when you need to quickly see what has been saved in the state file.

Terraform console

The Terraform console command is particularly useful when you need an interactive way to evaluate Terraform expressions. This command provides an environment where you can easily see the results of your expressions, letting you quickly and conveniently experiment with different options.

Maintenance and Cleanup Commands

illustration on effectiveness of Terraform Commands

When it comes to maintaining and cleaning up Terraform configurations, there are a few commands that come in handy. One of the most useful commands is `terraform fmt`. This command is quite useful as it rewrites configuration files to a canonical format and style. By using this command, the configurations can be maintained in a consistent style, making it easier for developers to read and understand the code. 

Managing Environments

Another important command for maintaining Terraform configurations is `terraform workspace`. This command helps in managing separate namespaces, which allow using the same configuration for different environments. 

By enabling developers to switch between workspaces, this command helps in maintaining multiple sets of Terraform configurations in a single directory. This feature allows developers to keep track of configurations easily and understand which configurations are being used for which environment.

Version and Provider Management

When managing Terraform versions and providers, there are several essential commands to ensure compatibility and functionality

Terraform providers

This command displays a tree of all the providers used in the configuration, along with their versions. It gives a clear overview of the providers in use, allowing you to check compatibility and update any outdated versions.

Terraform version

By using this command, you get to see the current Terraform version and provider versions in use. Keeping track of the versions is vital to ensure smooth operations and compatibility with different providers and resources.

Terraform login

When working with Terraform Cloud, you may need to obtain API credentials to save locally. The `terraform login` command facilitates this process, ensuring easy access to Terraform Cloud and enhancing the overall user experience.

Terraform logout

This command is the counterpart to `terraform login.` In case you need to remove stored API credentials for Terraform Cloud, you can use `terraform logout.` It helps maintain security measures and ensures that only authorized users have access to the Terraform Cloud account.

Managing Terraform versions and providers is essential for maintaining compatibility and ensuring smooth functionality. Using these commands helps achieve this seamlessly.

Related Reading

Cloud Integration and Operations

The Terraform cloud command contains subcommands to manage Terraform Cloud functionalities. With these subcommands, I can interact with Terraform Cloud to manage workspaces, run operations, update the variables in a workspace, list the runs of a workspace, and configure the Terraform Cloud organization. This command is quite useful, especially when I use Terraform Cloud to manage my infrastructure, version control, and collaborate with my team or other developers.

Zeet Contact Us

Get Control of Your Releases With Zeet's CI/CD & Deployment Platform for Kubernetes and Terraform

Zeet is a powerful platform that optimizes your cloud, Kubernetes, and Terraform investments to maximize your engineering team's productivity and performance. With Zeet, you can deploy your applications seamlessly to the cloud every time, reducing the risk of errors and downtime. 

By leveraging Zeet's CI/CD & deployment platform, your team can become top-performing contributors in your organization, driving innovation and success. Contact Zeet today to learn how you can unlock the full potential of your cloud infrastructure with a deployment platform that simplifies and streamlines your Terraform commands. 

Partner with Zeet to transform your cloud deployments and empower your engineering team to deliver exceptional results.

Subscribe to Changelog newsletter

Jack from the Zeet team shares DevOps & SRE learnings, top articles, and new Zeet features in a twice-a-month newsletter.

Thank you!

Your submission has been processed
Oops! Something went wrong while submitting the form.