Show Menu

Terraform CLI cheat sheet

Code Syntax

terraform fmt
Formats Terraform config­uration files to a canonical format.
terraform validate
Validates the syntax and config­uration of Terraform files.

Initia­lizing Terraform

terraform init
Initia­lizes the working directory containing Terraform config­uration files.
terraform init -upgrade
Updates provider plugins to the latest versions.

Provis­ioning Infras­tru­cture

terraform plan
Generates an execution plan showing what Terraform will do.
terraform apply
Applies the changes required to reach the desired state.
terraform destroy
Destroy all remote objects managed.


terraform version
Displays the Terraform version inform­ation.
terraform console
Opens an intera­ctive console to evaluate Terraform expres­sions.
terraform graph
Generates a visual repres­ent­ation of Terraform resources and depend­encies.

Azure Service Principal

provider "azurerm" {
  features {}
  subscription_id = "<subscription_id>"
  client_id       = "<application_id>"
  client_secret   = "<secret_value>"
  tenant_id       = "<tenant_id>"

Azure Backend

terraform {
  backend "azurerm" {
    resource_group_name   = "<resource_group_name>"
    storage_account_name  = "<storage_account_name>"
    container_name        = "<container_name>"
    key                   = "<state_file_key>"
    access_key            = "<storage_account_access_key>"

Azure Required Provider

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> x.xx.x "
Valid Operators:
= : Allows only one exact version number.
!= : Excludes an exact version number.
>, >=, <, <=: Compar­isons against a specified version.
~>: Allows only the rightmost version component to increment.

Terraform State

terraform state list
Lists all resources in the Terraform state.
terraform state show <re­sou­rce>
Shows the attributes of a specific resource in the Terraform state.
terraform state rm <re­sou­rce>
Removes a resource from the Terraform state.
terraform state mv <ol­d-r­eso­urc­e> <ne­w-r­eso­urc­e>
Moves a resource within the Terraform state.

Terraform State Backend

terraform init -backe­nd-­con­fig­=<c­onf­ig>
Initia­lizes Terraform with a specific backend config­ura­tion.
terraform remote config
Configures remote state storage.
terraform remote pull
Retrieves the current state from the remote backend.
terraform remote push
Uploads the local state to the remote backend.

Comman­d-Line Flags

Opt to upgrade modules and plugins
Disregards any existing config­uration and state.
Ask for input if necessary. If false, will error if input was required.
Disable text coloring & formatting in the output.
-var 'NAME=­VALUE'
Sets a value for a single input variable.
Sets values for variables as TFVARS.
Focus its efforts only on specific resource instance.

Resource Manipu­lation

moved {
  from = azurerm_resource_group.example.rg01
  to   = azurerm_resource_group.example.rg02

import {
  to = azurerm_resource_group.example.rg03
  id = /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>

removed {
  from = azurerm_resource_group.example.rg02

  lifecycle {
    destroy = false


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          DevOps Troubleshooting Study Guide Cheat Sheet
          JENKINS Cheat Sheet
          DevOps Cheat Sheet