Terraform (Series-4)


  1. Backends in terraform
  2. Terraform State file
  3. Introduction to Terraform commands or Terraform CLI
  4. Quick Glance of Terraform CLI Commands


Terraform backend is basically a location where terraform state file resides. This state file has all the resource details and tracking which were provisioned or will be provisioned via terraform plan or apply command.

There are two types of Backend one is local that resides where you run terraform from it could be Linux machine, windows machine or wherever you run it from and other is remote backend which is like a URL or may be a different storage location like S3 bucket.

# Local Backend

terraform {
  backend "local" {
    path = "relative/path/to/terraform.tfstate"

# Remote Backend S3

terraform {
  backend "s3" {
    bucket = "mybucket"
    key    = "path/to/my/key"
    region = "us-east-2"

Terraform State file

  • Main function of terraform state is to store bindings between objects in remote system and defined in your configuration.
  • State file is stored by default with terraform.tfstate name. You can modify the state file using terraform CLI.
  • Make sure when you modify the terraform state file your bindings are not disturbed.
  • States are stored in JSON formats.
  • When you run terraform show or terraform output command it fetches the output in JSON format either from output of Root module or child module or terraform state file.
  • You can manually pull and push your terraform.state file but we should generally avoid this practice.
  • You can Bring other resources i.e import existing infrastructure which you have created by other means in state file.
  • While writing anything on resource your state file gets Lock and that helps it getting corrupted if any other team member writes the same resource. You can use force-unlock but that’s not recommended..
  • You can store your remote state file in S3, Terraform Cloud, Hasicorp consul, Google cloud storage, Azure blob storage etc.

Introduction to Terraform Commands or Terraform CLI

  1. terraform init: It initialize the provider , module version requirements and backend configurations.
    • terraform init -input=true – You can give inputs else terraform will fail.
    • terraform init -lock=false – Disable lock of state file (Not recommended)
    • terraform init -upgrade – Upgrade modules and plugins
  2. terraform get: It is only to initialize or download the modules only.
  3. terraform plan: Determines the state of all resources it declares 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 – Only summary of warnings
    • terraform plan -out=path Saves the execution Plan
    • terraform plan -var-file= abc.tfvars use specfic terraform.tfvars
  4. terraform apply: To apply changes in specific providers account
    • terraform apply -backup=path – To Backup
    • terraform apply -lock=true – Locks the state file
    • terraform apply -state=path – Path to state file
    • terraform apply -var-file= abc.tfvars
    • terraform apply -auto-approve
  5. terraform destroy: It will destroy terraform managed infra
    • terraform destroy -auto-approve
  6. terraform console: Provides console for evaluating expressions
    • terraform console -state=path – Path to local state file
  7. terraform fmt: Formats the configuration file to proper format
    • terraform fmt -check – Checks the input format
    • terraform fmt – recursive – It formats subdirectories as well
    • terraform fmt – diff – displays the difference
  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: It lists all the resources present in state file
    • terraform state list – id=id ( Id of resource )
    • terraform state list -state=path ( Path of the state file )
  12. terraform state show: It show attributes of specific resources.
    • terraform state show -state=path
  13. terraform import : It will import existing resource in terraform
  14. terraform refresh: It will reconcile the terraform state file. Whatever resource you created using terraform and if they are manually or by any means modified , refresh will sync them in state file.
  15. terraform rm : Removes the resources from state file
  16. terraform mv : Move the resources from state file
  17. terraform state pull: Manually download and output the state from remote state
  18. terraform state push: Manually upload the local state file to remote state

Quick Glance of Terraform CLI Commands

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

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