#Initializes the working directory that contains our terraform code
$ terraform init
** Generally it downloads the modules and plugins and sets up the backend for storing terraform state file (by which terraform keeps tracks of resource).
#to check what changes will take place once terraform is applied
$ terraform plan
Terraform plan generally reads the code & then creates and shows a "plan" of execution/deployment.
Note:- This command doesn't deploy anything, only shows what changes will be done after running "apply"
#it deploys the instructions & statements in the code
$ terraform apply
** with every "terraform apply" the "state file" is updated every time
#Apply changes without being prompted to enter "yes"
$ terraform apply --auto-approve
#Destroys all resource which is recorded inside state file
$terraform destroy
**Should be used with caution as it is non-reversible command, it is best practise to always take the backup before running this command.
#destroy/cleanup deployment without being prompted to enter "yes"
$ terraform destroy --auto-approve
#only initializes the directory but not downloads the plugin for specific provider
$ terraform init -get-plugin=false
#Shows the terraform version which is running
$ terraform version
#Download and update modules in the "root" module
$ terraform get -update=true
#pass the deployment plan to test.out
$ terraform plan -out test.out
#Outputs a destroy plan
$ terraform plan -destroy
#Only apply changes to the target resource
$ terraform apply -target=aws_instance.my_ec2
#Override a variable or pass a variable at run time
$ terraform apply -var my_region_variable=us-east-1
#Lock the remote state file (possible only where backend allows locking eg. aws s3 bucket)
$ terraform apply -lock=true
#Number of resource which can run simultaneously
$ terraform apply --parallelism=4
#Provides information of providers which is used in current configuration
$ terraform providers
#Creates a new workspace
$ terraform workspace new mytestworkspace
#Change to the selected workspace
$ terraform workspace select mytestworkspace
#List out all workspaces
$ terraform workspace list
#Show the terraform state information in human readable format
$ terraform show
#show details stored in terraform state for the mentioned resource
$ terraform state show aws_instance.my_ec2
#download and output remote state to a file
$ terraform state pull > terraform.tfstate
#move a resource tracked via state to different/custom module
$ terraform state mv aws_instance.my_ec2 module.custom_module
#list all the resources tracked in current state file
$ terraform state list
#Unmanage a resource & delete it from terraform state file
$ terraform state rm aws_instance.my_ec2
#to check if the indentation of the code is well arranged or not as per HCL standard
$ terraform fmt
#validate code for syntax
$ terraform validate
#skip the backend validation
$ terraform validate -backend=false
#Taint resources which needs to be recreated in next apply
$ terraform taint aws_instance.my_ec2
#Remove taint for ny tainted resource
$ terraform untaint aws_instance.my_ec2
#Force unlock any locked state file
$ terraform force-unlock LOCK_ID
#Obtain and save API token for terraform cloud
$ terraform login
#Logout of terraform cloud
$ terraform logout
TIPS:- Setup the autocompletion so that after pressing the tab key we can get all options which we can use with terraform and instead of witing it manually autocompletion will write it for us.
$ terraform -install-autocomplete
0 Comments