Commonly Used Terraform Commands With Explanation

Terraform is widely used Infrastructure as Code tools, used for provisioning the cloud infrastructure.

This blog will capture the commonly used Terraform CLI commands to perform plan, deploy and clean-up infrastructure and more.

Terraform Commands Usage Explanation
terraform version To check the current version of Terraform installed and notifies if installed version is outdated.
terraform init To prepare the working directory for use with Terraform. The terraform init command initialized the backend and based on the configuration files, it’s determined which provider and modules it needs to pull down from the registry to allow configuration to work properly.
terraform init ­input=false To prepare working directory for terraform without asking Input.
terraform validate To checks whether a configuration file is syntactically correct. It can check various aspects like unsupported arguments, un-declared variables, and others. terraform init should be run before this command.
terraform plan To examine the .tf files we have provided an execution plan and a summary of the resources that will be created, changed or destroyed. This is something will show that what are changes we are deploying.
terraform plan -out=path To save terraform plan to a specific path. This plan can then be used with terraform apply to be certain that only the changes shown in this plan are applied.
terraform apply  To create/update infrastructure based on the configuration files to a provider. By default, it won’t apply the changes until we confirming as yes.
terraform apply -auto-approve To apply changes without being promoted to enter yes. This will useful when we need to deploy automatically via pipeline.
terraform apply -lock=ture To lock the state files so that it can’t be modified by any other terraform apply.
terraform destroy To destroy/clean-up the deployment based on the configuration files to a provider.
terraform destroy -auto-approve To destroy/clean-up the deployment based on the configuration files to a provider without being promoted to enter yes. This will useful when we need to deploy automatically via pipeline.
terraform fmt To format/rewrite configuration files using the HCL canonical standard to take care of overall formatting.
terraform output To extract the value of an output variable from the state file.

NOTE

Terraform commands are not limited to this. This blog will be updated periodically to include more.

Happy Learning!