Terraform Cheatsheet

Terraform CLI commands

Install terraform on Ubuntu

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

wget -O- https://apt.releases.hashicorp.com/gpg | \
    gpg --dearmor | \
    sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
    https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
    sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update && sudo apt-get install terraform && terraform version

Refer below link for other OS installations

Check Version

terraform version


terraform -h
terraform --help


terraform workspace new <workspace_name> #create a new workspace
terraform workspace select default #change to the selected workspace
terraform workspace list #list out all workspaces

Initialise your Terraform working directory

terraform init #initialise directory, pull down providers
terraform init -get-plugins=false #initialise directory, do not download plugins
terraform init -verify-plugins=false #initialise directory, do not verify plugins for Hashicorp signature

Format your code in configuration files/tf files

terraform fmt #format your configuration files using the HCL standard
terraform fmt --recursive #formats files in subdirectories
terraform fmt --diff #display differences between original configuration files and formatting changes.

Validate terraform code

terraform validate #syntax check on your code
terraform validate -backend=false #skip backend validation

Plan your real world infrastructure

terraform plan #will generate an execution plan of your infrastructure showing you what actions will be taken without actually performing the planned actions.
terraform plan -out plan.out #output the plan to plan.out
terraform plan -out=/home/$USER/ #save the plan file to a given path.
terraform plan -destroy #display a destroy plan

Apply infrastructure

terraform apply
terraform apply --auto-approve #apply changes without being prompted to enter “yes”
terraform apply plan.out #use the plan.out plan file to deploy infrastructure

More to write....