As a developer or system administrator, you may have heard of Terraform, a popular infrastructure as code (IaC) tool that allows you to manage your cloud infrastructure using code. Terraform is a powerful tool that can help you automate your infrastructure deployment and management, but it can also be complex and overwhelming to use at times.
That’s where a Terraform cheat sheet comes in handy. A Terraform cheat sheet is a quick reference guide that provides you with the most commonly used Terraform commands, syntax, and best practices. It can help you save time and avoid mistakes by providing you with a quick and easy way to look up the commands and syntax you need.
This cheat sheet provides a quick reference for Terraform commands, syntax, and best practices.
Table of Contents
Installation
Command Description brew install terraformInstall Terraform using Homebrew on macOS choco install terraformInstall Terraform using Chocolatey on Windows terraform --versionCheck the installed version of Terraform
Configuration
Command Description terraform initInitialize a new Terraform project terraform planPreview the changes to be made by Terraform terraform applyApply the changes to the infrastructure terraform destroyDestroy the infrastructure created by Terraform terraform validateValidate the syntax of the Terraform configuration terraform fmtFormat the Terraform configuration for consistency terraform stateInspect and modify the Terraform state file
Commands
Command Description terraform workspaceManage multiple workspaces for a Terraform project terraform importImport existing infrastructure into Terraform terraform outputDisplay the output values of a Terraform configuration terraform graphGenerate a visual representation of the Terraform dependency graph terraform taintMark a resource as tainted, forcing it to be recreated terraform untaintRemove the tainted state from a resource terraform refreshRefresh the state of the Terraform-managed infrastructure
Resources
Command Description resourceDefine a new resource in the Terraform configuration dataDefine a data source to retrieve information from an external system localsDefine local variables within the Terraform configuration outputDefine output values to be displayed after applying the Terraform configuration
Variables
Command Description variableDefine a variable to be used in the Terraform configuration varReference a variable in the Terraform configuration terraform.tfvarsDefine default values for variables in a file named terraform.tfvars -var-fileSpecify a file containing variable values with the -var-file flag
Modules
Command Description moduleDefine a module to encapsulate a set of resources sourceSpecify the source of a module, which can be a local directory or a remote repository terraform getDownload and install modules from the specified sources terraform init -upgradeUpgrade the modules used in a Terraform project
Providers
Command Description providerDefine a provider to interact with an external system terraform initDownload and install the required providers for a Terraform project terraform providersList the providers used in a Terraform project terraform state replace-providerReplace the provider used by a resource in the Terraform state file
Best Practices
Use version control to manage Terraform configurations and state files.
Use modules to encapsulate reusable sets of resources.
Use variables to parameterize Terraform configurations.
Use data sources to retrieve information from external systems.
Use output values to display important information after applying a Terraform configuration.
Use the terraform plan command to preview changes before applying them.
Use the terraform destroy command to clean up resources when they are no longer needed.
Use the terraform fmt command to ensure consistent formatting of Terraform configurations.
Use the terraform validate command to check the syntax of Terraform configurations.
Use the terraform state command to inspect and modify the Terraform state file.
Reference:
https://terraform-docs.io/