Introduction
If you’re working with infrastructure as code, mastering Terraform commands is essential. An open-source product developed by HashiCorp, Terraform allows a developer and DevOps professional to watermark and preserve infrastructure configurations across many cloud providers, utilizing a declarative coded configuration environment. Whether you’re deploying virtual machines, setting up Kubernetes clusters, or managing databases, Terraform makes the entire process more streamlined and predictable.
It is a Terraform cheat sheet that aims to assist both the beginner and experienced user to effectively make use of Terraform, including initialization and validation, planning, application, and destroying the infrastructure. Learning and executing these commands can result in time savings, elimination of human error, and consistency across environments.
In this blog, we will discuss Terraform commands list with real-life examples, tricks, and best practices.
What Is Terraform Command Line Interface (CLI)
When Terraform is used with the objective of infrastructure as code, the primary channel of interaction will be the Terraform Command Line Interface (CLI). It allows users to execute various Terraform commands such as init, plan, apply, and destroy to provision, modify, and tear down infrastructure across cloud providers. The CLI reads the config and directs the APIs, and monitors resource state. Mastering the Terraform commands within the CLI is crucial for automating, scaling, and maintaining reliable infrastructure deployments efficiently.
Commands For Terraform Cheat Sheet
Here’s a quick reference to essential Terraform commands that every developer or DevOps engineer should know for managing infrastructure efficiently and reliably.
Show your Terraform Version
Terraform –version | Display the latest version of your Terraform and inform you whether there is newer version to download. |
Terraform Help Command
terraform –help | Displays rendered help information and lists the available commands and options available to Terraform. |
To Optimise Code Structure
terraform fmt | Write the Terraform configuration files in a standard form. |
terraform fmt –recursive | Auto-formats all Terraform configuration files placed in the current directory and subdirectories. |
terraform fmt –diff | Formats Terraform format configuration files and shows the changes of the file made but does not make the changes in reality. |
terraform fmt –check | Checks that Terraform configuration files account formatting rules without altering it. |
For Terraform Configuration
terraform init | Sets the initial Terraform configuration, whether it is new or old. |
terraform init -get-plugins=false | Launches Terraform without requiring updates for plugins. |
terraform init -lock=false | Initiates a new Terraform configuration or launches an existing one without gaining a lock on state files. |
terraform init -migrate-state | Used during initialization to move the Terraform state to an alternate backend. |
For Module Management
terraform get | Download and install any modules referenced in the configuration files. |
terraform get -update | Updates modules to their latest versions, ensuring the configuration uses the most recent module releases. |
For Syntax Integrity
terraform validate | Checks the syntax and structure of Terraform configuration files. |
terraform validate -json | Checks the syntax of Terraform configuration files and outputs the results in JSON format. |
For Visualize Infrastructure Changes
terraform plan | Creates an execution plan, which explains what Terraform will do to transform the targeted structure of the infrastructure. |
terraform plan -out=<path> | Generates an execution plan for infrastructure changes and saves it to the specified path for later application. |
terraform plan -destro | Creates destruction of the current infrastructure resources by generating an execution plan only to destroy the resources configured in Terraform. |
For Provisioning your Infrastructure
terraform apply | Planned changes specified in the configuration files are put into action. |
Terraform apply –auto-approve | The execution plan is automated without the need for manual confirmation. |
terraform apply -var=”environment=dev” | Simply deploys infrastructure described in your Terraform files, and “-var” allows passing of variables, such as “environment=dev” at go-time. |
terraform apply -var=”environment=dev” | Makes changes to the infrastructure referenced by Terraform configuration files and also uses variable values specified by the “varfile.tfvars” file. |
For Clean State
terraform destroy | Removes all resources defined in the Terraform configuration from the infrastructure. |
terraform destroy –auto-approve | Automatically destroys all Terraform-managed infrastructure without requiring manual confirmation |
For Taint’ or ‘Untaint’ The Resources
terraform taint | Marks a specific resource for recreation during the next apply, effectively forcing it to be destroyed and recreated. |
terraform untaint | Inverse of the terraform taint. |
For Syncing State
terraform refresh | updates the Terraform state file. |
For Visualize Infrastructure State
terraform show | Displays the current state of Terraform-managed infrastructure. |
terraform show <path to statefile> | Displays the current state of infrastructure resources defined in the specified state file. |
For Terraform State Management Commands
terraform state | Manages the state of Terraform-managed infrastructure. |
terraform state list | List of all resources managed by Terraform within the current state file. |
terraform state Push | uploads the local state to the configured remote state storage. |
terraform state rm | Remove a resource from the Terraform state. |
terraform state mv | Move a resource instance from one Terraform state file to another. |
For Exploring Available Providers
terraform providers | Displays a list of all available providers along with their versions |
For Workspace Management Commands
terraform workspace list | Displays a list of available workspaces |
terraform workspace show | Displays the current workspace name being used in Terraform. |
terraform workspace new <workspace name> | Creates a new isolated workspace for managing Terraform configurations and state |
terraform workspace delete <workspace name> | Remove a workspace, effectively deleting its state and associated resources. |
For Visualizing Terraform Outputs
terraform output | Displays the values of outputs defined in Terraform configurations. |
terraform output -json | Displays Terraform output values in JSON format for easy consumption by other tools or scripts. |
For Emergency State Lock Release
terraform force-unlock <lock_id> | Forcefully release a Terraform state lock identified by <lock_id>. |
For Authentication and Credential Management Commands
terraform login | Allows users to authenticate with Terraform Cloud or Terraform Enterprise |
terraform logout | Remove locally stored credentials for Terraform Cloud or Terraform Enterprise |
For Visualizing Resource Dependencies
terraform graph | Generates a visual representation of the dependency graph for Terraform resources |
terraform graph -draw-cycles | Generates a visual representation of the dependency graph, highlighting any cycles present within the infrastructure configuration. |
Exploring Terraform Configuration with terraform console
terraform console | Opens an interactive shell for evaluating expressions and testing |
Directory-Based Terraform Configuration Application
terraform -chdir=”../stage” apply | Applies Terraform configurations located in the “../stage” directory. |
Enhancing Command-Line Efficiency with Terraform Autocompletion
terraform -install-autocomplete | Enables shell autocompletion for Terraform commands |
To Debug terraform
terraform -debug <command> | ‘-debug’ flag along with the specific Terraform command you want to debug. |
Terraform Cheat Sheet for AWS (Amazon Web Services)
An AWS Terraform configuration file allows the easy provisioning of cloud infrastructure such as EC2 and S3 buckets. In the case of AWS EC2, this file sets the AWS provider, the ID of an AMI, the type of instance, and, optionally, such elements as security groups. The configuration process automates deployment, as each deployment will be consistent and have fewer manual errors. In the case of AWS S3, the provider, bucket name, and region are provided, and other settings like versioning, encryption, and access control are optional.
Terraform is an efficient way to automate and scale cloud storage management. You can use this Terraform cheat sheet when creating EC2 and S3 resources reliably by visiting the links on the configuration pages selected.
Terraform Cheat Sheet for Azure
The Azure Terraform configuration file simplifies cloud provider deployment, including Virtual Machines and Storage Accounts. In the case of an Azure VM, the file defines the Azure provider, region, authentication credentials, VM size, image, and networking. The configuration automates the setup of VM, standardized and scalable infrastructure runs the configuration.
Azure storage configuration will specify the provider, authentication, region, and some storage account properties such as replication, access tier, and encryption. Such automation increases cloud efficiency and reliability. Use this Terraform cheat sheet to deploy and maintain Azure VM and Storage services with ease, using the examples shown in the configuration.
Frequently Asked Questions
Q1. What is the first Terraform Command?
The terraform init command creates an operational setting with configuration files in Terraform. It is the initial command you ought to execute following the establishment of a fresh configuration in Terraform or the reproduction of an in situ configuration out of version control.
Q2. What is Terraform Refresh?
Terraform refresh is mainly used to identify the occurrence of any drift between the actual state and the want-to-be state of the resources, which was stipulated in the configuration files. This drift may happen through manual changes to the infrastructure not done in Terraform or changes in the infrastructure environment itself.
Q3. How do I check Terraform syntax?
Make your way to the root in your module folder and just issue: terraform validate. This will give you the right syntax for your module.
Q4. What is my Terraform version Command?
To access the version of Terraform that is installed on your system, you can click on a command prompt or terminal, then use the command terraform –version.
Conclusion
Mastering Terraform commands is key to efficient infrastructure management, and this Terraform cheat sheet serves as a quick reference for all essential operations. Since you would like to init your schedule and code models and similarly, implement changes and command conditions, the entire commands are contained as a component of your infrastructure process. Whether you’re a beginner or a seasoned DevOps engineer, understanding these Terraform commands ensures faster deployment, improved code quality, and reduced risk. If you’re looking to take your skills further, a Terraform and Ansible course can help solidify your understanding and boost your confidence.
Now bookmark this page and keep it in your favorites as a Terraform cheat sheet so that you are never again unproductive and uncomfortable while writing infrastructure as code. When your fingertips have the right commands, you can automate, scale, and secure your cloud environments without getting anxious.