Terraform Commands Ultimate Cheat Sheet

Hello Everyone, welcome back to our blog. Today we are going to discuss some of the very cool terraform commands. These commands are very handy for day to day operations. Let’s just start our topic Terraform Commands Ultimate Cheat Sheet.

About Terraform CLI

Terraform, a tool created by HashiCorp in 2014, written in Go language, aims to build, change and version control your infrastructure. This tool have a powerful and very intuitive Command Line Interface.

Installation Cheat Sheet

Install through Curl

$ curl -O https://releases.hashicorp.com/terraform/
0.11.10/terraform_0.11.10_linux_amd64.zip
$ sudo unzip terraform_0.11.10_linux_amd64.zip
 -d /usr/local/bin/
$ rm terraform_0.11.10_linux_amd64.zip

Install through tfenv: a Terraform version manager

First of all, download the tfenv binary and put it in your PATH.

$ git clone https://github.com/Zordrak/tfenv.git
 ~/.tfenv
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"'
 >> $HOME/bashrc

Then, you can install desired version of terraform:

$ tfenv install 0.11.10

Usage

Show version

$ terraform --version

Initialize Terraform

$ terraform init

It’s the first command you need to execute. Unless, terraform plan, apply, destroy and import will not work. Terraform init will install:

  • terraform modules
  • eventually a backend
  • and provider(s) plugins

Init Terraform and don’t ask any input:

$ terraform init -input=false

Change backend configuration during the init:

$ terraform init -backend-config=cfg/s3.dev.tf -reconfigure

-reconfigure is used in order to tell terraform to not copy the existing state to the new remote state location.

Get

This command is useful when you have defined some modules Modules are vendored so when you edit them, you need to get again modules content.

$ terraform get -update=true

When you use modules, the first thing you’ll have to do a terraform get. This pulls modules into the .terraform directory. Once you do that, unless you do another terraform get -update=true, you’ve essentially vendored those modules.

Plan

The plan step check configuration to execute and write a plan to apply to target infrastructure provider.

$ terraform plan -out plan.out

When you execute terraform plan command, terraform will scan all *.tf files in your directory and create the plan.

Apply

Now you have the desired state so you can execute the plan.

$ terraform apply plan.out

Apply and auto approve:

$ terraform apply -auto-approve

Apply and define new variables value:

$ terraform apply -auto-approve
 -var tags-repositor_url=${GIT_URL}

Apply only one module:

$ terraform apply -target=module.s3

This -target option works with terraform plan too.

Destroy

$ terraform destroy

Delete all the resources! A deletion plan can be created before:

$ terraform plan –destroy

-target option allow to destroy only one resource, for example a S3 bucket :

$ terraform destroy -target aws_s3_bucket.my_bucket

Debug

The Terraform console command is useful for testing interpolations before using them in configurations. Terraform console will read configured state even it is remote.

$ echo "aws_iam_user.notif.arn" | terraform console
 arn:aws:iam::123456789:user/notif

Graph

$ terraform graph | dot –Tpng > graph.png

Visual dependency graph of terraform resources.

Validate

Validate command is used to validate/check the syntax of the Terraform files.

terraform validate

Providers

You can use a lot of providers/plugins in your terraform definition resources, so it can be useful to have a tree of providers used by modules in your project.

$ terraform providers
terraform providers command output

State

Pull remote state in a local copy

$ terraform state pull > terraform.tfstate

Push state in remote backend storage

terraform state push

This command is useful if for example you originally use a local tfstate and then you define a backend storage, in S3 or consul.

How to tell to Terraform you moved a resource in a module?

If you moved an existing resource in a module, you need to update
the state:

$ terraform state mv aws_iam_role.role1 module.mymodul

How to import existing resource in Terraform?

If you have an existing resource in your infrastructure provider, you can import it in your Terraform state:

$ terraform import aws_iam_policy.elastic_post
 arn:aws:iam::123456789:policy/elastic_post

Workspaces

To manage multiple distinct sets of infrastructure resources/environments. Instead of create a directory for each environment to manage, we need to just create needed workspace and use them:

Create Workspace:

This command create a new workspace and then select it

$ terraform workspace new dev

Select a workspace:

$ terraform workspace select dev

List workspaces:

$ terraform workspace list
  default
* dev
  prelive

Show current workspace:

$ terraform workspace show

Hope you will find these terraform commands handy and useful in your day to day work. This was all about our today’s topic Terraform Commands Ultimate Cheat Sheet. We will keep on adding more commands in the list. Stay Tuned.

Terraform official site

Check out our other popular blogs:

Related Keywords:

terraform commands, terraform commands for azure, terraform commands for aws, terraform commands for gcp, terraform commands for oracle cloud, terraform commands interview questions, terraform commands example, what is terraform.

Leave a Comment