Contents

A cheat sheet for Terraform CLI

This is a cheat sheet for Terraform.

Terraform CLI Bash

Setup tab auto-completion, requires logging back in

1
terraform -install-autocomplete

Format and Validate Terraform Code

Format code

1
terraform fmt

Validate code for synthax errors

1
terraform validate

Validate code without checking backend validation

1
terraform validate -backend=false

Terraform Init

Initialize Terraform in current directory, pull down providers

1
terraform init

Initialize Terraform in current directory, do not pull down providers

1
terraform init -get-plugins=false

Initialize Terraform in current directory, do not verify Hashicorp signature

1
terraform init -verify-plugins=false

Plan, Apply and Utilities

Apply changes without being prompted to enter “yes”

1
terraform apply --auto-approve

Destroy infrastructure without being prompted to enter “yes”

1
terraform destroy --auto-approve

Output the plan to a file : plan.out

1
terraform apply plan.out

Plan a destroy

1
terraform plan -destroy

Apply on a target resource

1
terraform apply -target=aws_instance.my_ec2

Variables into command

1
terraform apply -var my_region_variable=eu-central-1

Lock : the state file so it can’t be modified by any other Terraform apply or modification action (possible only where backend allows locking)

1
terraform apply -lock=true

Refresh : Do not reconcile state file with real-world resources(helpful with large complex deployments for saving deployment time)

1
terraform apply refresh=false

Refresh : Reconcile the state in Terraform state file with real-world resources

1
terraform refresh

Parallelism : Number of simultaneous resource operations

1
terraform apply --parallelism=5

Providers : get informations about the current providers

1
terraform providers

Terraform Workspaces

Create a new workspace

1
terraform workspace new mynewworkspace

Switch to another workspace

1
terraform workspace select anotherworkspace

List all workspaces

1
terraform workspace list

Terraform State

Show details stored in Terraform state for the resource

1
terraform state show aws_instance.my_ec2

Pull terraform state to a file

1
terraform state pull > terraform.tfstate

Move a resource tracked via state to different module

1
terraform state mv aws_iam_role.my_ssm_role module.custom_module

Replace an existing provider with another

1
terraform state replace-provider hashicorp/aws registry.custom.com/aws

List out all the resources tracked via the current state file

1
terraform state list

Remove a resource from the state, it doesn’t delete the resource

1
terraform state rm  aws_instance.myinstace

Terraform Import

Import EC2 instance

1
terraform import aws_instance.new_ec2_instance i-abcd1234

Import EC2 instance with a count

1
terraform import 'aws_instance.new_ec2_instance[0]' i-abcd1234

Terraform Outputs

List all outputs as stated in code

1
terraform output

List out a specific declared output

1
terraform output instance_public_ip

List all outputs in JSON format

1
terraform output -json

Terraform Taint

Taint resource to be recreated on next apply

1
terraform taint aws_instance.my_ec2

Remove taint from a taint resource

1
terraform untaint aws_instance.my_ec2

Terraform Console

echo an expression into terraform console and see its expected result as output

1
echo 'join(",",["foo","bar"])' | terraform console

Terraform console also has an interactive CLI just enter “terraform console”

1
echo '1 + 5' | terraform console

Display the Public IP against the “my_ec2” Terraform resource as seen in the Terraform state file

1
echo "aws_instance.my_ec2.public_ip" | terraform console

Terraform Graph

Produce a PNG diagrams showing relationship and dependencies

1
terraform graph | dot -Tpng > graph.png

Terraform Cloud

Obtain and save API token for Terraform cloud

1
terraform login

Log out of Terraform Cloud, defaults to hostname app.terraform.io

1
terraform logout

Terraform Miscelleneous

Show version

1
terraform version

Download and update modules in the “root” module.

1
terraform get -update=true