Skip to content

devops-cheat-sheets/terraform-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Terraform Cheat Sheet

Creating a cheat sheet of 100 commands for Terraform would be quite extensive and perhaps overwhelming. Instead, I'll provide you with a categorised cheat sheet of the most important commands. However, if you require additional or specific commands, feel free to ask.

Terraform Command Cheat Sheet

Initialization

Command Explanation
terraform init This command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It's safe to run this command multiple times.

Planning

Command Explanation
terraform plan This command creates an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.
terraform plan -out=path This command writes the plan to the given path. This can be used as input to the "apply" command.
terraform plan -destroy This is used when the plan should be created to destroy all resources.

Applying

Command Explanation
terraform apply This command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.
terraform apply -auto-approve This skips interactive approval before applying the plan.

Destroy

Command Explanation
terraform destroy This command is used to destroy the Terraform-managed infrastructure.

Other Commands

Command Explanation
terraform get This command downloads and updates modules mentioned in the root module.
terraform fmt This command is used to rewrite Terraform configuration files to a canonical format and style.
terraform validate This command validates the Terraform configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc.
terraform console This command provides an interactive console for evaluating expressions.
terraform graph This command is used to generate a visual representation of either a configuration or execution plan.
terraform import This command is used to import existing infrastructure into your Terraform state.
terraform output This command is used to extract the value of an output variable from the Terraform state.
terraform refresh This command is used to update the Terraform state file with real-world resources.
terraform state This command is used for advanced state management.
terraform taint This command manually marks a Terraform-managed resource as tainted, forcing it to be destroyed and recreated on the next apply.
terraform untaint This command manually unmarks a Terraform-managed resource as tainted.
terraform workspace This command is used for workspace management. Workspace allow you to maintain separate state files for the same configuration providing a simple way to create environments (e.g. dev, staging, prod).

Sure! Here are more commands that could be helpful to use with Terraform.

Terraform Variables and Output

Command Explanation
terraform apply -var 'variable=value' Allows you to set a variable directly from the command line.
terraform apply -var-file=foo Use this command to provide a variable file to Terraform.
terraform output -json To view the output in a JSON format.

Terraform State Management

Command Explanation
terraform state list This command lists resources within a Terraform state.
terraform state show [resource_name] This command shows the details of a specific resource in the Terraform state.
terraform state mv [source] [destination] This command is used to move items in the state.
terraform state rm [resource_name] This command is used to remove items from the Terraform state file.
terraform state pull This command is used to manually download and output the state from remote state.
terraform state push This command is used to manually upload a local state file to remote state.

Terraform Workspace

Command Explanation
terraform workspace new [workspace] This command is used to create a new workspace.
terraform workspace select [workspace] This command is used to switch between different workspaces.
terraform workspace list This command is used to list out all the workspaces.
terraform workspace show This command is used to display the current workspace.
terraform workspace delete [workspace] This command is used to delete a workspace.

Terraform Import

Command Explanation
terraform import [arguments] [address] This command is used to import existing infrastructure into Terraform.

Absolutely! Continuing with more commands, especially focusing on modules and providers.

Terraform Module and Providers

Command Explanation
terraform providers This command is used to print a tree of the providers used in the configuration.
terraform providers schema -json This command is used to output the JSON representation of the schemas for the providers used in the configuration.
terraform providers mirror [directory] This command is used to mirror the providers used in the configuration to a local directory.

Terraform Debugging and Troubleshooting

Command Explanation
TF_LOG=TRACE terraform apply This is used to debug the Terraform application. You can replace TRACE with DEBUG, INFO, WARN, or ERROR to change the verbosity of the logs.
TF_LOG_PATH=path/to/logfile terraform apply This command is used to store the debug log in a file instead of printing it in the console.

Terraform Format and Validation

Command Explanation
terraform fmt -check This command is used to check if files are correctly formatted. It will not rewrite the files.
terraform fmt -diff This command will display a diff of format changes.
terraform validate -json This command will validate the configuration and output the results in JSON format.

Terraform Cloud

Command Explanation
terraform login This command is used to obtain and save an API token for Terraform Cloud or Terraform Enterprise.
terraform logout This command removes locally-stored credentials for Terraform Cloud or Terraform Enterprise.

Terraform Misc Commands

Command Explanation
terraform version This command is used to print the Terraform version.
terraform force-unlock LOCK_ID [DIR] This command manually unlocks the state for the defined configuration. This will not modify your infrastructure.
terraform 0.12upgrade This command is used to upgrade Terraform syntax to the 0.12 version.
terraform -install-autocomplete This command installs the shell autocomplete feature.

These additional commands should provide a more holistic view of Terraform's capabilities. As always, consult the official documentation for the most accurate information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published