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 commands
There are many terraform commands, but among them few are important.
- terraform init
- terraform validate
- terraform plan
- terraform apply
- terraform destroy
- terraform import
- 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.
- Kubernetes Series: Part 1 – Introduction to Kubernetes | Background of Kubernetes
- Kubernetes Series: Part 2 – Components of Kubernetes cluster | Kubernetes cluster in detail
- Kubernetes Series: Part 3 – What is Minikube and How to create a Kubernetes cluster (on Linux) using Minikube?
- Introduction to Ansible | High Level Understanding of Ansible
- Basics of automation using Ansible | Automate any task
- Automation of Java installation – using Ansible
- Automation of JBoss EAP installation – using ansible
- Jenkins Pipeline as code – High level information
- Jenkins pipeline script to build Java application and push artifacts into repository
- Jenkins pipeline script to build & deploy application on web server
- What is End-to-End Monitoring of any web application, and Why do we need it?
- What is “Monitoring” in DevOps? Why do we need to Monitor App/DB servers, Transactions etc.?
- DevOps Engineer or Software Developer Engineer which is better for you?- Let’s discuss
- How To Be A Good DevOps Engineer?
- How to do git push, git pull, git add, git commit etc. with Bitbucket