0% found this document useful (0 votes)
458 views

Terraform Cheat SheetEdit Cheat Sheet

Terraform provides commands for managing infrastructure as code including plan, apply, destroy, refresh, and show. Workspaces allow managing multiple environments separately without affecting state. If state is lost, resources can be reconstructed using import. Drift between infrastructure and configuration is detected during plan and apply but not fully managed.

Uploaded by

Pankaj Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
458 views

Terraform Cheat SheetEdit Cheat Sheet

Terraform provides commands for managing infrastructure as code including plan, apply, destroy, refresh, and show. Workspaces allow managing multiple environments separately without affecting state. If state is lost, resources can be reconstructed using import. Drift between infrastructure and configuration is detected during plan and apply but not fully managed.

Uploaded by

Pankaj Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Terraform Cheat SheetEdit Cheat Sheet

CLI Commands
terraform plan # dry run
terraform apply
terraform refresh # sync state with remote resources
terraform show
terraform destroy

terraform validate # validate .tf file

terraform taint # mark resource for recreation


terraform untaint

terraform state push # e.g. force push state to S3 bucket


terraform state pull > terraform.tfstate # create a local state copy

Change verbosity by setting environment variable TF_LOG

export TF_LOG=INFO

Workspace Management
Terraform workspaces allow for the management of
two or more different environments i.e. Dev or Prod
separately without affecting the state of either
environment.
terraform workspace new dev

terraform workspace new test

terraform workspace new prod

terraform workspace select dev

terraform workspace select default

terraform workspace select prod

Recovering Lost State


One of the worst things that you happen is loosing the terraform state. In such a case you can
terraform import <address> <id>

# for example
terraform import aws_instance.myec2instance i-075c8d21cc91308dc

to let terraform reconstruct the resource state. Finally perform a

terraform state push

as import only imports into a local state file, even if you have an S3 bucket defined for keeping
state!

To avoid this use S3 bucket with versioning enabled for keeping state.

Drift Management
Terraform doesn't really do much drift management. Only some resource attributes are
checked. All detected drift is fixed by "apply".

Manually dump drift

terraform show >before


terraform refresh
terraform show >after

diff -u before after

Prevent auto-destroy:

lifecycle {
prevent_destroy = true
}

Bulk Imports
Check out https://github.com/jmcgill/formation

You might also like