Terraform Cheat Sheet and command line.

No comments

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 .

Link: 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 COMMANDS:

  1. 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
  2. terraform get: This command will initialize or download the modules only.
  3. 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
  4. 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.
  5. terraform destroy: It will destroy the terraform managed infrastructure.
    • terraform destroy -auto-approve # This command will not prompt to approve the destroy command.
  6. 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
  7. 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.
  8. terraform validates: It validates the configuration files
    • terraform validate -json : Output is in json format
  9. terraform graph: It Generates visual representation of execution plan
    • terraform graph -draw-cycles
    • terraform graph -type=plan
  10. terraform output : This is to extract the values of an output variable from state file.
    • terraform output -json
    • terraform output -state=path
  11. 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
  12. 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 .
  13. 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.
  14. 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.
  15. terraform rm : This command will remove the resources from the terraform state file
  16. terraform mv : This command moves the resources within the terraform state file from one location to another
  17. terraform state pull: This command will manually download the terraform state file from remote state in your local machine
  18. terraform state push: This command will manually upload the local state file to remote state

Quick Glance of Terraform CLI Commands

InitializeProvisionModify ConfigCheck infraManipulate State
terraform initterraform planterraform fmtterraform graphterraform state list
terraform getterraform applyterraform validateterraform outputterraform state show
terraform destroyterraform consoleterraform state showterraform state mv/rm
terraform state listterraform state pull/push

Conclusion:

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s