Terraform has been a hot topics in todays date and is used widely with all the organizations now. Terraform is a open source infrastructure as code tool which helps in provisioning of infrastructure and services on various cloud providers and third party vendors. This is a great tool and simple to learn. It has a very vast scope for DevOps and IT engineers.
If you wish to learn terraforms each component and topic which I have covered in terraform pocket guide .
In this tutorial I will only cover all the commands which will be used while using terraform. I am sure this tutorial is going to be very useful for all terraform beginners and experienced professionals.
- terraform init: This command initializes the providers , modules and backend configurations which are required to run the terraform.
- terraform init -input=true # If you wish to provide inputs use input as true else terraform will fail.
- terraform init -lock=false # This command will disable lock of terraform state file (Not recommended)
- terraform init -upgrade – # This command will upgrade your all modules and plugins required to run the terraform
- terraform get: This command will initialize or download the modules only.
- terraform plan: This command will provides you the state of all resources and and compares with real infrastructure. It uses terraform state file data to compare . It uses providers API to perform this step.
- terraform plan -compact-warnings # This will provide summary of warnings while running terraform plan
- terraform plan -out=path # This command will save the execution plan
- terraform plan -var-file= abc.tfvars # This will use specific terraform.tfvars which contains environment wise values
- terraform apply: To apply changes in specific providers account
- terraform apply -backup=path # This command will backup the terraform state file
- terraform apply -lock=true # This command will lock the terraform state file
- terraform apply -state=path # This command will prompt to provide the path of the terraform state file
- terraform apply -var-file= abc.tfvars # This command will allow you to enter the specific terraform.tfvars which contains environment wise variables.
- terraform apply -auto-approve # This command will not prompt to approve the apply command.
- terraform destroy: It will destroy the terraform managed infrastructure.
- terraform destroy -auto-approve # This command will not prompt to approve the destroy command.
- terraform console: This command will evaluate the expressions in configuration files.
- terraform console -state=path # This command will prompt to provide the path to local state file
- terraform fmt: This command will format the configuration file to a proper format
- terraform fmt -check # This command will check the input format
- terraform fmt – recursive # This command will format subdirectories as well
- terraform fmt – diff # This command will display the difference if any found in latest formatting with the previous ones.
- terraform validates: It validates the configuration files
- terraform validate -json : Output is in json format
- terraform graph: It Generates visual representation of execution plan
- terraform graph -draw-cycles
- terraform graph -type=plan
- terraform output : This is to extract the values of an output variable from state file.
- terraform output -json
- terraform output -state=path
- terraform state list: This command will list all the resources present in terraform state file
- terraform state list – id=id ( Id of resource ) # This command will search for particular resource using resource id in terraform state file.
- terraform state list -state=path # This command will prompt you to provide the path of state file and then provides the list of all resources in terraform state file
- terraform state show: This command shows the attributes of specific resources .
- terraform state show -state=path # This command will prompt you to provide the path and then provide the attributes of specific resources .
- terraform import : This command will import existing resource from infrastructure which was not created using terraform but will be imported in terraform state file and will be included in terraform next time we run it.
- terraform refresh: This command will reconcile the terraform state file. Whatever resource you created using terraform and if they got modified or updated by any other way such as CLI or using console UI , refresh will sync them in terraform state file.
- terraform rm : This command will remove the resources from the terraform state file
- terraform mv : This command moves the resources within the terraform state file from one location to another
- terraform state pull: This command will manually download the terraform state file from remote state in your local machine
- terraform state push: This command will manually upload the local state file to remote state
Quick Glance of Terraform CLI Commands
|Initialize||Provision||Modify Config||Check infra||Manipulate State|
|terraform init||terraform plan||terraform fmt||terraform graph||terraform state list|
|terraform get||terraform apply||terraform validate||terraform output||terraform state show|
|terraform destroy||terraform console||terraform state show||terraform state mv/rm|
|terraform state list||terraform state pull/push|
In this tutorial we covered terraform commands which are very useful for beginners and experienced professionals. I am sure the summary of terraform commands listed in this tutorial is going to be of massive use.
Keep Terraforming !! Please share with your friends if you like it.