Post

Terraform Cheat Sheet

Helpful Terraform commands to reference as a cheat sheet

Terraform Cheat Sheet

Format and Validate

CommandDescription
terraform fmtReformat your configuration in the standard style
terraform validateCheck whether the configuration is valid

Initialize Working Directory

CommandDescription
terraform initPrepare your working directory for other commands

Plan, Deploy and Cleanup

CommandDescription
terraform apply --auto-approveCreate or update infrastructure without confirmation prompt
terraform destroy --auto-approveDestroy previously-created infrastructure without confirmation prompt
terraform plan -out plan.outOutput the deployment plan to plan.out
terraform apply plan.outUse the plan.out to deploy infrastructure
terraform plan -destroyOutputs a destroy plan
terraform apply -target=aws_instance.myinstanceOnly apply/deploy changes to targeted resource
terraform apply -var myregion=us-east-1Pass a variable via CLI while applying a configuration
terraform apply -lock=trueLock the state file so it can’t be modified
terraform apply refresh=falseDo not reconcile state file with real-world resources
terraform apply --parallelism=5Number of simultaneous resource operations
terraform refreshReconcile the state in Terraform state file with real-world resources
terraform providersGet informatino about providers used in the current configuration

Workspaces

CommandDescription
terraform workspace new <workspace>Create a new workspace
terraform workspace select defaultChange to a workspace
terraform workspace listList all workspaces

State Manipulation

CommandDescription
terraform state show aws_instance.myinstanceShow details stored in the Terraform state file
terraform state pull > terraform.tfstateOutput Terraform state to a file
terraform state mv aws_iam_role.my_ssm_role module.mymoduleMove a resource tracked via state to different module
terraform state replace-provider hashicorp/aws registry.custom.com/awsReplace an existing provider with another
terraform state listList all resources tracked in the Terraform state file
terraform state rm aws_instance.myinstanceUnmanage a resource, delete it from the Terraform state file

Import and Outputs

CommandDescription
terraform import <resource_type>.<resource> <id>Import a Resource
terraform outputList all outputs
terraform output <output>List a specific output
terraform output -jsonList all outputs in JSON format

Terraform Cloud

CommandDescription
terraform loginLogin to Terraform Cloud with an API token
terraform logoutLogout from Terraform Cloud
This post is licensed under CC BY 4.0 by the author.