important terraform commands

7 important terraform commands

Last Updated on February 18, 2023 by cscontents

Introduction

In this article we will see the basic terraform commands which we use always while using terraform.

To get a high level understanding of terraform you can have a look at the below article.

Introduction to terraform – high level information

Terraform workflow

Before going to the commands let’s understand the workflow.

Below is a simple diagram which is self-explanatory.

terraform flow

terraform commands

There are many terraform commands, but among them few are important.

  1. terraform init
  2. terraform validate
  3. terraform plan
  4. terraform apply
  5. terraform destroy
  6. terraform import
  7. terraform out

Assuming we have the terraform files (.tf files) we need to run the below commands.

terraform init

This is the first command we need to run after writing new terraform configuration files, also if we clone any repository (of .tf files) from version control system then also we need to run this command.

This command initialize the working directory where .tf files exists.

terraform validate (optional)

This command will validate the code written in our terraform files. It is better to validate our code before we move to the next step.

terraform plan

This command shows us the plan. Here plan is basically the changes what terraform will perform on the infrastructure.

Basically, whatever we write or code in the terraform files (which is declarative) it will be compared with the existing state of the infrastructure (which is maintained in .tfstate file) and the result of this comparison will be shown to us.

  • If we add a new resource in the .tf file, then ‘terraform plan’ command will show that in the plan.
  • If we don’t do any changes in the .tf files and run the ‘terraform plan’ command will show nothing or ‘no changes to be done’.
  • At the very beginning when no infrastructure exist and when we have written new terraform files, then if we run the ‘terraform plan’ command it will show all the resources which will be deployed.

terraform apply

This command will implement the plan shown by ‘terraform plan’ command. This command uses relevant API’s to implement the plan or changes.

When we run this command it asks for approval, so we need to approve it. If we want to make it auto approve then we need to run the below command.

terraform apply --auto-approve

terraform destroy

This command is used to destroy or delete the resources created by terraform. This command will ask for approval, and we need to approve it. If we want to auto approve it then we need to run below command.

terraform destroy --auto-approve

 

Till now, we have discussed mostly used basic terraform commands. Below are few more commands.

terraform import

This command is used in the below use cases –

  • To update the terraform state file (.tfstate file) if there is any difference between real-world infrastructure and terraform state file.
  • If we lost our state file, then we can rebuild it using ‘terraform import’ command.

terraform out

This command is used to print the value of output variable. If any output variable is defined then this command can be used to print its value.

 

Thank You.

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.