Terraform Commands Cheat Sheet – The Only Commands You Need

Terraform commands cheat sheet.  Every problem has a solution, which means every complex code has a precise alternative command so that you don’t have to re-embark from the beginning. However, when it comes to detailed and less time-consuming tools, Terraform comes to our minds. Terraform has provided a facility of commands to their users to revise documentation whenever any new update arrives automatically.

Terraform is an open-source software developed by Hashicorps based on Hashicorp Configuration Language (HCL) compatible with JSON (understandable to human and machine language). Hashicorp has also developed Terraform Enterprise, their paid product, and provides Sentinel used in implementing Policy as Code. In addition, it offers a wide range of services such as AWS and external services.

Terraform permits the establishment of Infrastructure as Code (IaC) and uses infrastructure as code in managing, updating, and provisioning of infrastructure. In addition, it stores the history of alteration in an environment through terraform state management that can be helpful in long-term projects. Furthermore, Terraform stores the states in a remote computer in a JSON file. That file provides the map of detailed information of resources it is taking care of.

Terraform Best Practices For Secure Infrastructure Deployments

List of Terraform Commands Cheat Sheet

Terraform offers its users the facility of Commands. Commands are a component of Terraform Command Line Interface CLI. The CLI contains a vast range of sets of options and features. We call these commands cheats because of their ability to minimize workload. Moreover, it is comprehensible and succinctly declared. Therefore, it is more like a convenient way to achieve a goal without going through the whole process from the beginning.

In conclusion, it is more like a savior specializing in offering the best features. For your accessibility, we are mentioning the detailed list of Terraform commands cheat sheets.

Terraform destroy

Usage: Terraform destroy [options]

				
					terrafrom destroy [options]
				
			

The Terraform destroy command assists users in destroying infrastructure or all remote objects by simply using the kill command. Or it can be used to delete temporary things without touching the protracted/permanent. The Terraform destroy avails many of the options used by the Terraform apply such as, -auto-approve, -compact-warnings, -input=false, -JSON, -lock=false, -lock-timeout=DURATION, -no-color, and -parallelism=n (These options are more elaborated in the Terraform apply heading below) but the Terraform destroy command does not accept plan file argument and destroy planning mode.

There are two more ways to destroy or delete an infrastructure/data.

				
					terraform apply -destroy
				
			

By using terraform apply, you can accept/select options that need to be removed. 

				
					terraform plan -destroy
				
			

by using terraform plan command enables you to see the demo changes without altering or destroying a plan.

Terraform init

Usage: Terraform init [options] Terraform init [options]

				
					terraform init [options]
				
			

The Terraform init command has multi-purpose roles. It is used to refresh, update, initialize providers, and prepare directories.

You can use this command repetitively, and it will not cause any glitches to your data; instead, it will refresh and re-update the infrastructure. Moreover, this command is applied right after writing Terraform configuration for initializing the current working directory.

Terraform init options:

				
					-input=true
				
			

It is used for requesting input if it is required or will show an error if the information was needed. 

				
					-lock=false
				
			

It unlocks the state when any state operation is going on.

				
					-lock-timeout=
				
			

If another process has kept the lock, then it will generate an error. The default is zero seconds.

				
					-no-color
				
			

It will remove the color code in the command output.

				
					-upgrade
				
			

 It is used to boost the modules.

Terraform plan

Usage: Terraform plan [options]

				
					terraform plan [options]
				
			

The Terraform plan command delivers an execution plan that is more like a demo plan that shows the changes and steps that will occur while creating an infrastructure. The Plan consists of a few default steps; it checks whether the terraform state is up to date or not by analyzing the condition of remote objects, comparing the current state with the previous one, and preparing the alteration list that might be needed.

Furthermore, it has two modes:

  • terraform plan -destroy is used to clean all current remote objects.
  • terraform plan -refresh-only is used to update and refresh the states.

Terraform plan options:

				
					-refresh=false
				
			

Before testing for the configuration alterations, it prevents the automatic synchronizing of the terraform states with the virtual machine. It decreases the number of APIs and increases the speed of planning operations.

				
					-replace=ADDRESS
				
			

It assists in directing the terraform plan to supplant unit resource instance with the provided address. 

				
					-target=ADDRESS
				
			

It makes the terraform more attentive to its planning efforts on resource instances complimented to the provided address or the object on which instances have relied.

				
					-var 'NAME=VALUE'
				
			

Using this command, you can assign a value to a single input variable stated in the configuration’s root module.

				
					-var-file=FILENAME
				
			

Using this command, you can assign a value to many input variables, which is stated in the root module of the configurations.

				
					-compact-warnings
				
			

It provides the synopsis of the warning.

				
					-compact-warnings:
				
			

It provides the synopsis of the warning.

				
					-detailed-exitcode
				
			

It displays the comprehensive exit code when the command is left.  

				
					-input=false
				
			

It stops the terraform’s regime of prompting for input with no appointed value for root module input variables.

				
					-JSON
				
			

Allows the Json UI output.

				
					-lock=false
				
			

It unlocks the state during the operation.

				
					-lock-timeout=DURATION
				
			

It makes you regain the lock for a while before going back to an error if the -lock=false command is not applied. The default time is three seconds.

				
					-no-color
				
			

Halts the terminal formatting sequences in the result.

				
					-out=FILENAME
				
			

In an indirect file format, it delivers the plan to the given filename; then, it is forwarded to the terraform apply or other commands to run the plan changes.

Terraform apply

Usage: terraform apply [options] [plan file]

				
					terraform apply [options] [plan file]
				
			

The Terraform Apply command complies with the actions commanded by terraform plan. There are two ways to run terraform Apply; the first is to directly run it without adding a file that will bring up the new plan to implement. The second one is to add a file that will bring up the required changes.

In the default case, terraform develops its plan with no pre-saved file.

It also allows users to use destroy option; for example, Terraform apply -destroy; via this, you can select options that need to be removed.

Terraform apply options:

				
					-auto-approve
				
			

In advance, while applying a plan, it leaps an immersive consent. This option is not applicable on passing an already saved plan file.

				
					-compact-warnings
				
			

It generates a warning in the form of a message which helps debug the error.

				
					-input=false
				
			

All the terraform virtual prompts are deactivated by applying this command.

				
					-json
				
			

It will render the machine comprehensible Json UI output. 

				
					-lock=false
				
			

It is used to disable a lock during the operation.

				
					-lock-timeout=DURATION
				
			

It will automatically lock the state if the -lock=false is not implied.

				
					-no-color
				
			

It is used to delete the terminal formatting sequence in the result.

				
					-parallelism=n
				
			

All the synchronized operations are confined to default 10 when the terraform walks the graph.

Terraform refresh

Usage: Terraform refresh [options]

				
					terraform refresh [options]
				
			

The Terraform refresh command updates the terraform states by analyzing the contemporary terraform state. It only updates terraform state, not the remote object. The refresh option is also served by terraform apply and terraform plan. The Terraform refresh accepts all the options implemented by the Terraform apply (Options are mentioned in the above Terraform Apply heading)

 

There are two modes of Terraform refresh.

				
					terraform apply -refresh-only -auto-approve
				
			

Refreshes the states automatically.

				
					terraform apply -refresh
				
			

Only also refreshes the states. But before implementation, it previews the changes.

Terraform validate

Usage: Terraform validate [options]

				
					terraform validate [options]
				
			

The Terraform validate command is used to check the accuracy of the data in the directory. It verifies the revision of attribute names and value types. In addition, it also makes sure that a configuration is grammatically valid and uniform internally.

The command can go along with the following options:

				
					-json
				
			

By entering this, the result will be generated in machine-readable JSON format.

				
					-no-color
				
			

By entering this, the output will be colorless.

Terraform show

Usage: Terraform show [options] [file]

				
					terraform show [options] [file]
				
			

The Terraform show command provides you with a human-readable or understandable presentation from a state/plan file. You have to add any file or plan as an option; otherwise, It will show the newest state.

The command is compatible with the following options:

 

-json renders the machine-readable pattern from a state.

 

-no-color vanishes the colors from the output.

Terraform show options

				
					-json
				
			

Renders the machine-readable pattern from a state.

				
					-no-color
				
			

Vanishes the colors from the output.

Terraform fmt

Usage: Terraform fmt [options] [DIR]

				
					terraform fmt [options] [DIR]
				
			

The Terraform fmt command rephrases the file into the canonical format.

By default, fmt scrutinizes the recent directory for the configuration file. If the dir is declared, it will scan the given directory, or if only (-) is mentioned, it will analyze from the standard input (STDIN).

Terraform fmt options:

				
					-list=false
				
			

Removes the file having nonpersistent formatting.

				
					-write=false
				
			

Halts repetitive writing in the input file.

				
					-diff
				
			

Shows the changes in formating.

				
					-check
				
			

It is used to verify the formatting done in the input.

				
					-recursive
				
			

It is used to process files inside the directory.

Terraform state

Usage: Terraform state <subcommand> [options] [args]

				
					terraform state [options] [args]
				
			

The Terraform state command is used in organizing a state. This command does all the jobs remotely and retains the backup files. The backup file can be managed by mentioning -backup as an option. Terraform enforces state modification command to log reinforcement files.

Terraform state options:

				
					-backup
				
			

It is used to keep the backup files.

Terraform workspace

Usage: terraform workspace <subcommand> [options] [args]

				
					terraform workspace [options] [args]
				
			

The Terraform workspace command is used to manage and overlook the workspace. The configuration is assigned to a backend that deals with the execution of data, the place where the data is saved in the backend is called the workspace.

Terraform workspace options:

				
					terraform workspace new
				
			

It is used to create a new workspace.

				
					terraform workspace select
				
			

It allows you to switch workspaces.

Terraform version

Usage: The Terraform version comma

				
					terraform version [options]
				
			

The Terraform version command is used to showcase the version and installed plug-ins of the tool. By default, this command will provide the details of Terraform’s version, the platform on which it is installed, provider information, security verification, and a table of new updates.

However, if we use -json, it will show the version background written in the JSON format, excluding the information regarding security, provider information, and a list of upcoming new updates.

Terraform version options:

				
					-json
				
			

It will show the version background written in the JSON format, excluding the information regarding security, provider information, and a list of upcoming new updates.

Terraform Commands Cheat Sheets Conclusion

The extrapolation of this article is that every obstacle has a specific resolution method. Therefore, cheats have untangled the complexities occurring while coding. Because contemporary IT is bestowing you with simple techniques to accelerate your way to your destination without enduring any hindrance.

Hence terraform has provided tools, infrastructures, and commands which are assisting users in growing their capacity and ability to create/manage their models/infrastructure. The commands are just a gist of a huge backend code. The concise code means minimum effort and less time taking.

That’s why we have discussed the essential commands that will assist you while operating a Terraform state.

Avatar for Emad Bin Abid
Emad Bin Abid

I'm a software engineer who has a bright vision and a strong interest in designing and engineering software solutions. I readily understand that in today's agile world the development process has to be rapid, reusable, and scalable; hence it is extremely important to develop solutions that are well-designed and embody a well-thought-of architecture as the baseline. Apart from designing and developing business solutions, I'm a content writer who loves to document technical learnings and experiences so that peers in the same industry can also benefit from them.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x